Add permission to play songs in the local SD card in Android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.