Android MediaPlayer 背景音乐线程随机停止
Android MediaPlayer background music thread randomly stops
我玩的时候只有一个 activity 游戏(登录和角色选择后)。我的 SFX 声音池效果很好,但我的背景音乐随机停止播放。我试图添加 setOnErrorListener,但从未在那里看到任何东西。我想知道线程是否正在被垃圾收集?
当您在不同的城镇或荒野时,音乐会发生变化,请在此处进行检查:!currentPlayingMusicFilename.equals(shortFilename)。如果您停留在同一个音乐区域,音乐会随机停止循环。
我在这里和 google 上看了很多帖子,但找不到 "proper" 播放游戏背景音乐的方法。我试过 soundpool,但它们超过 1MB,看到很多东西说不要做服务,并且对这种方法有疑问。非常感谢任何帮助。
我将离开 "SFX" 部分,以防代码可以帮助任何人并提供完整的图片。
public static void playSoundOrMusic(final String shortFilename, final String type, double distanceFactor) {
String fullFilename = "";
if (type.equals("SFX")){
fullFilename = "res/sounds/sfx/" + shortFilename;
} else if (type.equals("MUSIC")){
if (mp3MUSICPlayer != null && mp3MUSICPlayer.isPlaying() && !currentPlayingMusicFilename.equals(shortFilename)){
mp3MUSICPlayer.stop();
}
fullFilename = "res/sounds/music/" + shortFilename;
}
float volumeManipulation = 1.0f;
if (type.equals("SFX")){
int sfxVolume = MyCommandReceiver.GetSharedPreferences().getInt(MyCommandReceiver.GetStringById(R.string.pref_general_sfx_volume_key), 100);
sfxVolume *= distanceFactor;
volumeManipulation = (float) (sfxVolume / 100.0);
//volumeManipulation = (float) (1 - (Math.log(MAX_VOLUME - sfxVolume) / Math.log(MAX_VOLUME)));
LoggerWrite("v", TAG, "sfxVolume: " + volumeManipulation);
} else if (type.equals("MUSIC")){
int musicVolume = MyCommandReceiver.GetSharedPreferences().getInt(MyCommandReceiver.GetStringById(R.string.pref_general_music_volume_key), 100);
volumeManipulation = (float) (musicVolume / 100.0);
//volumeManipulation = (float) (1 - (Math.log(MAX_VOLUME - musicVolume) / Math.log(MAX_VOLUME)));
LoggerWrite("v", TAG, "musicVolume: " + volumeManipulation);
}
final float finalVolume = volumeManipulation;
if (MyCommandReceiver.GetActiveActivity() == null){ //if not yet in Activity
return;
}
try {
Uri myUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename));
if (type.equals("SFX")){
if (!soundPoolIds.containsKey(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename)){ //not yet in soundpool
int soundId = soundPool.load(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename, 1);
soundPoolIds.put(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename, soundId);
//play it manually one time
mp3SFXPlayer = new MediaPlayer();
mp3SFXPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp3SFXPlayer.setVolume(finalVolume, finalVolume);
mp3SFXPlayer.setDataSource(MyCommandReceiver.GetActiveActivity(), myUri);
new Thread(new Runnable() {
@Override
public void run() {
try {
mp3SFXPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp3SFXPlayer.start();
}
});
mp3SFXPlayer.prepare();
} catch (Exception ex) {
GameActivity.LoggerWrite("e", TAG, "Sound(sfx) playing issue" + ex);
}
}
}).start();
} else { //already in soundpool - play it
soundPool.play(soundPoolIds.get(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename), finalVolume, finalVolume, 0, 0, 1);
}
} else if (type.equals("MUSIC")){
if (!currentPlayingMusicFilename.equals(shortFilename)){
mp3MUSICPlayer = new MediaPlayer();
mp3MUSICPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp3MUSICPlayer.setVolume(finalVolume, finalVolume);
mp3MUSICPlayer.setDataSource(MyCommandReceiver.GetActiveActivity(), myUri);
}
}
} catch (Exception e) {
GameActivity.LoggerWrite("e", TAG, "Sound file issue" + e);
}
if (type.equals("MUSIC") && !currentPlayingMusicFilename.equals(shortFilename)){
new Thread(new Runnable() {
@Override
public void run() {
try {
mp3MUSICPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
currentPlayingMusicFilename = shortFilename;
mp3MUSICPlayer.start();
}
});
mp3MUSICPlayer.prepare();
mp3MUSICPlayer.setLooping(true);
} catch (Exception ex) {
GameActivity.LoggerWrite("e", TAG, "Sound(music) playing issue" + ex);
}
}
}).start();
}
}
这是我解决这个问题的方法,因为似乎没有关于它随机死亡原因的答案。
if (type.equals("MUSIC") && (!currentPlayingMusicFilename.equals(shortFilename) || !mp3MUSICPlayer.isPlaying())){
new Thread(new Runnable() {
...
我玩的时候只有一个 activity 游戏(登录和角色选择后)。我的 SFX 声音池效果很好,但我的背景音乐随机停止播放。我试图添加 setOnErrorListener,但从未在那里看到任何东西。我想知道线程是否正在被垃圾收集?
当您在不同的城镇或荒野时,音乐会发生变化,请在此处进行检查:!currentPlayingMusicFilename.equals(shortFilename)。如果您停留在同一个音乐区域,音乐会随机停止循环。
我在这里和 google 上看了很多帖子,但找不到 "proper" 播放游戏背景音乐的方法。我试过 soundpool,但它们超过 1MB,看到很多东西说不要做服务,并且对这种方法有疑问。非常感谢任何帮助。
我将离开 "SFX" 部分,以防代码可以帮助任何人并提供完整的图片。
public static void playSoundOrMusic(final String shortFilename, final String type, double distanceFactor) {
String fullFilename = "";
if (type.equals("SFX")){
fullFilename = "res/sounds/sfx/" + shortFilename;
} else if (type.equals("MUSIC")){
if (mp3MUSICPlayer != null && mp3MUSICPlayer.isPlaying() && !currentPlayingMusicFilename.equals(shortFilename)){
mp3MUSICPlayer.stop();
}
fullFilename = "res/sounds/music/" + shortFilename;
}
float volumeManipulation = 1.0f;
if (type.equals("SFX")){
int sfxVolume = MyCommandReceiver.GetSharedPreferences().getInt(MyCommandReceiver.GetStringById(R.string.pref_general_sfx_volume_key), 100);
sfxVolume *= distanceFactor;
volumeManipulation = (float) (sfxVolume / 100.0);
//volumeManipulation = (float) (1 - (Math.log(MAX_VOLUME - sfxVolume) / Math.log(MAX_VOLUME)));
LoggerWrite("v", TAG, "sfxVolume: " + volumeManipulation);
} else if (type.equals("MUSIC")){
int musicVolume = MyCommandReceiver.GetSharedPreferences().getInt(MyCommandReceiver.GetStringById(R.string.pref_general_music_volume_key), 100);
volumeManipulation = (float) (musicVolume / 100.0);
//volumeManipulation = (float) (1 - (Math.log(MAX_VOLUME - musicVolume) / Math.log(MAX_VOLUME)));
LoggerWrite("v", TAG, "musicVolume: " + volumeManipulation);
}
final float finalVolume = volumeManipulation;
if (MyCommandReceiver.GetActiveActivity() == null){ //if not yet in Activity
return;
}
try {
Uri myUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename));
if (type.equals("SFX")){
if (!soundPoolIds.containsKey(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename)){ //not yet in soundpool
int soundId = soundPool.load(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename, 1);
soundPoolIds.put(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename, soundId);
//play it manually one time
mp3SFXPlayer = new MediaPlayer();
mp3SFXPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp3SFXPlayer.setVolume(finalVolume, finalVolume);
mp3SFXPlayer.setDataSource(MyCommandReceiver.GetActiveActivity(), myUri);
new Thread(new Runnable() {
@Override
public void run() {
try {
mp3SFXPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp3SFXPlayer.start();
}
});
mp3SFXPlayer.prepare();
} catch (Exception ex) {
GameActivity.LoggerWrite("e", TAG, "Sound(sfx) playing issue" + ex);
}
}
}).start();
} else { //already in soundpool - play it
soundPool.play(soundPoolIds.get(Environment.getExternalStorageDirectory() + "/KisnardOnline/" + fullFilename), finalVolume, finalVolume, 0, 0, 1);
}
} else if (type.equals("MUSIC")){
if (!currentPlayingMusicFilename.equals(shortFilename)){
mp3MUSICPlayer = new MediaPlayer();
mp3MUSICPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp3MUSICPlayer.setVolume(finalVolume, finalVolume);
mp3MUSICPlayer.setDataSource(MyCommandReceiver.GetActiveActivity(), myUri);
}
}
} catch (Exception e) {
GameActivity.LoggerWrite("e", TAG, "Sound file issue" + e);
}
if (type.equals("MUSIC") && !currentPlayingMusicFilename.equals(shortFilename)){
new Thread(new Runnable() {
@Override
public void run() {
try {
mp3MUSICPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
currentPlayingMusicFilename = shortFilename;
mp3MUSICPlayer.start();
}
});
mp3MUSICPlayer.prepare();
mp3MUSICPlayer.setLooping(true);
} catch (Exception ex) {
GameActivity.LoggerWrite("e", TAG, "Sound(music) playing issue" + ex);
}
}
}).start();
}
}
这是我解决这个问题的方法,因为似乎没有关于它随机死亡原因的答案。
if (type.equals("MUSIC") && (!currentPlayingMusicFilename.equals(shortFilename) || !mp3MUSICPlayer.isPlaying())){
new Thread(new Runnable() {
...