在OpenGL ES渲染循环中的短时叠加声音。

huangapple go评论74阅读模式
英文:

Short superimposed sounds in a OpenGL ES render loop

问题

需要在游戏中添加能够同时或几乎同时播放的短声音(例如,爆炸声)。为此,我尝试使用带有MediaPlayer实例的队列:

public class Renderer implements GLSurfaceView.Renderer {
    // 用于交替播放声音的队列
    private Queue<MediaPlayer> queue = new LinkedList<>();
    private MediaPlayer player1;
    private MediaPlayer player2;
    private MediaPlayer player3;
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
        player1.setDataSource(context, uri);
        player1.prepareAsync();
        ...
        queue.add(player1);
        queue.add(player2);
        queue.add(player3);
    }
    public void onDrawFrame(GL10 glUnused) {
        if (isExplosion) {
            MediaPlayer currentPlayer = queue.poll(); // 从队列头部检索播放器
            if (currentPlayer != null) {
                if (!currentPlayer.isPlaying()) currentPlayer.start();
                queue.add(currentPlayer); // 将播放器添加到队列末尾
            }            
        }
    }
}

但是,这种方法显示出性能较差。如何在OpenGL ES渲染循环中播放重叠的短声音呢?

英文:

Need to add short sounds to the game that can play simultaneously or almoust simultaneously (for example, explosive sounds). For this, I tried to use the queue with instances of MediaPlayer:

public class Renderer implements GLSurfaceView.Renderer {
    // queue for alternately playing sounds
    private Queue&lt;MediaPlayer&gt; queue = new LinkedList&lt;&gt;();
    private MediaPlayer player1;
    private MediaPlayer player2;
    private MediaPlayer player3;
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
        player1.setDataSource(context, uri);
        player1.prepareAsync();
        ...
        queue.add(player1);
        queue.add(player2);
        queue.add(player3);
    }
    public void onDrawFrame(GL10 glUnused) {
        if (isExplosion) {
            MediaPlayer currentPlayer = queue.poll(); // retrieve the player from the head of the queue
            if (currentPlayer != null) {
                if (!currentPlayer.isPlaying()) currentPlayer.start();
            queue.add(currentPlayer); // add player to end of queue
            }            
        }
    }
}

But this approach showed poor performance. How to play short superimposed sounds in a OpenGL ES render loop?

答案1

得分: 0

在阅读了这篇帖子之后:

解决方案: 我使用了SoundPool:

public class Renderer implements GLSurfaceView.Renderer {
    private SoundPool explosionSound;
    private SparseIntArray soundPoolMap = new SparseIntArray();
    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
        // 使用三个流来播放声音
        explosionSound = new SoundPool(3, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap.put(0, explosionSound.load(gameActivity, R.raw.explosion, 0));
    }
    public void onDrawFrame(GL10 glUnused) {
        if (isExplosion) { 
            // 播放当前声音
            explosionSound.play(soundPoolMap.get(0), 1.0f, 1.0f, 0, 0, 1f);
        }
    }
}
英文:

After reading this post

Solution: I used SoundPool:

public class Renderer implements GLSurfaceView.Renderer {
    private SoundPool explosionSound;
    private SparseIntArray soundPoolMap = new SparseIntArray();
    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
        // use three streams for playing sounds
        explosionSound = new SoundPool(3, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap.put(0, explosionSound.load(gameActivity, R.raw.explosion, 0));
    }
    public void onDrawFrame(GL10 glUnused) {
        if(isExplosion) { 
            // play current sound
            explosionSound.play(soundPoolMap.get(0), 1.0f, 1.0f, 0, 0, 1f);
        }
    }
}

huangapple
  • 本文由 发表于 2020年3月16日 16:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60702738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定