Media Player start stop start

后端 未结 9 1992
死守一世寂寞
死守一世寂寞 2020-12-10 13:09

I am making a new android sound application. I made a clickable button to play sound when I click on it. But I also want it to stop playing sound when I click for the second

相关标签:
9条回答
  • 2020-12-10 13:14

    A simple solution is to Use pause instead of stop and the seek to the beginning of the song.

    0 讨论(0)
  • 2020-12-10 13:18

    I know that this question is quite old but recently while learning Android, I also got stuck at this point and found a very simple solution which I'd like to share with everyone.

    Instead of trying to stop or reset the media, you can just seek back to the starting position.

    mediaPlayer.seekTo(0);
    

    For reference, I am also posting my code below:

    public class MainActivity extends AppCompatActivity {
    
        MediaPlayer mp;
    
        public void play(View view) {
            mp.start();
        }
    
        public void pause(View view) {
            mp.pause();
        }
    
        public void stop(View view) {
            // this seeks to the beginning of the file
            mp.seekTo(0);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mp = MediaPlayer.create(this, R.raw.sample_audio);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 13:19

    I am using this code to play a sound from the beginning while playing or not.

    mp.seekTo(0);
    mp.start();
    
    0 讨论(0)
  • 2020-12-10 13:22

    Change your class with below code:

    remove reset();.

    init well all components:

    MediaPlayer mpButtonClick1;
    MediaPlayer mpButtonClick2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prvi);
    
         mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
         mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
    
    
        Button dugme = (Button) findViewById(R.id.dugme);
        dugme.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (mpButtonClick1.isPlaying()) {
                    mpButtonClick1.stop();
                 }
                else {              
                    mpButtonClick1.start();
                }
             }   
    
        });
    
    0 讨论(0)
  • 2020-12-10 13:25

    Try to use pause instead of stop.

    Reason: if you pause the MediaPlayer, then you can resume it later. However, if you use stop, almost any other method won't work and you will have to prepare the MediaPlayer again (or create a new one).

    enter image description here

    More info: here and here

    PS: don't forget to release the memory when you finish using the resources.

    0 讨论(0)
  • 2020-12-10 13:33

    Hey please use following

    for stop -> media player mp.seekTo(0); mp.pause();

    again for start just call mp.start();

    0 讨论(0)
提交回复
热议问题