我遇到一个问题,在程序中更改屏幕方向时无法保持音乐播放。我尝试在 AndroidManifest.xml 文件的 MainActivity 内添加 android:configChanges="keyboardHidden|orientation|screenSize
,但虽然它保持音乐播放,但它也禁用了横向模式的不同布局。
这是 MainActivity 的代码:
package com.example.gomoku;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up click listeners for all the buttons.
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View highScoreButton = findViewById(R.id.high_score_button);
highScoreButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.new_button:
startGame();
break;
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.main);
}
@Override
protected void onPause() {
super.onPause();
Music.stop(this);
}
private void startGame() {
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
}
}
这是音乐的代码:
package com.example.gomoku;
import android.content.Context;
import android.media.MediaPlayer;
public class Music {
private static MediaPlayer mp = null;
/** Stop old song and start new one. */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences.
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
/** Stop the music. */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
最佳答案
编辑原代码;
package com.example.gomoku;
import android.content.Context;
import android.media.MediaPlayer;
public class Music {
//####First change is here####
//adding a variable to store the time of music being played.
private static int pos = 0;
private static MediaPlayer mp = null;
/** Stop old song and start new one. */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences.
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
//####Second change is here####
//this will continue the music from wherever it was paused
mp.seekTo(pos);
}
}
/** Stop the music. */
public static void stop(Context context) {
if (mp != null) {
//####Third change is here####
mp.pause();//to pause the music
//to store current pause time in pos
//as pos is static it will retain the value
pos = mp.getCurrentPosition();
mp.stop();
mp.release();
mp = null;
}
}
}
添加此代码以在按下后退键时停止应用程序。
@Override
public void onBackPressed() {
pos = 0;
super.onBackPressed();
mp.release();
System.exit(0);
}
现在这个应用程序将:
- 开始时循环播放音乐。
- 屏幕方向改变时继续播放音乐。
- 按主页按钮后重新启动应用时恢复音乐。
- 如果按下后退按钮,则重新播放歌曲。
关于android - Android 中屏幕方向更改时继续播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300389/