获取SD卡路径播放音频文件的例外情况

时间:2013-12-21 14:24:08

标签: android

在为mediaplayer设置dataresource时遇到问题,尝试通过传递字符串但显示错误,它应该是Uri,所以改为Uri就像这样

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    try {
        System.out.println(path
                + myList.getItemAtPosition(position).toString());
        String newpath = path
                + myList.getItemAtPosition(position);
        Uri data = Uri.parse("file://"+ path
                + myList.getItemAtPosition(position));
        if(mp.mediaPlayer!=null){
            mp.mediaPlayer.stop();
            mp.mediaPlayer.reset();
            mp.mediaPlayer.release();
        }
        mp.mediaPlayer = MediaPlayer.create(getApplicationContext(), data);
        mp.mediaPlayer.setDataSource(newpath);
        mp.mediaPlayer.prepare();
        mp.mediaPlayer.start();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

仍然遇到此异常

12-21 07:53:08.284: W/System.err(32134): java.lang.IllegalStateException
12-21 07:53:08.294: W/System.err(32134):    at android.media.MediaPlayer.setDataSource(Native Method)
12-21 07:53:08.294: W/System.err(32134):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1204)
12-21 07:53:08.294: W/System.err(32134):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1176)
12-21 07:53:08.294: W/System.err(32134):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1122)
12-21 07:53:08.294: W/System.err(32134):    at com.srikanth.lullaby.ListFileActivity.onItemClick(ListFileActivity.java:283)
12-21 07:53:08.294: W/System.err(32134):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
12-21 07:53:08.294: W/System.err(32134):    at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
12-21 07:53:08.294: W/System.err(32134):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
12-21 07:53:08.294: W/System.err(32134):    at android.widget.AbsListView$1.run(AbsListView.java:3973)
12-21 07:53:08.294: W/System.err(32134):    at android.os.Handler.handleCallback(Handler.java:615)
12-21 07:53:08.294: W/System.err(32134):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 07:53:08.294: W/System.err(32134):    at android.os.Looper.loop(Looper.java:137)
12-21 07:53:08.304: W/System.err(32134):    at android.app.ActivityThread.main(ActivityThread.java:4960)
12-21 07:53:08.304: W/System.err(32134):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 07:53:08.304: W/System.err(32134):    at java.lang.reflect.Method.invoke(Method.java:511)
12-21 07:53:08.304: W/System.err(32134):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-21 07:53:08.304: W/System.err(32134):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)

1 个答案:

答案 0 :(得分:1)

嗯,你有:

mp.mediaPlayer = MediaPlayer.create(getApplicationContext(), data);

来自the documentation

  

public static MediaPlayer create (Context context, Uri uri)
  为给定MediaPlayer创建Uri的便捷方法。成功时,prepare()已经被调用,不能再被调用。

然后你做:

mp.mediaPlayer.setDataSource(newpath);

来自文档:

  

调用setDataSource ...将MediaPlayer状态的Idle对象转移到Initialized状态。 如果在任何其他州调用IllegalStateException,则会引发setDataSource()

所以文档很清楚问题是什么。由于您在执行setDataSource时已经指定了数据源,因此对MediaPlayer.create的调用似乎完全没必要。