Question

I am currently trying to play a MP3 file from the SDCard. Unfortunately I can't manage to do it.

Here is what I tried:

private MediaPlayer _mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _mediaPlayer = new MediaPlayer();
    }

    public void playButtonClick(View v) {
        File sdcard = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        File dir = new File(sdcard, "Music/Undone.mp3");

        _mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        _mediaPlayer.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                android.util.Log.d("Error", "What? " + String.valueOf(what)
                        + "  Extra?" + String.valueOf(extra));
                return false;
            }
        });
        _mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                _mediaPlayer.start();
            }
        });
        try {
            FileInputStream fileInputStream = new FileInputStream(dir);
            fileInputStream.close();
            _mediaPlayer.setDataSource(fileInputStream.getFD());
            _mediaPlayer.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Problem is that I get a SecurityException when calling _mediaPlayer.prepareAsync(). Here is the error message:

02-26 22:11:19.020: W/System.err(26048): java.lang.SecurityException
02-26 22:11:19.020: W/System.err(26048):    at android.media.MediaPlayer.prepareAsync(Native Method)
02-26 22:11:19.025: W/System.err(26048):    at com.bert.bertmusicplayer.MainActivity.playButtonClick(MainActivity.java:53)
02-26 22:11:19.025: W/System.err(26048):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:11:19.025: W/System.err(26048):    at java.lang.reflect.Method.invoke(Method.java:511)
02-26 22:11:19.025: W/System.err(26048):    at android.view.View$1.onClick(View.java:3686)
02-26 22:11:19.025: W/System.err(26048):    at android.view.View.performClick(View.java:4211)
02-26 22:11:19.025: W/System.err(26048):    at android.view.View$PerformClick.run(View.java:17267)
02-26 22:11:19.025: W/System.err(26048):    at android.os.Handler.handleCallback(Handler.java:615)
02-26 22:11:19.025: W/System.err(26048):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 22:11:19.025: W/System.err(26048):    at android.os.Looper.loop(Looper.java:137)
02-26 22:11:19.025: W/System.err(26048):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-26 22:11:19.025: W/System.err(26048):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:11:19.025: W/System.err(26048):    at java.lang.reflect.Method.invoke(Method.java:511)
02-26 22:11:19.025: W/System.err(26048):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-26 22:11:19.025: W/System.err(26048):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-26 22:11:19.025: W/System.err(26048):    at dalvik.system.NativeStart.main(Native Method)

Here are my permissions:

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

Just by the Logcat Output I can't figure out what the problem is.

I hope that someone can help me.

Was it helpful?

Solution

I'm guessing the cause is that you are calling getFD() on a closed FileInputStream. When closed the stream has likely got an invalid file descriptor, and you are setting that as the data source for your MediaPlayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top