Android MediaPlayer - error (1, -4) when playing a song

I am trying to play an mp3 file from an SD card using the path and file name of the audio file to get its Uri.

I have a Spinner filled with the track names stored on the 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 get 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 know about this? Thanks in advance:)

+6
source share
3 answers

Dude you put mP.prepare(); on the wrong line, you need to call it after the function mP.setDataSource(context, uri); and to mp.start();

Also, do not try to make multiple instances of MediaPlayer . Do this and use the reset () function each time before starting.

See this link for more details.

+11
source

Do you have this permission in AndroidManifest.xml?

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

Incrorect is the name of the file or the path to the file that is set to .setDataSource(fileName) .

Or an invalid call to .prepare() (MUST .setDataSource() before .prepare() ). See @TheLittleNaruto answer.

0
source

Source: https://habr.com/ru/post/952208/


All Articles