简体   繁体   English

我的音池项目无法正常播放?

[英]my soundpool project won't play properly?

I've been trying to implement this soundpool class (with some help from a tutorial) but when I try to run it on a virtual device, the sound won't play... The main_activity XML file loads fine, but it was expected that on loading, a sound would be played. 我一直在尝试实现此soundpool类(在教程中得到了一些帮助),但是当我尝试在虚拟设备上运行它时,声音将无法播放... main_activity XML文件可以很好地加载,但是可以预期加载时会播放声音。

here is the source code. 这是源代码。

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);

    }
    }
}

Any help would be appreciated! 任何帮助,将不胜感激!

  • play() takes the soundID returned by SoundPools load(), not the generic android resource id play()采用SoundPools load()返回的soundID,而不是通用的android资源ID
  • onLoadComplete is called then sound file finishes loading, notice the sampleId argument 调用onLoadComplete然后声音文件完成加载,请注意sampleId参数

So, in it's basic form: 因此,以其基本形式:

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);

Note that SoundPool loads data in background so samples are not immediately available. 请注意,SoundPool在后台加载数据,因此无法立即获得样本。

here's how you should use 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM