Android Media Player play/pause Button

前端 未结 7 1386
孤独总比滥情好
孤独总比滥情好 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:31

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    //the song was previously saved in the raw folder. The name of the song is mylife (it's an mp3 file) final MediaPlayer mMediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.mylife);

        //      Play song
        Button playButton = (Button) findViewById(R.id.play);
        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mMediaPlayer.start(); // no need to call prepare(); create() does that for you
            }
        });
        //      Pause song
        Button pauseButton = (Button) findViewById(R.id.pause);
        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mMediaPlayer.pause();
            }
        });
    
        //      Stop song - when you stop a song, you can't play it again. First you need to prepare it.
    
        Button stopButton = (Button) findViewById(R.id.stop);
        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mMediaPlayer.stop();
                mMediaPlayer.prepareAsync();
            }
        });
    
    }
    

    }

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