String path = Environment.getExternalStorageDirectory().toString()+"/Music/";
File directory = new File(path);
File[] mSongsList = directory.listFiles();
Uri mUri= Uri.fromFile(mSongsList[0]);
Log.v("MainActivity",mUri.toString());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(this, mUri);
mMediaPlayer.prepare();
mMediaPlayer.start();
I am trying to play the first song in my list but, i am getting an error on setDataSource.
it say " Unhandled Exception: java.io.IOException "
Place music files(s) under res/raw or any other folder under SD card. We are going to refer the audio file based on the Uri.
Create MediaPlayer object:
MediaPlayer mPlayer = new MediaPlayer();
Locate the audio file:
Uri myUri1 = Uri.parse("file:///sdcard/Songs/ARR Hits/hosannatamil.mp3");
Set the audio stream type of the media player:
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Set the data source as a content Uri:
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI
correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI
correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI
correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
Prepare the player for playback, synchronously:
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI
correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI
correctly!", Toast.LENGTH_LONG).show();
}
Start the player:
mPlayer.start();
The above information has been taken from the below link:
" https://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-uri/ "
Keep your code inside the try-catch
block.
try{
// your code
}catch(IOException exception){
}
This is because you are trying to get the music file
. If its not present then it will throw IOException
, so you must handle this.
Replace Your Code Like below:
Note: Make Sure That You Have Some Music In MusicDirectory.
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
File directory = new File(path);
try{
File[] mSongsList = directory.listFiles();
Uri mUri= Uri.fromFile(mSongsList[0]);
Log.v("MainActivity",mUri.toString());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(this, mUri);
mMediaPlayer.prepare();
mMediaPlayer.start();
}catch(IOException exception){
e.printStacktrace();
}
First of all make sure you have read permissions in your manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
If you are using the android version Marshmallow or above you need to ask the user for Runtime Permissions
.
This is a good article to request runtime permissions.
For now you can go to the app info page and give the permissions manually. It will start working.
You can play the song from your device by the following codes, but I don't know how to play next songs and previous songs , if you have any idea , pls share me.
fun getURI(){
val path: String =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()
val directory = File(path)
try {
mSongsList= directory.listFiles()
val mUri = Uri.fromFile((mSongsList as Array<File>?)!![0])
Log.v("MainActivity", mUri.toString())
mp!!.reset()
mp!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
mp!!.setDataSource(this, mUri)
mp!!.prepare()
mp!!.start()
} catch (e: IOException) {
e.printStackTrace()
}
}
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.