I am trying to play an mp3 audio file from sd card, using the path and the filename of the audio file to get its Uri.
I have a Spinner
populated with the tracks names stored in sd card.
When an item is selected, the following code will be executed:
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
OnItemSelectedListener listener = new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
TextView tv = (TextView) selectedItemView;
File file = new File(uri.getPath() + "/" + tv.getText().toString());
Log.i(TAG, "------------- PATH : " + file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
MediaPlayer mP = new MediaPlayer();
try {
mP.setDataSource(context, uri);
mP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mP.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
I am getting the following errors:
08-21 16:16:37.099: I/MusicFileActivity(3940): ------------- PATH : /external/audio/media/antazirouka
08-21 16:16:37.159: W/MediaPlayer(3940): info/warning (1, 26)
08-21 16:16:37.159: E/MediaPlayer(3940): error (1, -4)
08-21 16:16:37.159: W/System.err(3940): java.io.IOException: Prepare failed.: status=0x1
08-21 16:16:37.169: W/System.err(3940): at android.media.MediaPlayer.prepare(Native Method)
08-21 16:16:37.169: W/System.err(3940): at com.example.project.MusicFileActivity$1.onItemSelected(MusicFileActivity.java:84)
08-21 16:16:37.169: W/System.err(3940): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
08-21 16:16:37.169: W/System.err(3940): at android.widget.AdapterView.access$200(AdapterView.java:42)
08-21 16:16:37.169: W/System.err(3940): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
08-21 16:16:37.169: W/System.err(3940): at android.os.Handler.handleCallback(Handler.java:587)
08-21 16:16:37.169: W/System.err(3940): at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 16:16:37.169: W/System.err(3940): at android.os.Looper.loop(Looper.java:123)
08-21 16:16:37.169: W/System.err(3940): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-21 16:16:37.169: W/System.err(3940): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 16:16:37.169: W/System.err(3940): at java.lang.reflect.Method.invoke(Method.java:507)
08-21 16:16:37.169: W/System.err(3940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-21 16:16:37.169: W/System.err(3940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-21 16:16:37.169: W/System.err(3940): at dalvik.system.NativeStart.main(Native Method)
Does anyone have any idea about it? Thanks in advance :)
Dude you have placed mP.prepare();
at wrong line
you need to call it after mP.setDataSource(context, uri);
function
and before mp.start();
Also not try to make several instances of MediaPlayer. Make one and use reset() function everytime before start.
Please see this link for more help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With