简体   繁体   English

在MediaPlayer中连续播放声音时应用崩溃

[英]App Crashing when playing sounds in succession in MediaPlayer

I found the following code as an answer to a question here Playing multiple files in MediaPlayer one after other In android 我在这里找到以下代码作为问题的答案: 在MediaPlayer中一个接一个地播放多个文件

When I run it, the 3 sounds are played but then the app closes with the message 当我运行它时,会播放3种声音,但应用程序会关闭并显示消息

unfortunately the application has stopped. 不幸的是,该应用程序已停止。

Is there something missing inside the onCompletion method? onCompletion方法内是否缺少某些内容?

public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {

    int[] tracks = new int[3];
    int currentTrack = 0;
    private MediaPlayer mediaPlayer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tracks[0] = R.raw.p46;
        tracks[1] = R.raw.p52;
        tracks[2] = R.raw.p55;
        mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();
    }

    public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        if (currentTrack < tracks.length) {
            currentTrack++;
            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }

Change this: 更改此:

public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        if (currentTrack < tracks.length) {
            currentTrack++;
            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }

to : 至 :

public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        currentTrack++;
        if (currentTrack < tracks.length) {

            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }

basically you are getting a ArrayIndexOutofBoundException becoz at last currentTrack is 3 and and there is no item at index 3 ie 4th item 基本上你在最后一个currentTrack上得到一个ArrayIndexOutofBoundException becoz是3,并且索引3上没有项目,即第4个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同时播放两种声音时,Android MediaPlayer有时会跳过或失败 - Android MediaPlayer sometimes skips or fails when playing two sounds simultaneously 使用MediaPlayer在片段内播放声音 - Playing sounds inside of a fragment using MediaPlayer MediaPlayer:声音彼此叠加播放 - MediaPlayer: Sounds playing over the top of each other 即使在最小化应用程序并在浏览片段时重新启动后,声音也会继续播放 - Sounds keep playing even after minimizing the app and restarts when navigating through fragments 即使退出应用程序,Mediaplayer仍在播放? - Mediaplayer playing even after exiting app? 如何循环播放我的mediaPlayer,而不会让声音互相播放? - How do I loop threw my mediaPlayer on a delay without having sounds playing over one another? 单击按钮时MediaPlayer停止播放 - MediaPlayer stops playing when button is clicked 连接BassBoost时无法播放MediaPlayer-错误(-22,0) - MediaPlayer not playing when attaching BassBoost - error (-22, 0) 在MediaPlayer中添加会导致我的应用崩溃-无法输出声音 - Adding in MediaPlayer is crashing my app - Unable to output sound MediaPlayer使我的应用程序崩溃(编辑:这是由于数组超出范围引起的) - MediaPlayer Crashing my App (edit: it was caused by an out of bound array)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM