在Android模拟器上播放音频片段出错

时间:2016-10-19 16:50:57

标签: android audio media-player

我试图播放名为"音乐"的音频片段。来自res / raw与我的模拟器。基本上,当您按下按钮"播放"时,音乐会根据其当前状态开始或停止。问题是,当我按下按钮播放时,会出现此错误并且音乐无法播放。 Android Monitor中返回的错误是:

10-19 05:35:55.514 22071-22071/com.app.app E/MediaPlayer: stop called in state 1, mPlayer(0x0)
10-19 05:35:55.514 22071-22071/com.app.app E/MediaPlayer: error (-38, 0)

如果我再次按下播放,应用程序崩溃,错误是:

10-19 05:53:02.193 5540-5540/com.app.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.ap.app, PID: 5540
                                                               java.lang.IllegalStateException
                                                                   at android.media.MediaPlayer._stop(Native Method)
                                                                   at android.media.MediaPlayer.stop(MediaPlayer.java:1236)
                                                                   at com.app.app.com.app.LibraryActivities.ClipsAdapter$MyViewHolder$1.onClick(ClipsAdapter.java:99)
                                                                   at android.view.View.performClick(View.java:5610)
                                                                   at android.view.View$PerformClick.run(View.java:22260)
                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

你能告诉我我的代码中的问题在哪里(见下文)?谢谢:))

private Clips currentClip;
MediaPlayer mediaPlayer = new MediaPlayer();

public MyViewHolder(final View itemView) {
        super(itemView);

        title = ((TextView) itemView.findViewById(R.id.title));
        play = ((ImageView) itemView.findViewById(R.id.play));
    }

public void display(Clips Clip) {
        currentClip = Clip;
        title.setText(Clip.title);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mediaPlayer != null)
                {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }

                Log.d(TAG, "You want to play: " + currentClip.title);
                mediaPlayer.create(activity, R.raw.music);
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {

                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.stop();
                            mediaPlayer.reset();
                            Log.d(TAG, currentClip.title + " has stopped");
                        } else {
                            mediaPlayer.start();
                            Log.d(TAG, currentClip.title + " is playing");
                        }
                    }
                });
            }
        });
    }

0 个答案:

没有答案