音乐播放,但不会暂停在Android [英] music play but not pause on android

查看:424
本文介绍了音乐播放,但不会暂停在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我上面所说的在下面当我按一下按钮音乐code是玩,但是当我点击按钮,第二次不会暂停,将再次发挥搜索
我能做什么?
请大家帮我搜索

 公共类AudioPlayer延伸活动实现OnClickListener {
按钮为playButton;
MediaPlayer的播放器;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
为playButton =(按钮)this.findViewById(R.id.ButtonPlayStop);
playButton.setOnClickListener(本);
}公共无效的onClick(视图v){
意向意图=新意图(android.content.Intent.ACTION_VIEW);
文件SD卡= Environment.getExternalStorageDirectory();
文件的AudioFile =新的文件(sdcard.getPath()+/蓝牙/یهسوالدارممگهMP3。);
玩家= MediaPlayer.create(这一点,Uri.fromFile(AUDIOFILE));
如果(audioFile.exists())
{
如果(player.isPlaying()){
    如果(玩家!= NULL){
        player.pause();
    }
}其他{
    //简历歌
    如果(玩家!= NULL){
        player.start();
    }
}}
其他
{
生成警报=新AlertDialog.Builder(AudioPlayer.this);
                alert.setTitle(警报);
                alert.setMessage(فایلدانلودنشدهاست);
                alert.setPositiveButton(OK,NULL);
                alert.show();
 } }


解决方案

试试这个:


  

将这个出侧的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();
       }
    }
}

这篇关于音乐播放,但不会暂停在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆