这是错误:
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)
我正在做的是 - 在基础Activity中实例化一个SoundFX对象(所有其他活动扩展基础 - 所以它们都共享声音效果和子菜单)。
这是我的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个活动之后出现错误。在此之前它运作良好。
答案 0 :(得分:1)
在销毁课程之前你需要release()
媒体播放器(可能在你活动的onDestroy
中),否则资源不会被释放(duh)而你很快就会失去记忆(因为你)下次再分配它们。)
如果您在许多活动中使用这些声音,您可以创建单一声音,并避免每次切换活动时都创建它们。