等待的Soundpool到移动与应用程序之前加载 [英] Waiting for SoundPool to load before moving on with application
问题描述
我在下面的教程来的Soundpool融入我的应用程序,这是code这是在本教程给出:
I was following a tutorial to integrate SoundPool into my app, and this is the code which was given in the tutorial:
package com.example.soundpoolexample;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class SoundPoolExample extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.textView1);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
}
现在的问题是,我要确保应用程序只向前移动一次的Soundpool加载成功,并没有检查它是否在每次播放声音之前加载。我将如何做到这一点?
The problem is, I want to make sure that the application only moves forward once the SoundPool is loaded successfully, and not have to check if it is loaded before playing a sound each time. How would I do this?
我相信这是一个穷凶极恶的解决方案,但会像这样的工作后,我加载的onCreate声音文件:?
I am sure this is an atrocious solution, but would something like this work after I load the sound file in onCreate?:
...
soundID = soundPool.load(this, R.raw.sound1, 1);
while (!loaded) {}
...
我相信有一个更好的办法。
I am sure there is a better way.
推荐答案
您可以有一个完美的解决方案使用旗语
,但尝试的避免的堵的onCreate()
,因为应用程序将不会响应,在code在这里只是为了说明使用旗语$ C的$ C>
You can have an elegant solution using Semaphore
, however try to avoid blocking onCreate()
, since the app will not be responsive, the code here is just to illustrate the use of Semaphore
public class SoundPoolExample extends Activity implements OnTouchListener {
public static final Semaphore semaphore = new Semaphore(0);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
semaphore.release();
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);
// Will block since there is not permits (the semaphore is initialized with 0)
// then will continue once semaphore.release(); is called i.e. after loading is finished
semaphore.acquire();
}
// rest of your class
}
这篇关于等待的Soundpool到移动与应用程序之前加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!