Android Media Player play/pause Button

前端 未结 7 1448
孤独总比滥情好
孤独总比滥情好 2020-12-24 13:03

In my project, I am playing music file in android media player by using the following code

MediaPlayer mPlayer = MediaPlayer.create(MyActivity.this, R.raw.my         


        
7条回答
  •  长发绾君心
    2020-12-24 13:22

    Please try this::

    final Button bPlay = (Button) findViewById(R.id.bPlay);
    MediaPlayer song1 = MediaPlayer.create(tutorialFour.this, R.raw.fluet);
    Button bStop = (Button) findViewById(R.id.bStop);
    bPlay.setWidth(10);
    
    song1.setOnCompletionListener(new OnCompletionListener() {
    
            public void onCompletion(MediaPlayer mp) {
    
                bPlay.setText("Play");
    
    
            }
        });
    
    bPlay.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
                b = true;
    
                if (bPlay.getText().equals("Play") && b == true) {
    
                    song1.start();
    
                    bPlay.setText("Pause");
                    b = false;
                } else if (bPlay.getText().equals("Pause")) {
                    x = song1.getCurrentPosition();
                    song1.pause();
                    bPlay.setText("Resume");
                    Log.v("log", "" + x);
                    b = false;
                } else if (bPlay.getText().equals("Resume") && b == true) {
                    song1.seekTo(x);
                    song1.start();
                    bPlay.setText("Pause");
                    b = false;
                }
    
    
            }
    
        });
    

提交回复
热议问题