qq_42546454
qq_42546454
2021-06-19 16:11
采纳率: 0%
浏览 20

android studio 实现只有一个播放按钮的音乐播放器,

为什么点击两次他就同时播放两遍,单线程怎么写,有么有大佬帮帮小弟

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • zhiwenganyong
    像向日葵一样~ 2021-06-19 16:59
    
    public boolean isPaused = true;
    
    public void onClick(View v){
    
        mediaPlayer = MediaPlayer.create(MainActivity.this,path);
    
        if(isPaused) {
    
             
             isPaused = false; 
              
             mediaPlayer.start();
    
            
        }else{
            
            isPaused = true; 
            
            mediaPlayer.pause();
                    
    
        }
    
        
    
    }
    点赞 评论
  • qq_42546454
    qq_42546454 2021-06-19 17:16
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    
    
    import static com.example.musiceplay02.R.*;
    
    public class MainActivity extends AppCompatActivity {
        Button musicPlay, musicStop;
        TextView musicName;
        MediaPlayer mediaPlayer;
        int path = R.raw.mest;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(layout.activity_main);
            musicName = findViewById(id.musicName);
            musicPlay = findViewById(id.musicPlay);
            musicStop = findViewById(id.musicStop);
            mediaPlayer = new MediaPlayer();
            musicPlay.setOnClickListener(new musicStart());
            
        }
    
        public class musicStart implements View.OnClickListener {
           public boolean isPaused=true;
            public void onClick(View v) {
                mediaPlayer=MediaPlayer.create(MainActivity.this,path);
                if (isPaused){
                    isPaused=false;
                    mediaPlayer.start();
                }
                else {
                    isPaused=true;
                    mediaPlayer.pause();
                }
            }
    }
    }
    点赞 评论
  • g984160547
    帅次 2021-06-19 19:26

    判断mediaPlayer= null就执行你的操作即可

    点赞 评论

相关推荐