Android:如何循环播放音效?

发布于 2024-10-19 21:47:57 字数 3997 浏览 0 评论 0原文

我为一个按钮分配了两个音效。我想知道在播放最后一个音效后如何循环回到第一个音效。

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
mSoundManager.addSound(3, R.raw.lsp_dontlumpingyellatme);
mSoundManager.addSound(4, R.raw.pb_shouldntpunchbrainiacs);
mSoundManager.addSound(5, R.raw.iceking_imabanana);
mSoundManager.addSound(5, R.raw.finn_wordtoyourmother);

final Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        mSoundManager.playSound(1, 0);
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mSoundManager.playSound(6, 0);


    }

});
    }
});

SoundManger.java

package com.andrew.finnandjake;

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;

SoundManager() {
}

/**
 * Requests the instance of the Sound Manager and creates it if it does not
 * exist.
 * 
 * @return Returns the single instance of the SoundManager
 */
static synchronized public SoundManager getInstance() {
    if (_instance == null)
        _instance = new SoundManager();
    return _instance;
}

/**
 * Initialises the storage for the sounds
 * 
 * @param theContext
 *            The Application context
 */
public static void initSounds(Context theContext) {
    mContext = theContext;
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    mSoundPoolMap = new HashMap();
    mAudioManager = (AudioManager) mContext
            .getSystemService(Context.AUDIO_SERVICE);
}

/**
 * Add a new Sound to the SoundPool
 * 
 * @param Index
 *            - The Sound Index for Retrieval
 * @param SoundID
 *            - The Android ID for the Sound asset.
 */
public static void addSound(int Index, int SoundID) {
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}

/**
 * Loads the various sound assets Currently hardcoded but could easily be
 * changed to be flexible.
 */
public static void loadSounds() {
    mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.finn_whatthejugisthat, 1));
    mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.jake_dancingwithbabes, 1));
    mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.lsp_dontlumpingyellatme, 1));
    mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.pb_shouldntpunchbrainiacs, 1));
    mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.iceking_imabanana, 1));
}


/**
 * Plays a Sound
 * 
 * @param index
 *            - The Index of the Sound to be played
 * @param speed
 *            - The Speed to play not, not currently used but included for
 *            compatibility
 */
public static void playSound(int index, float speed) {
    float streamVolume = mAudioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume
            / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume,
            1, 0, speed);
}

/**
 * Stop a Sound
 * 
 * @param index
 *            - index of the sound to be stopped
 */
public static void stopSound(int index) {
    mSoundPool.stop((Integer) mSoundPoolMap.get(index));
}

/**
 * Deallocates the resources and Instance of SoundManager
 */
public static void cleanup() {
    mSoundPool.release();
    mSoundPool = null;
    mSoundPoolMap.clear();
    mAudioManager.unloadSoundEffects();
    _instance = null;

}

public int getNumberOfSounds() {
    // TODO Auto-generated method stub
    return 0;
}

}

I have two sound effects assigned to a button. I'm wondering how I can loop back to the first sound effect, after the last one is played.

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
mSoundManager.addSound(3, R.raw.lsp_dontlumpingyellatme);
mSoundManager.addSound(4, R.raw.pb_shouldntpunchbrainiacs);
mSoundManager.addSound(5, R.raw.iceking_imabanana);
mSoundManager.addSound(5, R.raw.finn_wordtoyourmother);

final Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        mSoundManager.playSound(1, 0);
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mSoundManager.playSound(6, 0);


    }

});
    }
});

SoundManger.java

package com.andrew.finnandjake;

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;

SoundManager() {
}

/**
 * Requests the instance of the Sound Manager and creates it if it does not
 * exist.
 * 
 * @return Returns the single instance of the SoundManager
 */
static synchronized public SoundManager getInstance() {
    if (_instance == null)
        _instance = new SoundManager();
    return _instance;
}

/**
 * Initialises the storage for the sounds
 * 
 * @param theContext
 *            The Application context
 */
public static void initSounds(Context theContext) {
    mContext = theContext;
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    mSoundPoolMap = new HashMap();
    mAudioManager = (AudioManager) mContext
            .getSystemService(Context.AUDIO_SERVICE);
}

/**
 * Add a new Sound to the SoundPool
 * 
 * @param Index
 *            - The Sound Index for Retrieval
 * @param SoundID
 *            - The Android ID for the Sound asset.
 */
public static void addSound(int Index, int SoundID) {
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}

/**
 * Loads the various sound assets Currently hardcoded but could easily be
 * changed to be flexible.
 */
public static void loadSounds() {
    mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.finn_whatthejugisthat, 1));
    mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.jake_dancingwithbabes, 1));
    mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.lsp_dontlumpingyellatme, 1));
    mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.pb_shouldntpunchbrainiacs, 1));
    mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.iceking_imabanana, 1));
}


/**
 * Plays a Sound
 * 
 * @param index
 *            - The Index of the Sound to be played
 * @param speed
 *            - The Speed to play not, not currently used but included for
 *            compatibility
 */
public static void playSound(int index, float speed) {
    float streamVolume = mAudioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume
            / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume,
            1, 0, speed);
}

/**
 * Stop a Sound
 * 
 * @param index
 *            - index of the sound to be stopped
 */
public static void stopSound(int index) {
    mSoundPool.stop((Integer) mSoundPoolMap.get(index));
}

/**
 * Deallocates the resources and Instance of SoundManager
 */
public static void cleanup() {
    mSoundPool.release();
    mSoundPool = null;
    mSoundPoolMap.clear();
    mAudioManager.unloadSoundEffects();
    _instance = null;

}

public int getNumberOfSounds() {
    // TODO Auto-generated method stub
    return 0;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2024-10-26 21:47:57

嗯......

final Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
    int currentSound = 0;
    public void onClick(View v) {
        mSoundManager.playSound(currentSound, 0);
        currentSound++;
        currentSound %= mSoundmanager.getNumberOfSounds();
    }
});

如果我理解你的代码正确的话,你想每次按下按钮时播放不同的声音。

Ehhmm...

final Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
    int currentSound = 0;
    public void onClick(View v) {
        mSoundManager.playSound(currentSound, 0);
        currentSound++;
        currentSound %= mSoundmanager.getNumberOfSounds();
    }
});

If I understand your code right, you want to play a different sound each time the button is pressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文