Android can't play some wav file with soundpool?

耗尽温柔 提交于 2021-02-10 12:22:20

问题


Some .wav file I can't play with soundpool. I can't hear anything. Some files play just fine. Why?

code

    AudioManager mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, 0, 1f);

回答1:


I had lots of problems with soundpool and file formats. Read my original question, it might be helpful to you.

Basically, I changed to use MediaPlayer and had no more problems.




回答2:


My finding using .WAV resource files with an LG Optimus F3 running Jellybean 4.1.2 is that its MediaPlayer library fails to decode:

  1. Files whose bitrate exceeds 256kbps, and
  2. Files whose duration is less than .075 seconds

The symptom in logcat in either case is:

V/SoundPool﹕ load: fd=49, offset=123932, length=536, priority=1
V/SoundPool﹕ create sampleID=3, fd=50, offset=536, length=123932
V/SoundPool﹕ doLoad: loading sample sampleID=3
V/SoundPool﹕ Start decode
V/MediaPlayer﹕ decode(50, 123932, 536)
V/SoundPool﹕ close(50)
E/SoundPool﹕ Unable to load sample: (null)

Rather than generate a fatal exception, the above error causes SoundPool to later attempt to use these "null" sounds, which in turn can cause the phone to lock up and/or lose frames. Reducing the bitrate and using Audacity's "add silence" effect to lengthen the time of my sound files has solved this problem.

In contrast to the symptom above, a successful SoundPool load looks like this in logcat:

V/SoundPool﹕ load: fd=55, offset=765700, length=3534, priority=1
V/SoundPool﹕ create sampleID=7, fd=56, offset=3534, length=765700
V/SoundPool﹕ doLoad: loading sample sampleID=7
V/SoundPool﹕ Start decode
V/MediaPlayer﹕ decode(56, 765700, 3534)
V/SoundPool﹕ close(56)
V/SoundPool﹕ pointer = 0x5f464000, size = 6976, sampleRate = 8000, numChannels = 2

If you are loading multiple sounds (which is the purpose of SoundPool), note your sampleID values as they tell which sound file failed to load, the first one loaded being sampleID=1, and so on. That helped me distinguish "bad" from "good" .WAV files.

Note that I have not had this problem on several other platforms from LG and Samsung running Android Kitkat, which load higher bitrate and short sound files successfully.



来源:https://stackoverflow.com/questions/5068068/android-cant-play-some-wav-file-with-soundpool

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!