我的音池项目不能正常播放?

时间:2013-04-17 23:38:36

标签: java android soundpool

我一直在尝试实现这个soundpool类(在教程的帮助下)但是当我尝试在虚拟设备上运行时,声音将无法播放... main_activity XML文件加载正常,但是预计在加载时会发出声音。

这是源代码。

public class MainActivity extends Activity {

private SoundPool soundpool;
private boolean soundPoolLoaded = false;

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

    // Create a new SoundPool instance.
    // 2 = number of sounds that can be simultaneously played
    // AudioManager.STREAM_MUSIC = The type of sound stream. STREAM_MUSIC is the most common one
    // 0 = sound quality, default is 0.
    // setOnLoadCompleteListener loads the file 
    soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
    soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            soundPoolLoaded = true;
        }
    });

    playSound("car_phonic");
}


// This method will load a sound resource from the res/raw folder by the input name given as a string (soundName).
// If the sound resource is found by name, it will attempt to play the sound
// soundPoolLoaded value becomes true when the file is fully loaded
public void playSound(String soundName) {
    if(soundPoolLoaded == true)
    {
        // Get the current context resources and find the correct sound resource for playing the sound
        Resources res = this.getResources();
        int soundId = 0;
        soundId = res.getIdentifier(soundName, "raw", this.getPackageName());

        if(soundId != 0){   
        soundpool.play(soundId, 1, 1, 0, 0, 1);
        }


        SystemClock.sleep(500);
        playSound(soundName);

    }
    }
}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

  • play()接受SoundPools load()返回的soundID,而不是通用的android资源id
  • onLoadComplete被调用然后声音文件完成加载,注意sampleId参数

所以,用它的基本形式:

soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        soundpool.play(sampleId, 1, 1, 0, 0, 1);
    }
});

soundpool.load(this, R.raw.car_phonic, 0);

请注意,SoundPool会在后台加载数据,因此样本不会立即可用。

答案 1 :(得分:0)

这是你应该如何使用soundpool

soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        float curVolume = audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float leftVolume = curVolume / maxVolume;
        float rightVolume = curVolume / maxVolume;
        int priority = 10;
        // int priority_1=5;
        int no_loop = 0;
        float normal_playback_rate = 1f;


final int ID=1;
    soundPool.play(soundPoolMap.get(ID,
                        leftVolume, rightVolume, priority, no_loop,
                        normal_playback_rate);