且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

音乐播放,但不会暂停在Android

更新时间:2023-10-30 11:11:10

试试这个:


  

将这个出侧的OnCreate点击数说()


块引用>
  =玩家MediaPlayer.create(这一点,Uri.fromFile(AUDIOFILE));

 如果(audioFile.exists())
 {
    如果(玩家!= NULL)
    {
       如果(player.isPlaying())
       {
          player.pause();
       }
       其他
       {
         player.start();
       }
    }
}

As I said above In the code below when i click the button music is play but When I click button for the second time not pause and Will play again
what can i do? please help me

public class AudioPlayer extends Activity implements OnClickListener {
Button playButton;
MediaPlayer player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.ButtonPlayStop);
playButton.setOnClickListener(this);
}

public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/bluetooth/یه سوال دارم مگه.mp3");
player = MediaPlayer.create(this,Uri.fromFile(audioFile));
if(audioFile.exists())
{
if(player.isPlaying()){
    if(player!=null){
        player.pause();
    }
}else{
    // Resume song
    if(player!=null){
        player.start();
    }
}

}
else
{
Builder alert = new AlertDialog.Builder( AudioPlayer.this);
                alert.setTitle("Alert");
                alert.setMessage("فایل دانلود نشده است");
                alert.setPositiveButton("OK", null);
                alert.show();
 }

 }

Try this:

Put this out side Onlick say in Oncreate()

player = MediaPlayer.create(this,Uri.fromFile(audioFile));

And

 if(audioFile.exists())
 {
    if(player!=null)
    {
       if(player.isPlaying())
       {
          player.pause();
       }   
       else
       {
         player.start();
       }
    }
}