package com.example.tobiadegoroye.pokemonsoundboard;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class PokemonSoundboard extends AppCompatActivity {
MediaPlayer psyduckplayer; //member variable
MediaPlayer pikachuplayer; //member variable
MediaPlayer diglettplayer; //member variable
ImageButton mpsyduckbutton;
ImageButton mpikachubutton;
ImageButton mdiglettbutton;
View.OnClickListener psyducklistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
psyduckplayer.start();
}
};
View.OnClickListener pikachulistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
pikachuplayer.start();
}
};
View.OnClickListener digletlistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
diglettplayer.start();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon_soundboard);
mpsyduckbutton = (ImageButton) findViewById(R.id.psyduckbutton);
mpikachubutton = (ImageButton) findViewById(R.id.pikachubutton);
mdiglettbutton = (ImageButton) findViewById(R.id.diglettbutton);
psyduckplayer = MediaPlayer.create(this,R.raw.psyduck);
pikachuplayer = MediaPlayer.create(this,R.raw.pikachu);
diglettplayer = MediaPlayer.create(this,R.raw.diglett);
mpsyduckbutton.setOnClickListener(psyducklistner);
mpikachubutton.setOnClickListener(pikachulistner);
mdiglettbutton.setOnClickListener(digletlistner);
}
}
答案 0 :(得分:0)
使用MediaPlayer.isPlaying()
和MediaPlayer.stop()
方法。
public void stopPlaying()
{
if(pikachuplayer != null && pikachuplayer.isPlaying())
{
pikachuplayer.stop();
}
if(digletplayer != null && digletplayer.isPlaying())
{
digletplayer.stop();
}
if(psyduckplayer != null && psyduckplayer.isPlaying())
{
psyduckplayer.stop();
}
}
View.OnClickListener psyducklistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlaying();
psyduckplayer.start();
}
};
View.OnClickListener pikachulistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlaying();
pikachuplayer.start();
}
};
View.OnClickListener digletlistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlaying();
diglettplayer.start();
}
};
View.OnClickListener digletlistner = new View.OnClickListener() {
@Override
public void onClick(View v) {
diglettplayer.start();
}
};
stopPlaying()
方法停止播放所有媒体,只有当它们正在播放而不是null
时(它们可能不是null
,因为它们是成员变量,但它是仍然是一个很好的做法)。