這裏的錯誤:MediaPlayer爲什麼會失敗?
09-12 22:13:05.253: ERROR/AudioFlinger(561): not enough memory for AudioTrack size=53560
09-12 22:13:05.253: ERROR/AudioTrack(561): AudioFlinger could not create track, status: -12
09-12 22:13:05.253: ERROR/AudioSink(561): Unable to create audio track
09-12 22:13:05.253: ERROR/audiothread(561): Error creating AudioTrack
09-12 22:13:05.253: ERROR/PlayerDriver(561): Command PLAYER_PREPARE completed with an error or info PVMFFailure
09-12 22:13:05.253: ERROR/MediaPlayer(2368): error (1, -1)
我做is--實例化一個活動基地內SoundFX對象(所有其他活動延長base--所以他們都共享音效和子菜單)是什麼。
這裏是我的SoundFX類:
public class SoundFX {
private SharedPreferences prefs = null;
private MediaPlayer[] sounds = null;
SoundFX(Context c){
ValidateObject.isNotNull(c, "Null context.");
prefs = PreferenceManager.getDefaultSharedPreferences(c);
ValidateObject.isNotNull(prefs, "Null preferences.");
sounds = new MediaPlayer[4];
sounds[0] = MediaPlayer.create(c, R.raw.snd_button_menu);
sounds[1] = MediaPlayer.create(c, R.raw.snd_button_direction);
sounds[2] = MediaPlayer.create(c, R.raw.snd_button_text);
sounds[3] = MediaPlayer.create(c, R.raw.snd_button_drill);
}
public void menu(){
if (prefs.getBoolean("SoundFX", true)){
sounds[0].start();
}
}
public void direction(){
if (prefs.getBoolean("SoundFX", true)){
sounds[1].start();
}
}
public void text(){
if (prefs.getBoolean("SoundFX", true)){
sounds[2].start();
}
}
public void drill(){
if (prefs.getBoolean("SoundFX", true)){
sounds[3].start();
}
}
}
這將是我的「基地」活動,所有活動擴展:
public class ActivityWithMenu extends Activity {
protected SoundFX soundEffects = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// no content view...
soundEffects = new SoundFX(this);
ValidateObject.isNotNull(soundEffects, "Null SoundFX Object.");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.submenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.submenu_hide:
moveTaskToBack(true);
return true;
case R.id.submenu_help:
startActivity(new Intent(this, HelpActivity.class));
return true;
case R.id.submenu_preferences:
startActivity(new Intent(this, Preferences.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
是每個活動創建一個新的實例,因此分配更多的內存?我似乎無法弄清楚爲什麼我會耗盡內存。我的印象是所有子活動 將使用相同的SoundFX對象下...
編輯:
錯誤來創建和銷燬大約10〜12後活動。它在那之前運作良好。
Hehe,先生。谷歌,你應該知道你自己的代碼。 –