所以我想創建的是一個顯示圖像按鈕的活動。圖像按鈕的背景指向可繪製文件夾中的xml,以顯示用於聚焦和點擊的不同圖片。這一切都很好。我的主要活動中有音樂,可以循環播放。默認情況下,圖像按鈕被設置爲說音樂開。我想要發生的是當按鈕被點擊時,主要聲音將暫停,按鈕背景將改變爲不同的xml可繪製佈局,即音樂關閉。當再次點擊時,音樂將從停止的位置恢復,並再次切換回音樂開啓。媒體播放器和如果語句
我遇到的一個問題是暫停主要聲音。因爲我是Android新手,可以在我的主要活動中引用的媒體播放器變量在不同的活動中進行更改?另外,在我的選項活動中,我有兩個if語句,點擊圖像按鈕以檢查聲音是否正在播放或不在,然後根據聲音是哪一個來暫停或恢復音樂。我不知道如何做第二條陳述,但我認爲第一條我認爲可能是對的。
對不起,我嘗試做很多不同的事情,但我試圖分解它。此外,當我開始選擇活動時,我正在關閉力量,並且我會將所有內容包括在主要活動中,因爲這是我建立mainSound的地方。感謝你給與我的幫助。
MainActivity:
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private MediaPlayer mainSound;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed //Things to Do
if(mainSound!= null) { mainSound.pause(); mainSound=null; } finish(); return true; } return super.onKeyDown(keyCode, event); }
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen);
mainSound.setLooping(true);
mainSound.start();
//CREATE BUTTON 1 & SOUND
final MediaPlayer buttonSound = MediaPlayer.create(
MainActivity.this, R.raw.swords);
ImageButton button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
button1Activity.class));
}
});
ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
multiplayerbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
multiplayerbuttonActivity.class));
}
});
ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton);
optionsbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
optionsActivity.class));
}
});
ImageButton creditbutton = (ImageButton) findViewById(R.id.creditbutton);
creditbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
creditsActivity.class));
}
});
ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton);
exitbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
finish();
mainSound.stop();
System.exit(0);
}
});
//END OF BUTTON1 & SOUND
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
optionsActivity:
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class optionsActivity extends Activity {
/** Called when the activity is first created. */
ImageButton musicbutton, musicbutton2;
private MediaPlayer mainSound;
final MediaPlayer buttonSound = MediaPlayer.create(
optionsActivity.this, R.raw.swords);
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mainSound.isPlaying()) {
musicbutton.setBackgroundResource(R.drawable.musicbutton2);
buttonSound.start();
mainSound.pause();
}
}
});
}
}
的logcat:
01-15 16:10:55.059: E/AndroidRuntime(7319): FATAL EXCEPTION: main
01-15 16:10:55.059: E/AndroidRuntime(7319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.crazycastles/com.crazycastles.optionsActivity}: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.access$2500(ActivityThread.java:129)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.os.Looper.loop(Looper.java:143)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.main(ActivityThread.java:4701)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:10:55.059: E/AndroidRuntime(7319): at dalvik.system.NativeStart.main(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): Caused by: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.media.MediaPlayer.create(MediaPlayer.java:641)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.crazycastles.optionsActivity.<init>(optionsActivity.java:17)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.Class.newInstanceImpl(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.Class.newInstance(Class.java:1429)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
謝謝,我會看看。 – 2012-01-16 19:18:07
@Zach J.祝你好運 – arufian 2012-01-16 19:32:24