与媒体播放器问题 [英] Problems with Media Player

查看:152
本文介绍了与媒体播放器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以,我提出申请的声音为Android和有轻微的问题。
我做了我的应用程序停止播放当前的声音,当我在另一个按钮,点击播放另一种声音,所以他们不会对彼此顶部玩耍。
对于第4媒体播放器,它的工作好。但5日和6日媒体播放器的一个问题,我想不出什么导致它。
当我玩MP6,我想例如MP2玩,它首先会暂停,所以我需要双击进行播放。
我的继承人code

Alright so, I am making sound application for android and there is a slight problem. I made my app to stop playing current sound when I click on another button to play another sound, so they don't play on top of each other. For first 4 Media Players, it's working good. But there is a problem on 5th and 6th Media player and I can't figure out what's causing it. When I play Mp6, and I want to play for example Mp2, it pauses it first so I need to double click to play it. heres my code

mp=MediaPlayer.create(this, R.raw.hekler);

的ImageButton BTN1 =(的ImageButton)findViewById(R.id.btn1);
btn1.setOnClickListener(新View.OnClickListener(){

ImageButton btn1 = (ImageButton) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    if (mp.isPlaying()){
        mp.pause();
        mp.seekTo(0);
    }
    if (mp2.isPlaying()){
        mp2.pause();
        mp2.seekTo(0);
    }
    if (mp3.isPlaying()){
        mp3.pause();
        mp3.seekTo(0);
    }
    if (mp4.isPlaying()){
        mp4.pause();
        mp4.seekTo(0);
    }
    if (mp5.isPlaying()){
        mp5.pause();
        mp5.seekTo(0);

    }
    if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);

    }
    else{
        mp.start();
    }
    }

});

 mp2=MediaPlayer.create(this, R.raw.famas);
 ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
 btn2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }

        else{
            mp2.start();
        }
        }

 });
 mp3=MediaPlayer.create(this, R.raw.kalas);
 ImageButton btn3 = (ImageButton) findViewById(R.id.btn3);
 btn3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }

        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }

        else{
            mp3.start();
        }   
    }
});
 mp4=MediaPlayer.create(this, R.raw.emka);
 ImageButton btn4 = (ImageButton) findViewById(R.id.btn4);
 btn4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }


        else{
            mp4.start();
        }   
    }
});
 mp5=MediaPlayer.create(this, R.raw.uzi);
 ImageButton btn5 = (ImageButton) findViewById(R.id.btn5);
 btn5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }

        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);
        }

        else{
            mp5.start();
        }   
    }
});
 mp6=MediaPlayer.create(this, R.raw.sporet); 
 ImageButton btn6 = (ImageButton) findViewById(R.id.btn6);
 btn6.setOnClickListener(new View.OnClickListener() {

     @Override
        public void onClick(View v) {

            if (mp.isPlaying()){
                mp.pause();
                mp.seekTo(0);
            }
            if (mp2.isPlaying()){
                mp2.pause();
                mp2.seekTo(0);
            }
            if (mp3.isPlaying()){
                mp3.pause();
                mp3.seekTo(0);
            }

            if (mp4.isPlaying()){
                mp4.pause();
                mp4.seekTo(0);
            }

            if (mp5.isPlaying()){
                mp5.pause();
                mp5.seekTo(0);
            }
            if (mp6.isPlaying()){
                mp6.pause();
                mp6.seekTo(0);
            }

            else{
                mp6.start();
            }   
        }
    });

}

推荐答案

这一次,问题是你的为了的检查媒体播放器和的逻辑,如果别的冲突

This time, the problem is with your order of checking the media players and logical if else conflict.

请在您的MP2部分使用这种code的:

Make use of this code in your mp2 part:

 mp2=MediaPlayer.create(this, R.raw.famas);
 ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
 btn2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

    if (mp.isPlaying()){
        mp.pause();
        mp.seekTo(0);
    }
    if (mp3.isPlaying()){
        mp3.pause();
        mp3.seekTo(0);
    }
    if (mp4.isPlaying()){
        mp4.pause();
        mp4.seekTo(0);
    }
    if (mp5.isPlaying()){
        mp5.pause();
        mp5.seekTo(0);

    }
    if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }
     if (mp2.isPlaying()){ // Put this if branch just before else, likewise for other media players too.
        mp2.pause();
        mp2.seekTo(0);
    } 
    else{
        mp2.start();
    }
    }

});

请务必检查是否MP2是打还是不只是的其他条件之前。同样地为别人着想。也就是说,只有你选中其他玩家之后,你应该检查你正在尝试播放当前播放器。

Always check if mp2 is playing or not just before the else condition. Likewise for others. That is, only after do you check other players, you should check the current player you are trying to play.

让我解释一下,为什么在细节

 if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }

    else{
        mp2.start();
    }
    }

在MP2部分的 code 的一部分,你正在检查,如果的 MP6 的播放和停止它。由于的如果的保存好,其他不会的在这反过来执行。因此,你必须preSS的再次的制作MP2发挥。通过这样做,我说,就是检查当前玩家将与相应的else条件相匹配,因此您的问题将得到解决。

In that portion of your code in mp2 part, you are checking if mp6 is playing and stopping it. Since if holds good, else won't execute in that turn. Hence you have to press again to make mp2 play. By doing as I said, that is, checking the current player will be matched with the corresponding else condition, and hence your problem will be solved.

修改:在所有六个按钮的点击事件,你如果MP6只是else部分打前检查。这是一个逻辑缺陷。在else之前,您应该有如果的对应的的MediaPlayer对象的。

EDIT: In all six button click events, you have checked if mp6 is playing just before the else part. This is a logical flaw. Before the else, you should have the if of the corresponding mediaplayer object.

这篇关于与媒体播放器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆