且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android按钮单击以播放音乐,再次单击以停止播放声音

更新时间:2023-12-04 08:24:04

根据 http://developer.android.com/reference/android/media/MediaPlayer.html ,我想您可以执行以下操作:

According to http://developer.android.com/reference/android/media/MediaPlayer.html, I suppose you could do something like this:

 Button two = (Button)this.findViewById(R.id.button2);
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
            two.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {

                    // If the music is playing
                    if(mp2.isPlaying() == true)
                        // Pause the music player
                        mp2.pause();
                    // If it's not playing
                    else
                        // Resume the music player
                        mp2.start();
                }
            });

您实际上可以只写

 if(mp2.isPlaying())

代替

 if(mp2.isPlaying() == true)

这只是为了了解正在发生的事情.

It's just for the sake of understanding what is going on.