这是我的代码:
在 onCreate 里面:
在 onCreate 里面:
new LoadMusicInBackground().execute();
然后,在我的主类末尾我有这段代码
/** Helper class to load all the music in the background. */
class LoadMusicInBackground extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(A1,
soundPool.load(GameScreen_bugfix.this, R.raw.a, 1));
soundPoolMap.put(A3,
soundPool.load(GameScreen_bugfix.this, R.raw.b, 1));
soundPoolMap.put(A5,
soundPool.load(GameScreen_bugfix.this, R.raw.c_s, 1));
soundPoolMap.put(A6,
soundPool.load(GameScreen_bugfix.this, R.raw.d, 1));
soundPoolMap.put(A8,
soundPool.load(GameScreen_bugfix.this, R.raw.e, 1));
soundPoolMap.put(A10,
soundPool.load(GameScreen_bugfix.this, R.raw.f_s, 1));
soundPoolMap.put(A12,
soundPool.load(GameScreen_bugfix.this, R.raw.g_s, 1));
soundPoolMap.put(wrong,
soundPool.load(GameScreen_bugfix.this, R.raw.wrong2, 1));
publishProgress("");
Log.v("SOUNDPOOL", "" + soundPoolMap);
return (null);
}
@Override
protected void onProgressUpdate(String... item) {
// text1.setText(item[0]);
}
@Override
protected void onPostExecute(Void unused) {
//Toast.makeText(GameScreen_bugfix.this, "music loaded!", Toast.LENGTH_SHORT).show();
}
}
如果音乐没有加载,我会得到一个空指针异常。查看文档,我发现有一个getStatus()方法,但我尝试过类似这样的代码:
music_load_status=LoadMusicInBackground.getStatus()
但是那并没有起作用 :(
我如何检查后台任务是否完成并且音乐已经加载?
谢谢!
Ryan