ToggleButton

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doPlay"
            android:src="@drawable/play" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doPause"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doStop"
            android:src="@drawable/stop" />
    </LinearLayout>

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

</LinearLayout>
package com.example.app15;

import java.io.IOException;
import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    MediaPlayer player;
    ToggleButton tb;
    AudioManager am;
    boolean isMute = false;// 離開程式前要靜音關閉

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

        player = MediaPlayer.create(this, R.raw.stripme);

        tb = (ToggleButton) this.findViewById(R.id.toggleButton1);
        am = (AudioManager) this.getSystemService(Service.AUDIO_SERVICE);

        tb.setBackgroundResource(R.drawable.off);// 按鈕的圖片

        CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                isMute = isChecked;
                am.setStreamMute(AudioManager.STREAM_MUSIC, isChecked);// 讓音樂靜音

                tb.setBackgroundResource(isMute ? R.drawable.on
                        : R.drawable.off);// 切換按鈕的圖片
            }
        };
        tb.setOnCheckedChangeListener(listener);

        // 有些手機會需要下面這段
        try {
            player.prepare(); // 主動要求準備音樂
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void doPlay(View view) {
        player.start();
    }

    public void doPause(View view) {
        if (player.isPlaying()) {
            player.pause();
        }
    }

    public void doStop(View view) {
        if (player.isPlaying()) {
            player.stop();
        }
        player = MediaPlayer.create(this, R.raw.stripme);

        // 有些手機會需要下面這段
        try {
            player.prepare(); // 主動要求準備音樂
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        if (player.isPlaying()) {
            player.stop();
        }
        player.release();// 釋放資源

        if (isMute) {
            am.setStreamMute(AudioManager.STREAM_MUSIC, false);
        }
        super.onStop();
    }
}