简体   繁体   English

Android:当我单击时按钮不会更改为暂停状态

[英]Android: button is not changing to pause state when i click

i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button 我已经为媒体播放器编码,并且正在播放歌曲。在前端,仅使用图像按钮并将其硬编码为播放按钮

when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong 当我再次单击时,它必须将图像更改为暂停状态,但是每当我单击它时,我使用的按钮就会同时播放歌曲两次,这是错误的

but i need the song to be paused so whereever i used 但是我需要暂停歌曲,所以无论我在哪里使用

mp.start();

function which will be start to play the music from the url i added the code as 功能将开始从网址播放音乐,我将代码添加为

if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}

but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously 但是,当我单击“播放”按钮时,它将开始播放;如果再次单击该按钮,它将同时开始播放两次

please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function 请帮助我如何启动和暂停已在url中显示的mp3文件,否则不会阻塞自身,如果我使用其他功能

the full code of the project is 该项目的完整代码是

public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {



MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
@Override
public void onPrepared(MediaPlayer mp) 
 {
   Log.i("StreamAudioDemo", "prepare finished");
   //pd.setMessage("Playing.....");
   //mp.start();

   /*if(mp.isPlaying()== true)
   {
       mp.start();
       iv.setImageResource(R.drawable.pause);
   }
   else 
   {
       mp.pause();
       mp.release();
       iv.setImageResource(R.drawable.play);
   }*/

}
@Override
 public void onClick(View v) {

   try
    {
        pd = new ProgressDialog(this);
        pd.setMessage("Buffering.....");
        //pd.show();
        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnPreparedListener(this);
        mp.setOnErrorListener(this);
        mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
        mp.prepareAsync();
        mp.setOnCompletionListener(this);
    }
    catch(Exception e)
    {
        Log.e("StreamAudioDemo", e.getMessage());
    }
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
  pd.dismiss();
  return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
    pd.dismiss();
    Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();     
}}

thanks for your help in advance 谢谢你的帮助

i have commented some code above i have to re correct it 我已经在上面注释了一些代码,我必须重新更正它

您可以在您的onclickListener上尝试一下,在播放该播放器时,如果要暂停播放,则应调用MediaPlayer.pause(),如果要释放它的数据,则可以调用MediaPlayer.release(),然后,您可以播放下一个音乐。

use this 用这个

public void onPrepared(MediaPlayer mp) 
 {
   Log.i("StreamAudioDemo", "prepare finished");
   //pd.setMessage("Playing.....");

// mp.start(); - this should be removed 
   if(mp.isPlaying()== true)
   {
     mp.pause();

       iv.setImageResource(R.drawable.pause);
   }
   else 
   {
      mp.start();
       mp.release();
       iv.setImageResource(R.drawable.play);
   }

}

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

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