When you use MediaPlayer to play a local Mp3 file , you need to be aware of the problem with access paths and access permissions .
1. Access path:/storage/emulated/0 This path is the root path of the phone, you can download the es file browser software to view the current playing MP3 specific path, through Environment.getexternalstoragedirectory (). GetPath () gets /storage/emulated/0 This sdcard path This in the organization void Android.media.MediaPlayer.setDataSource (String path) throws IOException, IllegalArgumentException, SecurityException, illegalstateexception path Path can be more clearly accessed paths, usually can be obtained by stitching strings.
2. Access rights:
Accessing the SD card requires access to the appropriate read and write permissions provided by Android.
Required permissions (add the following code in Androidmanifest.xml):
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Play the Mp3 file code in the SD card as follows:
SD Card Resources
MediaPlayer mediaplayer=new MediaPlayer ();
String sdcard=environment.getexternalstoragedirectory (). GetPath ();
Mediaplayer.setdatasource (sdcard+file.separator+ "Rockthatbody.mp3");
Mediaplayer.prepare (); //ready to play
Mediaplayer.start (); //Play
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Add permission to play songs in the local SD card in Android