开发者

Android can't play some wav file with soundpool?

开发者_开发技巧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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜