我必须开发一个Android应用程序。它允许用户静音或取消静音背景音乐。如果用户按下了静音按钮,当活动意图进行下一个活动时,它的背景音乐也应该处于静音模式。我曾参考过这个mute just the background music of my app,但仍面临一些问题。 这是我的第一个活动。
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer mp;
Button button1;
Button button2;
ImageButton speaker, mute;
private Context mContext = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speaker = (ImageButton) findViewById(R.id.speaker);
mute = (ImageButton) findViewById(R.id.mute);
mute.setVisibility(View.INVISIBLE);
if (mp != null) {
mp.release();
mp = null;
}
mp = MediaPlayer.create(MainActivity.this,R.raw.bg_music);
mp.start();
mp.setLooping(true);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.level);
button2 = (Button) findViewById(R.id.kategori);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Tahap.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, KategoriSenang.class);
startActivity(intent);
}
});
speaker.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mp.pause();
speaker.setVisibility(View.INVISIBLE);
mute.setVisibility(View.VISIBLE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Editor editor = prefs.edit();
editor.putBoolean("isMuted", true);
editor.commit();
}
});
mute.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mp.start();
mute.setVisibility(View.INVISIBLE);
speaker.setVisibility(View.VISIBLE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Editor editor = prefs.edit();
editor.putBoolean("isMuted", false);
editor.commit();
}
});
}
然后第二个活动从sharedpreferences中检索值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tahap);
mp = MediaPlayer.create(Tahap.this,R.raw.bg_music);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isMuted = prefs.getBoolean("isMuted", false);
if (isMuted=true){
//mp.stop();
mute.setVisibility(View.VISIBLE);
speaker.setVisibility(View.INVISIBLE);
}
else{
mute.setVisibility(View.INVISIBLE);
speaker.setVisibility(View.VISIBLE);
mp.start();
}
应用程序意外停止。
这里是logcat
04-12 01:55:13.803:D / JpgDecHal(6002):JpgDecHal :: JpgDecHal 04-12 01:55:13.803:W / JpegDecDriver(6002):打开JPEG驱动程序错误(权限被拒绝) 04-12 01:55:13.803:W / JpgDecHal(6002):[JPEG解码器] - Jpeg解码器资源忙 04-12 01:55:13.803:D / JpgDecHal(6002):JpgDecHal :: ~JpgDecHal 04-12 01:55:13.803:W / mHalJpgDec(6002):无法锁定资源 04-12 01:55:13.803:W / skia(6002):使用JPEG SW解码器 04-12 01:55:13.804:D / JpgDecHal(6002):JpgDecHal :: JpgDecHal 04-12 01:55:13.804:W / JpegDecDriver(6002):打开JPEG驱动程序错误(权限被拒绝) 04-12 01:55:13.804:W / JpgDecHal(6002):[JPEG解码器] - Jpeg解码器资源忙 04-12 01:55:13.804:D / JpgDecHal(6002):JpgDecHal :: ~JpgDecHal 04-12 01:55:13.804:W / mHalJpgDec(6002):无法锁定资源 04-12 01:55:13.805:W / skia(6002):HW不支持格式 04-12 01:55:13.805:W / skia(6002):使用JPEG SW解码器 04-12 01:55:13.811:D / skia(6002):jpeg_decoder成功完成,L:1028 !!! 04-12 01:55:13.819:D / MediaPlayer(6002):mPlayerID = 326 04-12 01:55:13.836:D / AndroidRuntime(6002):关闭VM 04-12 01:55:13.836:W / dalvikvm(6002):threadid = 1:线程退出未捕获异常(组= 0x4001b6a8) 04-12 01:55:13.839:E / AndroidRuntime(6002):致命异常:主要 04-12 01:55:13.839:E / AndroidRuntime(6002):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.downsyndromemobilelearningsystem / com.example.downsyndromemobilelearningsystem.Tahap}:java.lang.NullPointerException 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1755) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1774) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread.access $ 1500(ActivityThread.java:157) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1001) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.os.Handler.dispatchMessage(Handler.java:130) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.os.Looper.loop(SourceFile:351) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread.main(ActivityThread.java:3841) 04-12 01:55:13.839:E / AndroidRuntime(6002):at java.lang.reflect.Method.invokeNative(Native Method) 04-12 01:55:13.839:E / AndroidRuntime(6002):at java.lang.reflect.Method.invoke(Method.java:538) 04-12 01:55:13.839:E / AndroidRuntime(6002):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:969) 04-12 01:55:13.839:E / AndroidRuntime(6002):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 04-12 01:55:13.839:E / AndroidRuntime(6002):at dalvik.system.NativeStart.main(Native Method) 04-12 01:55:13.839:E / AndroidRuntime(6002):引起:java.lang.NullPointerException 04-12 01:55:13.839:E / AndroidRuntime(6002):at com.example.downsyndromemobilelearningsystem.Tahap.onCreate(Tahap.java:36) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 04-12 01:55:13.839:E / AndroidRuntime(6002):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1719) 04-12 01:55:13.839:E / AndroidRuntime(6002):... 11更多