在为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)
答案 0 :(得分:1)
嗯,你有:
mp.mediaPlayer = MediaPlayer.create(getApplicationContext(), data);
public static MediaPlayer create (Context context, Uri uri)
为给定MediaPlayer
创建Uri
的便捷方法。成功时,prepare()
已经被调用,不能再被调用。
然后你做:
mp.mediaPlayer.setDataSource(newpath);
来自文档:
调用
setDataSource
...将MediaPlayer
状态的Idle
对象转移到Initialized
状态。 如果在任何其他州调用IllegalStateException
,则会引发setDataSource()
。
所以文档很清楚问题是什么。由于您在执行setDataSource
时已经指定了数据源,因此对MediaPlayer.create
的调用似乎完全没必要。