我目前正在尝试从SDCard播放MP3文件。不幸的是,我无法做到这一点。

这是我尝试过的:

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();
        }
    }


问题是调用SecurityException时得到_mediaPlayer.prepareAsync()。这是错误消息:

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)


这是我的权限:

<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" >


仅靠Logcat输出,我无法弄清楚问题出在哪里。

我希望有人能帮助我。

最佳答案

我猜原因是您在封闭的getFD()上调用FileInputStream。关闭时,流可能具有无效的文件描述符,并且您将其设置为MediaPlayer的数据源。

09-28 00:01