java - 从媒体播放器更改为声音池

标签 java android eclipse

我有一些代码设置,可以使用 android.media.MediaPlayer 对象在我的 /raw/ 文件夹中依次播放 3 个声音文件。每次调用 MediaPlayer 来播放时,会随机选择 3 个声音中的两个。

但是,我意识到这确实不是最好的方法,因为声音剪辑太短了。我对 android.media.SoundPool 进行了一些研究。但是我不太确定如何合并 SoundPool 而不是使用 MediaPlayer。这是我目前所拥有的。

任何有关我如何从使用 MediaPlayer 基本上更改为 SoundPool 的指示将不胜感激。

mpButtonOne = MediaPlayer.create(camera_page.this, R.raw.default);

if (mpButtonOne==null){
    //display a Toast message here
    return;
}

mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        int audio = mfile[rnd.nextInt(SOUND_CLIPS)];
        audioReplayPrimary = audio;
        mpButtonOne.reset();
        mpButtonOne = MediaPlayer.create(camera_page.this, audio);

        if (mpButtonOne==null){
            //display a Toast message here
            return;
        }

        mpButtonOne.start();

        mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                int audio2 = mfile2[rnd.nextInt(SOUND_CLIPS2)];
                audioReplaySecondary = audio2;
                mpButtonOne.reset();
                mpButtonOne = MediaPlayer.create(camera_page.this, audio2);

                if (mpButtonOne==null){
                    //display a Toast message here
                    return;
                }
                mpButtonOne.start();

                mpButtonOne.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mpButtonOne.release();
                    }

更新

    private static SoundPool mSoundPool;
private static AudioManager mAudioManager;
private final int SOUND_CLIPS3 = 5;
private int mfile3[] = new int[SOUND_CLIPS3];
private Random rnd = new Random();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sptest);

    mfile3[0] = R.raw.one;
    mfile3[1] = R.raw.two;
    mfile3[2] = R.raw.three;
    mfile3[3] = R.raw.four;
    mfile3[4] = R.raw.five;

    mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);


        Button spTest = (Button) findViewById(R.id.buttonSp);
    spTest.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int audioPre = mfile3[rnd.nextInt(SOUND_CLIPS3)];
            int sound1 = mSoundPool.load(sptestclass.this, audioPre, 1);
            mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);

    }
    });

最佳答案

您可以像这样使用 android.media.SoundPool 对象:

import android.media.SoundPool;
//...

//inside your class:
//define: mContext = <your context>
private static SoundPool mSoundPool;
private static AudioManager mAudioManager;

//inside your method:
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

int sound1 = mSoundPool.load(mContext, R.raw.sound1, 1); //<context, soundId, priority>
int sound2 = mSoundPool.load(mContext, R.raw.sound2, 1); //<context, soundId, priority>
int sound3 = mSoundPool.load(mContext, R.raw.sound3, 1); //<context, soundId, priority>

//play a sound:
mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);//soundId (from load()), volLeft, volRight, priority, loop(boolean), playbackRate


//register a callback for onLoad...:
mSoundPool1.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        //call .play() for the next sound
    }
});


//if you want to explicitly stop the sound:
mSoundPool.stop(sound1);

关于java - 从媒体播放器更改为声音池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18970877/

相关文章:

java - 当我尝试将 Object 转换为 List<Object> 时发出警告

java - JPA - InheritanceType.Joined 生成错误的表

java - PaintComponent 不绘制图 block 或绘制字符串

android形状不改变大小

android - 在单独的线程中运行我的 MainActivity 的一部分

android - jar 不匹配!修复你的依赖

java - 创建 1MB 字符串的好方法是什么?

安卓 : How to add a listview inside a scrollview. ?

eclipse - 'update to HEAD' 的快捷方式

java - 从头开始构建 Eclipse IDE - 制作更大的图标有时会成功