![](/img/trans.png)
[英]I want to play audio one after another when i click button in android
[英]i want to stop current playing audio when user click on next play button
我的應用程序遇到問題。 所有音頻都播放良好,但問題是當我按第一個按鈕開始播放音頻時,它會播放它,如果我單擊也開始播放的下一個播放按鈕,但第一個音頻不會停止。 如何停止。 請幫忙
public class ringtone_tab extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone_tab);
clk6 = (Button) findViewById(R.id.btn_play6);
clk5 = (Button) findViewById(R.id.btn_play5);
clk4 = (Button) findViewById(R.id.btn_play4);
clk3 = (Button) findViewById(R.id.btn_play3);
clk2 = (Button) findViewById(R.id.btn_play2);
clk1 = (Button) findViewById(R.id.btn_play1);
mdx6 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_vandana);
mdx5 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_tandav_mantra);
mdx4 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shiv_om);
mdx3 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shiv);
mdx2 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_aaradhna);
mdx = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shankar);
}
public void setBtn_play6(View v)
{
if(mdx6.isPlaying())
{
mdx6.stop();
mdx6.reset();
mdx6.release();
}
mdx6 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mdx6.start();
}
public void setBtn_play5(View v)
{
if(mdx5.isPlaying())
{
mdx5.stop();
mdx5.reset();
mdx5.release();
}
mdx5 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mdx5.start();
}
public void setBtn_play4(View v)
{
if(mdx4.isPlaying())
{
mdx4.stop();
mdx4.reset();
mdx4.release();
}
mdx4 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mdx4.start();
}
public void setBtn_play3(View v)
{
if(mdx3.isPlaying())
{
mdx3.stop();
mdx3.reset();
mdx3.release();
}
mdx3 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mdx3.start();
}
public void setBtn_play2(View v)
{
if(mdx2.isPlaying())
{
mdx2.stop();
mdx2.reset();
mdx2.release();
}
mdx2 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mdx2.start();
}
public void setBtn_play1(View v)
{
if(mdx.isPlaying())
{
mdx.stop();
mdx.reset();
mdx.release();
}
mdx = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mdx.start();
}
}
引入新的方法調用,例如stopAllplayers
private void stopAllPlayers(){
if(mdx1 != null && mdx1.isPlaying())
{mdx1.stop();mdx1.reset(); mdx1.release();}
if(mdx2 != null && mdx2.isPlaying())
{mdx2.stop();mdx2.reset(); mdx2.release();}
if(mdx3 != null && mdx3.isPlaying())
{mdx3.stop();mdx3.reset(); mdx3.release();}
if(mdx4 != null && mdx4.isPlaying())
{mdx4.stop();mdx4.reset(); mdx4.release();}
if(mdx5 != null && mdx5.isPlaying())
{mdx5.stop();mdx5.reset(); mdx5.release();}
if(mdx6 != null && mdx6.isPlaying())
{mdx6.stop();mdx6.reset(); mdx6.release();}
}
然后在所有播放方法中調用此方法。
public void setBtn_play6(View v)
{
stopAllPlayers()
........
對所有setBtn_play1,setBtn_play2 .....執行此操作
在一個媒體播放器上使用以下代碼
public class ringtone_tab extends AppCompatActivity {
Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone_tab);
clk6 = (Button) findViewById(R.id.btn_play6);
clk5 = (Button) findViewById(R.id.btn_play5);
clk4 = (Button) findViewById(R.id.btn_play4);
clk3 = (Button) findViewById(R.id.btn_play3);
clk2 = (Button) findViewById(R.id.btn_play2);
clk1 = (Button) findViewById(R.id.btn_play1);
mediaPlayer = new MediaPlayer();
}
public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();
}
public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();
}
public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();
}
public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();
}
public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();
}
public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}
private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.