Play mp3 sounds from SD card

前端 未结 2 447
慢半拍i
慢半拍i 2020-12-19 05:29

I have a directory on my tablet\'s SD card called \"/Android/data/com.example.android.app/files\". I just created it manually because I don\'t know how else to test this as

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 06:02

    root folder path may be variable in android devices, for example, it can be /mnt/sdcard in a device and another can be /sdcard/ .therefore you can use an easy way for extracting root folder by using android.os.Environment.getExternalStorageDirectory(); and after definig it, u can directory point to your directory in the internal or external path that u want. for example: ( try catch can generated by IDE automatically)

            mediaPlayer = new MediaPlayer();
            final File root = android.os.Environment.getExternalStorageDirectory();
            try {
    
                mediaPlayer.setDataSource(root.getAbsolutePath() + AUDIO_FILES_DIRECTORY+"/musicFile.MP3");
    
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
    
                mediaPlayer.prepare();
    
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

提交回复
热议问题