简体   繁体   English

如何在单击按钮时停止播放声音以启动另一个声音文件?

[英]How to stop playing sounds on button click to start another sound file?

I have two buttons and two songs.我有两个按钮和两首歌。 If I click the first button, the first sound plays.如果我单击第一个按钮,将播放第一个声音。 But if I click the second button while the first sound is playing the second sound starts playing too.但是,如果我在播放第一个声音时单击第二个按钮,第二个声音也会开始播放。
How can I stop other sounds?我怎样才能停止其他声音?

My code:我的代码:

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

    final MediaPlayer johnCenaPlayer = MediaPlayer.create(this, R.raw.john_cena);
    Button johnCenaButton = (Button) findViewById(R.id.john_cena_button);
    johnCenaButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            johnCenaPlayer.start();
        }
    });

    final MediaPlayer haGayPlayer = MediaPlayer.create(this, R.id.ha_gay_button);
    Button haGayButton = (Button) findViewById(R.id.ha_gay_button);
    haGayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            haGayPlayer.start();
        }
    });
}

Stop the other MediaPlayer in clickListener using stop() method.使用 stop() 方法停止 clickListener 中的其他 MediaPlayer。

public void onClick(View view) {
    ha_gay.stop()
    john_cena.start();
}

If you have many audio files use a single MediaPlayer and change the sources dynamically.如果您有许多音频文件,请使用单个 MediaPlayer 并动态更改源。

public class MainActivity extends AppCompatActivity {


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

    final MediaPlayer mediaPlayer = new MediaPlayer();

    Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
    john_cena_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopAndPlay(R.raw.john_cena, mediaPlayer);
        }
    });

    Button ha_gay_button = (Button) findViewById(R.id.ha_gay_button);
    ha_gay_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopAndPlay(R.raw.ha_gay, mediaPlayer);
        }
    });
}

// This resets the mediaPlayer and starts the given audio
private void stopAndPlay(int rawId, MediaPlayer mediaPlayer) {
    mediaPlayer.reset();
    AssetFileDescriptor afd = this.getResources().openRawResourceFd(rawId);
    try {
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
}

}

If you have multiple audio files, I think an easy solution would be to assign each of the buttons created to play your audio file, a tag.The use of tags makes it much easier to start, and stop your button. 如果您有多个音频文件,我认为一个简单的解决方案是将为播放音频文件而创建的每个按钮分配一个标签。标签的使用使启动和停止按钮更加容易。 When using tags, you should set the tag equal to the file name. 使用标签时,应将标签设置为与文件名相同。 Once you do that, here is the actual code that you can use: 完成此操作后,可以使用以下实际代码:

     public void play(View view){

    buttonPressed = (Button) view;

    mp = MediaPlayer.create(this, getResources().getIdentifier(buttonPressed.getTag().toString(), "raw", getPackageName()));

    mp.start();

    }

Side not - Assign your media player at the very beginning of your code, so it will be easier for you to call it later on. 不支持-在代码的开头分配媒体播放器,这样以后您就可以更轻松地调用它。 EX: EX:

    MediaPlayer mp; 

To stop the file from playing, try this: 要停止播放文件,请尝试以下操作:

    public void stop(View view){

    if(mp.isPlaying()){

    mp.stop();
    }
    }

I'm only 14, and I just started learning java, not too long ago, but with the knowledge that I have, thus far, I think my code should be an easy fix for your problem. 我只有14岁,不久前我才刚开始学习Java,但是到目前为止,我已经知道了,我认为我的代码应该很容易解决您的问题。

Try this one.试试这个。 Do it same for the other.对另一个做同样的事情。

final MediaPlayer john_cena = MediaPlayer.create(this, R.raw.john_cena);
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (he_gay.isPlaying()){
             he_gay.stop();
    }
        else if (john_cena.isPlaying()){
            john_cena.seekTo(0);
    }  else{
       john_cena.start();
    }
}
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM