[英]Passing soundpool to multiple activities in Android
Basically, in a short explanation, I am making a sound board. 简而言之,我基本上是制作一个共鸣板。 Once I run the initial activity, it adds all sound clips to the soundpool. 运行初始活动后,它将所有声音片段添加到声音池中。 I then have two other activities that are to organize the sound clips, and I would like to use the previously loaded soundpool so that there isn't move load time when switching between activities. 然后,我还有另外两个组织声音片段的活动,我想使用以前加载的声音池,以便在活动之间进行切换时不会移动加载时间。 I am fairly new to this Android coding, so please make things in simple terms! 对于这种Android编码,我还很陌生,所以请简单说明一下!
EDIT: Also, does anybody know how to stop the playback on a second button click? 编辑:此外,有人知道如何在第二个按钮单击时停止播放吗? Not as in clicking a different button, I understand that, but if the same button is clicked, it will stop it? 我不像单击其他按钮那样理解,但是如果单击同一按钮,它将停止它吗?
My main activity: 我的主要活动:
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0, R.raw.stop_playing);
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
Dealing with soundpool stuff: 处理声音池的东西:
public void initSounds(Context theContext) { mContext = theContext; mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); mSoundPoolMap = new HashMap<Integer, Integer>(); mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void pauseSound(){
mSoundPool.autoPause();
}
public void stopSound(){
mSoundPool.stop(playingNumber);
}
public int playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int soundId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
playingNumber = index;
return playingNumber;
}
} }
Alwade create Application class, and create static SoundManager instance in the your application class, and add needed sounds to SoundManager in the Initial activity. Alwade创建Application类,并在您的应用程序类中创建静态SoundManager实例,并在Initial活动中向SoundManager添加所需的声音。 Then you can use current Soundmanager in other activities. 然后,您可以在其他活动中使用当前的Soundmanager。 Change SoundManager class to static class. 将SoundManager类更改为静态类。
For example: 例如:
public class MyApplication extends Application {
public static SoundManager soundManager;
public static SoundManager getSoundManager() {
return soundManager;
}
public static void setSoundManager(SoundManager soundManagerIns) {
soundManager = soundManagerIns;
}
}
and in the initial activity create instance and set it to : 并在初始活动中创建实例并将其设置为:
mSoundManager = new SoundManager();
mSoundManager.initSounds(getApplicationContext);
mSoundManager.addSound(0, R.raw.stop_playing);
mSoundManager.addSound(1, R.raw.choo_choo);
mSoundManager.addSound(2, R.raw.all);
mSoundManager.addSound(3, R.raw.hearts);
MyApplication.setSoundManager(mSoundManager);
Then you can get this SoundManager in the other activities: 然后,您可以在其他活动中获得此SoundManager:
SoundManager sManager = MyApplication.getSoundManager(); SoundManager sManager = MyApplication.getSoundManager();
This is my example project: SoundPoolExample 这是我的示例项目: SoundPoolExample
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.