1

这是我的主要活动,我相信这就是你所需要的,看看是否有人可以帮助我......

package com.art.drumdrum;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
SoundPool soundPool = null;
int kickId = 0;
int snareId = 0;
int crashhId = 0;
int crashlId = 0;
int muteId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button kick = (Button) findViewById(R.id.kick);
    Button snare = (Button) findViewById(R.id.snare);
    Button crashh = (Button) findViewById(R.id.crashh);
    Button crashl = (Button) findViewById(R.id.crashl);

    kick.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                soundPool.play(kickId, 1, 1, 0, 0, 1);
            return false;
        }
    });
    snare.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                soundPool.play(snareId, 1, 1, 0, 0, 1);
            return false;
        }
    });
    crashh.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                soundPool.play(crashhId, 1, 1, 0, 0, 1);
            return false;
        }
    });
    crashl.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                soundPool.play(crashlId, 1, 1, 0, 0, 1);
            return false;
        }
    });

}

protected void onResume() {
    super.onResume();
    if (soundPool == null) {
        soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
        kickId = soundPool.load(this, R.raw.kick, 1);
        snareId = soundPool.load(this, R.raw.snare, 1);
        crashhId = soundPool.load(this, R.raw.crashh, 1);
        crashlId = soundPool.load(this, R.raw.crashl, 1);
    }
}

protected void onPause() {
    super.onPause();
    if (soundPool != null) {
        soundPool.release();
        soundPool = null;
    }
}

}

我想知道在我想做的情况下,使用 JetPlayer 是否会比 soundPool 更好。我有它,所以我可以同时按下多个按钮并播放声音,一切看起来都很好,但是从你按下按钮到它播放声音有一点延迟,使它听起来变得不自然......我'几天来一直在查找信息并没有找到任何帮助,是否有简单的答案,或者它是否需要更多代码甚至可能需要一些 c++?

4

1 回答 1

4

据我所知,JetPlayer 不适合这里,因为它的目标是 MIDI 音频同步。在 Jet 音频引擎中,当您​​按下按钮时,您无法正确播放声音,除非您停止所有先前播放的声音(通过JetPlayer#clearQueue())。

安卓音频延迟问题也没有简单的答案。可能您可以使用 C++ (OpenSL ES) 获得更好的延迟,但我对此知之甚少。请查看相关答案:https ://stackoverflow.com/a/12309643

于 2013-03-05T20:25:12.317 回答