How to play, pause and stop a song with only one button?

前端 未结 4 703
野的像风
野的像风 2021-01-16 10:23

I have tried to make an application in android to play, pause and stop a song with one button only.

Can anyone show me how can I make that application?

相关标签:
4条回答
  • 2021-01-16 10:37
    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;
                    }
    
    
                }
    
            });
    
    0 讨论(0)
  • 2021-01-16 10:48

    click once to toggle between play/pause. double click for stop.

    0 讨论(0)
  • 2021-01-16 10:52

    if you have the reference for the button at onClickListenerObject, you may call setOnClickListener in there.

    0 讨论(0)
  • 2021-01-16 10:54

    With one button, the idea, I would guess, can be as follows:

    • Keep a boolean indicating whether the playback is active at the moment; set it to true when you start playback, and false when you stop/pause it or it completes.
    • On a regular tap of the button, if no playback is active, then start the playback
    • On a regular tap, if playback is active, then pause it
    • On a long tap, if playback is active, stop it
    0 讨论(0)
提交回复
热议问题