本文介绍了播放多个声音,同时在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用下面的方法code到播放多种音/同时发出蜂鸣声。在我的onclicklistener我加...公共无效的onClick(视图v){mSoundManager.playSound(1); mSoundManager.playSound(2); } ...但是这个只播放一个声音时,声音与指数1后面的声音,索引2

I am unable to use the following to code to play multiple sounds/beeps simultaneously. In my onclicklistener I have added ... public void onClick(View v) { mSoundManager.playSound(1); mSoundManager.playSound(2); } ... But this plays only one sound at a time, sound with index 1 followed by sound with index 2.

我怎么能玩至少有2个声音同时使用这code,每当有一个onClick()事件?

How can I play atleast 2 sounds simultaneously using this code whenever there is an onClick() event?

public class SoundManager {

private  SoundPool mSoundPool;
private  HashMap<Integer, Integer> mSoundPoolMap;
private  AudioManager  mAudioManager;
private  Context mContext;


public SoundManager()
{

}

public void initSounds(Context theContext) {
     mContext = theContext;
     mSoundPool = new SoundPool(4, 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(1, mSoundPool.load(mContext, SoundID, 1));
}

public void playSound(int index) {

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

public void playLoopedSound(int index) {

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

}

推荐答案

具有下列类采用声池播放声音效果:

Having the following class to play sound fx using sound pool:

public class SoundManager {

    public static int SOUNDPOOLSND_MENU_BTN         = 0;
    public static int SOUNDPOOLSND_WIN              = 1;
    public static int SOUNDPOOLSND_LOOSE            = 2;
    public static int SOUNDPOOLSND_DRAW             = 3;
    public static int SOUNDPOOLSND_TICK1            = 4;
    public static int SOUNDPOOLSND_TICK2            = 5;
    public static int SOUNDPOOLSND_OUT_OF_TIME      = 6;
    public static int SOUNDPOOLSND_HISCORE          = 7;
    public static int SOUNDPOOLSND_CORRECT_LETTER   = 8;

    public static boolean isSoundTurnedOff;

    private static SoundManager mSoundManager;

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

    public static final int maxSounds = 4;

    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);

//        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
//            public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
//               loaded = true;
//            }
//        });

        mSoundPoolMap = new SparseArray<Integer>();
        mSoundPoolMap.put(SOUNDPOOLSND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_WIN, mSoundPool.load(mContext, R.raw.win, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_CORRECT_LETTER, mSoundPool.load(mContext, R.raw.correct_letter, 1));

        // testing simultaneous playing
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mSoundPool.play(mSoundPoolMap.get(0), streamVolume, streamVolume, 1, 20, 1f);
        mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 2, 1f);
        mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);


    }

    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;
    }
}

它并同时播放的评论测试过程中同步播放下面提到3声音。好像你需要实现我的code听众评论( OnLoadCompleteListener )。它可能发生的系统只是没有加载声音自动柜员机开始播放它。如果你不想要实现上述监听器试图等待一段时间你的演奏听起来活动开始,并开始了一段时间后播放声音后。

it DOES play simultaneously 3 sounds mentioned below the comment "testing simultaneous playing". It seems like you need to implement commented in my code listener (OnLoadCompleteListener). It could happen that system just didn't load sounds atm you start playing it. If you dont want to implement mentioned listener try to wait some time after your playing sounds activity started and start playing sounds after a while.

这篇关于播放多个声音,同时在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:00