我正在尝试从 sd 卡播放 mp3 音频文件,使用音频文件的路径和文件名来获取其 Uri。
我有一个Spinner
存储在 SD 卡中的曲目名称。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();
}
}
};
我收到以下错误:
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)
有人对此有任何想法吗?提前致谢 :)