我目前正在尝试从 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 输出,我无法弄清楚问题所在。
我希望有人能帮助我。