不幸的是,当播放器播放歌曲时,我的应用程序停止,完成歌曲后,我想要更改应用程序停止播放

时间:2017-05-31 11:22:56

标签: android media-player

这是代码,在播放一首歌之后它应该在应用程序关闭时更改顶部。在这里,我想改变歌曲它会停止

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                               @Override
                               public void onCompletion(MediaPlayer mpp) {
                                   mp.stop();
                                   mp.reset();
                                   mp.release();


                                   pos =(pos+1)% mysongs.size();
                                   String mediaPath =file.getAbsolutePath()+ "/" + mysongs.get(pos)+".wav";
                                   Log.d("pathh1", mediaPath);
                                   u = Uri.parse(mediaPath);
                                   mp = MediaPlayer.create(getApplicationContext(),u);
                                   // mp = MediaPlayer.create(getApplicationContext(),u);
                                   try {
                                       mp.setDataSource(getApplicationContext(),u);
                                       mp.prepare();
                                       mp.start();
                                       th.start();
                                   } catch (IOException e) {
                                       e.printStackTrace();
                                   }

                               }
                           });

ERROR:

05-31 17:24:52.908 9460-9460/php.example.abs.voicerecorder E/MediaPlayer: Should have subtitle controller already set
05-31 17:25:02.798 9460-9460/php.example.abs.voicerecorder D/pathh1: /storage/sdcard/MediaRecorderSample/Recordingg4.wav
05-31 17:25:02.798 9460-9460/php.example.abs.voicerecorder D/MediaPlayer: Couldn't open file on client side, trying server side
05-31 17:25:02.800 9460-9460/php.example.abs.voicerecorder D/AndroidRuntime: Shutting down VM                                                                            --------- beginning of crash
05-31 17:25:02.801 9460-9460/php.example.abs.voicerecorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: php.example.abs.voicerecorder, PID: 9460
                                                                             java.lang.IllegalStateException
                                                                                 at android.media.MediaPlayer._setDataSource(Native Method)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1119)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1104)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1083)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1060)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1014)
                                                                                 at android.media.MediaPlayer.setDataSource(MediaPlayer.java:957)
                                                                                 at php.example.abs.voicerecorder.Player$2.onCompletion(Player.java:137)
                                                                                 at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2538)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

代码中出现的一个问题是,在使用数据源创建播放器后设置数据源。这是错误的方式:

mp = MediaPlayer.create(getApplicationContext(),u);
                                   // mp = MediaPlayer.create(getApplicationContext(),u);
                                   try {
                                       mp.setDataSource(getApplicationContext(),u);
                                       mp.prepare();

如API中所述:

调用setDataSource ....将处于空闲状态的MediaPlayer对象传输到Initialized状态。如果在任何其他状态下调用setDataSource(),则抛出IllegalStateException。

因此,您已经通过以下方式初始化了MediaPlayer对象:

mp = MediaPlayer.create(getApplicationContext(),u);

然后在try / catch块中设置数据源:

mp.setDataSource(getApplicationContext(),u);

这不起作用,因为此时,MediaPlayer不再处于空闲状态。 try / catch没有捕获错误,因为它抛出IllegalStateException而你只是抓住了IOException

所以你唯一要做的就是删除

mp.setDataSource(getApplicationContext(),u);

相关问题