在我的主要活动开始时,我呼吁媒体播放器播放一首歌
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourSurfaceView = new MyBringBack(this);
ourSurfaceView.setOnTouchListener(this);
Context ctx;
firstRun = 0;
MediaPlayer ourSong = MediaPlayer.create(MainActivity.this, R.raw.music);
ourSong.setLooping(true);
ourSong.start();
然而,当我打开一个活动时,它并没有停止。下次我开始活动时,它不会停止。但我第三次这样做。以下是为第三个活动制作意图的代码:
(我为此使用了ViewPager)
public void fiveGames(View view){
Intent i = new Intent(this, WhichNormalLevelActivity.class);
i.putExtra("uh", 1);
startActivity(i);
finish();
}
以下是创建活动的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_which_normal_level);
ctx = this;
box = (TextView)findViewById(R.id.box_button);
whichPage = getIntent().getIntExtra("uh", 1);
final SharedPreferences prefs = getSharedPreferences("SETTINGS", 0);
if(whichPage == 1) {
whichClass = FiveByFiveGame.class;
box.setText("5x5 Games");
box.setTextColor(Color.parseColor("#ffecea00"));
isLevelOneComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_ONE_COMPLETE", false);
isLevelTwoComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_TWO_COMPLETE", false);
isLevelThreeComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_THREE_COMPLETE", false);
isLevelFourComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_FOUR_COMPLETE", false);
isLevelFiveComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_FIVE_COMPLETE", false);
isLevelSixComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_SIX_COMPLETE", false);
isLevelSevenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_SEVEN_COMPLETE", false);
isLevelEightComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_EIGHT_COMPLETE", false);
isLevelNineComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_NINE_COMPLETE", false);
isLevelTenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_TEN_COMPLETE", false);
isLevelElevenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_ELEVEN_COMPLETE", false);
isLevelTwelveComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_TWELVE_COMPLETE", false);
isLevelThirteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_THIRTEEN_COMPLETE", false);
isLevelFourteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_FOURTEEN_COMPLETE", false);
isLevelFifteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_FIFTEEN_COMPLETE", false);
isLevelSixteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_SIXTEEN_COMPLETE", false);
isLevelSeventeenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_SEVENTEEN_COMPLETE", false);
isLevelEighteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_EIGHTEEN_COMPLETE", false);
isLevelNineteenComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_NINETEEN_COMPLETE", false);
isLevelTwentyComplete = prefs.getBoolean("FIVE_BY_FIVE_LEVEL_TWENTY_COMPLETE", false);
}
if(whichPage == 2) {
whichClass = SevenBySevenGame.class;
box.setText("7x7 Games");
box.setTextColor(Color.parseColor("#ff00ec1a"));
isLevelOneComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_ONE_COMPLETE", false);
isLevelTwoComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_TWO_COMPLETE", false);
isLevelThreeComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_THREE_COMPLETE", false);
isLevelFourComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_FOUR_COMPLETE", false);
isLevelFiveComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_FIVE_COMPLETE", false);
isLevelSixComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_SIX_COMPLETE", false);
isLevelSevenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_SEVEN_COMPLETE", false);
isLevelEightComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_EIGHT_COMPLETE", false);
isLevelNineComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_NINE_COMPLETE", false);
isLevelTenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_TEN_COMPLETE", false);
isLevelElevenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_ELEVEN_COMPLETE", false);
isLevelTwelveComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_TWELVE_COMPLETE", false);
isLevelThirteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_THIRTEEN_COMPLETE", false);
isLevelFourteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_FOURTEEN_COMPLETE", false);
isLevelFifteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_FIFTEEN_COMPLETE", false);
isLevelSixteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_SIXTEEN_COMPLETE", false);
isLevelSeventeenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_SEVENTEEN_COMPLETE", false);
isLevelEighteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_EIGHTEEN_COMPLETE", false);
isLevelNineteenComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_NINETEEN_COMPLETE", false);
isLevelTwentyComplete = prefs.getBoolean("SEVEN_BY_SEVEN_LEVEL_TWENTY_COMPLETE", false);
}
if(whichPage == 3) {
whichClass = NineByNineGame.class;
box.setText("9x9 Games");
box.setTextColor(Color.parseColor("#ffff4179"));
isLevelOneComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_ONE_COMPLETE", false);
isLevelTwoComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_TWO_COMPLETE", false);
isLevelThreeComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_THREE_COMPLETE", false);
isLevelFourComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_FOUR_COMPLETE", false);
isLevelFiveComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_FIVE_COMPLETE", false);
isLevelSixComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_SIX_COMPLETE", false);
isLevelSevenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_SEVEN_COMPLETE", false);
isLevelEightComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_EIGHT_COMPLETE", false);
isLevelNineComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_NINE_COMPLETE", false);
isLevelTenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_TEN_COMPLETE", false);
isLevelElevenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_ELEVEN_COMPLETE", false);
isLevelTwelveComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_TWELVE_COMPLETE", false);
isLevelThirteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_THIRTEEN_COMPLETE", false);
isLevelFourteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_FOURTEEN_COMPLETE", false);
isLevelFifteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_FIFTEEN_COMPLETE", false);
isLevelSixteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_SIXTEEN_COMPLETE", false);
isLevelSeventeenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_SEVENTEEN_COMPLETE", false);
isLevelEighteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_EIGHTEEN_COMPLETE", false);
isLevelNineteenComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_NINETEEN_COMPLETE", false);
isLevelTwentyComplete = prefs.getBoolean("NINE_BY_NINE_LEVEL_TWENTY_COMPLETE", false);
}
Button levelOne = (Button)findViewById(R.id.level_one);
Button levelTwo = (Button)findViewById(R.id.level_two);
Button levelThree = (Button)findViewById(R.id.level_three);
Button levelFour = (Button)findViewById(R.id.level_four);
Button levelFive = (Button)findViewById(R.id.level_five);
Button levelSix = (Button)findViewById(R.id.level_six);
Button levelSeven = (Button)findViewById(R.id.level_seven);
Button levelEight = (Button)findViewById(R.id.level_eight);
Button levelNine = (Button)findViewById(R.id.level_nine);
Button levelTen = (Button)findViewById(R.id.level_ten);
Button levelEleven = (Button)findViewById(R.id.level_eleven);
Button levelTwelve = (Button)findViewById(R.id.level_twelve);
Button levelThirteen = (Button)findViewById(R.id.level_thirteen);
Button levelFourteen = (Button)findViewById(R.id.level_fourteen);
Button levelFifteen = (Button)findViewById(R.id.level_fifteen);
Button levelSixteen = (Button)findViewById(R.id.level_sixteen);
Button levelSeventeen = (Button)findViewById(R.id.level_seventeen);
Button levelEighteen = (Button)findViewById(R.id.level_eighteen);
Button levelNineteen = (Button)findViewById(R.id.level_nineteen);
Button levelTwenty = (Button)findViewById(R.id.level_twenty);
levelOne.setBackgroundResource(R.drawable.bluespace);
if (isLevelOneComplete) {
levelTwo.setBackgroundResource(R.drawable.bluespace);
} else {
levelTwo.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelTwoComplete) {
levelThree.setBackgroundResource(R.drawable.bluespace);
} else {
levelThree.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelThreeComplete) {
levelFour.setBackgroundResource(R.drawable.bluespace);
} else {
levelFour.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelFourComplete) {
levelFive.setBackgroundResource(R.drawable.bluespace);
} else {
levelFive.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelFiveComplete) {
levelSix.setBackgroundResource(R.drawable.bluespace);
} else {
levelSix.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelSixComplete) {
levelSeven.setBackgroundResource(R.drawable.bluespace);
} else {
levelSeven.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelSevenComplete) {
levelEight.setBackgroundResource(R.drawable.bluespace);
} else {
levelEight.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelEightComplete) {
levelNine.setBackgroundResource(R.drawable.bluespace);
} else {
levelNine.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelNineComplete) {
levelTen.setBackgroundResource(R.drawable.bluespace);
} else {
levelTen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelTenComplete) {
levelEleven.setBackgroundResource(R.drawable.bluespace);
} else {
levelEleven.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelElevenComplete) {
levelTwelve.setBackgroundResource(R.drawable.bluespace);
} else {
levelTwelve.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelTwelveComplete) {
levelThirteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelThirteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelThirteenComplete) {
levelFourteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelFourteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelFourteenComplete) {
levelFifteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelFifteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelFifteenComplete) {
levelSixteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelSixteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelSixteenComplete) {
levelSeventeen.setBackgroundResource(R.drawable.bluespace);
} else {
levelSeventeen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelSeventeenComplete) {
levelEighteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelEighteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelEighteenComplete) {
levelNineteen.setBackgroundResource(R.drawable.bluespace);
} else {
levelNineteen.setBackgroundResource(R.drawable.lockedlevel);
}
if (isLevelNineteenComplete) {
levelTwenty.setBackgroundResource(R.drawable.bluespace);
} else {
levelTwenty.setBackgroundResource(R.drawable.lockedlevel);
}
Button backButton = (Button)findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
levelOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gotoLevelOne = new Intent(ctx, whichClass);
gotoLevelOne.putExtra("WHICH_LEVEL", 1);
startActivity(gotoLevelOne);
finish();
}
});
levelTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelOneComplete) {
Intent gotoLevelTwo = new Intent(ctx, whichClass);
gotoLevelTwo.putExtra("WHICH_LEVEL", 2);
startActivity(gotoLevelTwo);
finish();
}
}
});
levelThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelTwoComplete) {
Intent gotoLevelThree = new Intent(ctx, whichClass);
gotoLevelThree.putExtra("WHICH_LEVEL", 3);
startActivity(gotoLevelThree);
finish();
}
}
});
levelFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelThreeComplete) {
Intent gotoLevelFour = new Intent(ctx, whichClass);
gotoLevelFour.putExtra("WHICH_LEVEL", 4);
startActivity(gotoLevelFour);
finish();
}
}
});
levelFive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelFourComplete) {
Intent gotoLevelFive = new Intent(ctx, whichClass);
gotoLevelFive.putExtra("WHICH_LEVEL", 5);
startActivity(gotoLevelFive);
finish();
}
}
});
levelSix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelFiveComplete) {
Intent gotoLevelSix = new Intent(ctx, whichClass);
gotoLevelSix.putExtra("WHICH_LEVEL", 6);
startActivity(gotoLevelSix);
finish();
}
}
});
levelSeven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelSixComplete) {
Intent gotoLevelSeven = new Intent(ctx, whichClass);
gotoLevelSeven.putExtra("WHICH_LEVEL", 7);
startActivity(gotoLevelSeven);
finish();
}
}
});
levelEight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelSevenComplete) {
Intent gotoLevelEight = new Intent(ctx, whichClass);
gotoLevelEight.putExtra("WHICH_LEVEL", 8);
startActivity(gotoLevelEight);
finish();
}
}
});
levelNine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelEightComplete) {
Intent gotoLevelNine = new Intent(ctx, whichClass);
gotoLevelNine.putExtra("WHICH_LEVEL", 9);
startActivity(gotoLevelNine);
finish();
}
}
});
levelTen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelNineComplete) {
Intent gotoLevelTen = new Intent(ctx, whichClass);
gotoLevelTen.putExtra("WHICH_LEVEL", 10);
startActivity(gotoLevelTen);
finish();
}
}
});
levelEleven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelTenComplete) {
Intent gotoLevelEleven = new Intent(ctx, whichClass);
gotoLevelEleven.putExtra("WHICH_LEVEL", 11);
startActivity(gotoLevelEleven);
finish();
}
}
});
levelTwelve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelElevenComplete) {
Intent gotoLevelTwelve = new Intent(ctx, whichClass);
gotoLevelTwelve.putExtra("WHICH_LEVEL", 12);
startActivity(gotoLevelTwelve);
finish();
}
}
});
levelThirteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelTwelveComplete) {
Intent gotoLevelThirteen = new Intent(ctx, whichClass);
gotoLevelThirteen.putExtra("WHICH_LEVEL", 13);
startActivity(gotoLevelThirteen);
finish();
}
}
});
levelFourteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelThirteenComplete) {
Intent gotoLevelFourteen = new Intent(ctx, whichClass);
gotoLevelFourteen.putExtra("WHICH_LEVEL", 14);
startActivity(gotoLevelFourteen);
finish();
}
}
});
levelFifteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelFourteenComplete) {
Intent gotoLevelFifteen = new Intent(ctx, whichClass);
gotoLevelFifteen.putExtra("WHICH_LEVEL", 15);
startActivity(gotoLevelFifteen);
finish();
}
}
});
levelSixteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelFifteenComplete) {
Intent gotoLevelSixteen = new Intent(ctx, whichClass);
gotoLevelSixteen.putExtra("WHICH_LEVEL", 16);
startActivity(gotoLevelSixteen);
finish();
}
}
});
levelSeventeen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelSixteenComplete) {
Intent gotoLevelSeventeen = new Intent(ctx, whichClass);
gotoLevelSeventeen.putExtra("WHICH_LEVEL", 17);
startActivity(gotoLevelSeventeen);
finish();
}
}
});
levelEighteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelSeventeenComplete) {
Intent gotoLevelEighteen = new Intent(ctx, whichClass);
gotoLevelEighteen.putExtra("WHICH_LEVEL", 18);
startActivity(gotoLevelEighteen);
finish();
}
}
});
levelNineteen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelEighteenComplete) {
Intent gotoLevelNineteen = new Intent(ctx, whichClass);
gotoLevelNineteen.putExtra("WHICH_LEVEL", 19);
startActivity(gotoLevelNineteen);
finish();
}
}
});
levelTwenty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isLevelNineteenComplete) {
Intent gotoLevelTwenty = new Intent(ctx, whichClass);
gotoLevelTwenty.putExtra("WHICH_LEVEL", 20);
startActivity(gotoLevelTwenty);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_which_normal_level, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed(){}
}
关于它为什么停止播放音乐的任何想法?
谢谢:)
答案 0 :(得分:2)
您可以像这样创建MediaPlayer
:
MediaPlayer ourSong = MediaPlayer.create(MainActivity.this, R.raw.music);
您正在将MainActivity
的实例作为Context
参数传递。这会将MediaPlayer
生命周期与MainActivity
的生命周期相关联。
当你启动WhichNormalLevelActivity
时,就像这样:
public void fiveGames(View view){
Intent i = new Intent(this, WhichNormalLevelActivity.class);
i.putExtra("uh", 1);
startActivity(i);
finish();
}
您在finish()
上致电MainActivity
。 Android会清理并销毁与MainActivity
相关联的生命周期的所有内容,包括您的MediaPlayer
。
如果您希望MediaPlayer
生命周期独立于特定Activity
,请在创建Context
时将全局应用程序上下文作为MediaPlayer
参数传递。要获取全局上下文,请致电getApplicationContext()
。只是不要忘记关闭它并在完成后释放资源!
答案 1 :(得分:0)
这应该可以解决您的问题:
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.prepare();
mPlayer.start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mPlayer) {
mPlayer.release();
}
};