0
在我的主要活動的應用程序中,我有一個設置爲循環的媒體播放器變量。當我點擊主頁按鈕時,音樂繼續播放。我寧願發生的事情是音樂在用戶點擊主頁按鈕時關閉。從我收集的主頁按鈕不能被覆蓋,我嘗試onPause(),但是當我點擊我的任何圖像按鈕時,音樂媒體播放器將停止。有沒有解決這個問題?謝謝,並讓我知道你是否需要我的代碼。如何在主頁按鈕被擊中時停止媒體播放器
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. */
static MediaPlayer mainSound;
@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.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 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
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
mainSound.pause(); //stop media player
return true;
}
return super.onKeyDown(keyCode, event);
}
}
我不確定我做了什麼,但是我放入了這段代碼,並且它在主頁按鈕被擊中時仍然播放。我把我的代碼放在上面。 – 2012-01-16 23:01:53