简体   繁体   中英

Cannot play local mp3 file using MediaPlayer in Android

I am working with MediaPlayer on Android to play mp3. When I play mp3 file from network using url , it is working fine. But when I play local mp3 file in Download folder, it is not playing. It is not showing error as well.

This is how I play local file

    MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("Download/9591455192457575.mp3"));
    mediaPlayer.start();

I already checked, the file really exists. I run it in "try catch" statement and error message in catch block is empty. What is wrong with my code, and how can I play local file?

First you cant access download directory like that. You need to get Download library like this.

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

(available since API 8) To access individual files in this directory use either File.list() or File.listFiles().

Try like this

MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/9591455192457575.mp3"));

NOTE

you need this permission

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM