<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) {
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) {
e.printStackTrace();
} catch (IOException e) {
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onStop() {
if (player.isPlaying()) {
player.stop();
}
player.release();
if (isMute) {
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
super.onStop();
}
}