gpt4 book ai didi

android - 无法循环播放声音-android

转载 作者:行者123 更新时间:2023-12-03 02:15:00 25 4
gpt4 key购买 nike

当我播放声音不是循环播放时,它可以正常工作,但是当我尝试循环播放声音或多次播放时,它根本无法播放。

我的声音课:

public class GameSounds {
private SoundPool soundPool;
private HashMap soundPoolHashMap;
Context mContext;

public GameSounds() {
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
soundPoolHashMap = new HashMap();
}

/**
*
* @param index is the ID *we* choose for this sound
* @param soundID is the id of the sound file in our resurce
* @param context - our resurce is in this context
*/
public void addSound(int index, int soundID, Context context) {
//we call the "load" function in order to convert the sound in the soundID to raw
//and to insert the ID that return from "load" to soundPoolID
int soundPoolID = soundPool.load(context, soundID, 1);

mContext = context;
//insert new value to the hash
soundPoolHashMap.put(index, soundPoolID);
}

/**
*
* @param index - the sound code we want to sound
* @param loop - whether to sound this sound in infintly loop
*/
public void play(int index, boolean loop) {
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(mContext);

if(preferences.getBoolean("isMuted", false)){
if (!loop)
//the "1" is for highest volum
soundPool.play((Integer) soundPoolHashMap.get(index), 1, 1, 1, 0, 1f);
else
soundPool.play((Integer) soundPoolHashMap.get(index), 1, 1, 1, -1, 1f); //even when I write 10 instead of -1 , it is not do anything...
}
}
public void stop(int index) {
soundPool.stop((Integer) soundPoolHashMap.get(index));
}

public void release() {
soundPool.release();
}
}

现在,这就是我播放声音的方式:
    gameSounds.play(this.gameLevel.music,false); //work good - one time
gameSounds.play(this.gameLevel.music,true); //not work at all

可能是什么原因?谢谢!

最佳答案

在我的类(class)上尝试一下-对我来说效果很好:

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseArray;

public class SoundManager {

public static int SND_MENU_BTN = 0;
public static int SND_WIN = 1;
public static int SND_LOOSE = 2;
public static int SND_DRAW = 3;
public static int SND_TICK1 = 4;
public static int SND_TICK2 = 5;
public static int SND_OUT_OF_TIME = 6;
public static int SND_HISCORE = 7;

public static boolean isSoundTurnedOff;

private static SoundManager mSoundManager;

private SoundPool mSoundPool;
private SparseArray <Integer> mSoundPoolMap;
private AudioManager mAudioManager;

public static final int maxSounds = 3; //an object to change if need

public static SoundManager getInstance(Context context)
{
if (mSoundManager == null){
mSoundManager = new SoundManager(context);
}

return mSoundManager;
}

public SoundManager(Context mContext)
{
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0);

//actually the following preloads all the sounds
mSoundPoolMap = new SparseArray<Integer>();
mSoundPoolMap.put(SND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1));
mSoundPoolMap.put(SND_WIN, mSoundPool.load(mContext, R.raw.win, 1));
mSoundPoolMap.put(SND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1));
mSoundPoolMap.put(SND_DRAW, mSoundPool.load(mContext, R.raw.hangmanbodydrawing, 1));
mSoundPoolMap.put(SND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1));
mSoundPoolMap.put(SND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1));
mSoundPoolMap.put(SND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1));
mSoundPoolMap.put(SND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1));

}


public void playSound(int index) {
if (isSoundTurnedOff)
return;

int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

public static void clear()
{
if (mSoundManager != null){
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}
}

显然,要播放某些已定义的声音,您需要编写类似的内容(如果在 Activity 中):
SoundManager.getInstance(this).playSound(SoundManager.SND_TICK1);

但是,在我的项目中,我只调用一次 SoundManager.getInstance(this)来初始化它,然后在每次 Activity 中,我都持有指向SoundManager的链接以缩短用法,例如:
soundManager.playSound(SoundManager.SND_TICK1);

关于android - 无法循环播放声音-android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com