1

所以我的声音大概是 6.5 秒长,当我用 soundpool 播放它时,它只播放 5 秒……我尝试使用媒体播放器,但对于这种应用程序来说太慢了。声池代码:

    SoundPool sp;
    OnCreate
    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
dub1s = sp.load(this, R.raw.dub1, 1);
Button
    if (dub1s != 0)
            sp.play(dub1s, 1, 1, 0, 0, 1);
    }
4

2 回答 2

2

尝试使用以下加载函数。

public int load (FileDescriptor fd, long offset, long length, int priority)

第三个参数是声音的长度。说,你正在阅读一个文件

FileInputStream in = new FileInputStream(new File(soundFile));

load() 应该看起来像

sp.load(in.getFD(), start, length, 0);
于 2012-11-30T18:14:28.413 回答
0

Better late than never, so for future reference, Soundpool has 1Mb buffer size limit per track. But this limit applies not to file size but to decompressed raw PCM data.

SoundPool is going to decompress the loaded audio to PCM data so it is ready to play instantly without the latency of decoding. If the audio you are loading is compressed heavily, such as MP3, then that can get blown up quite a bit.

You can use Soundpool for sounds longer than 5 seconds (I have played sounds around 30 seconds). But you might need to modify your files. You can try to convert your sound file to ogg, it seems to give better results with Soundpool.

Also, make these changes:

  • Change your sound file's sampling rate to 16000 Hz
  • Change your audio channel to mono, instead of stereo.
  • Make sure your file after these processes is smaller than 1 mb in size.

For these changes you can use an online converter such as this one.

于 2021-01-09T22:26:16.703 回答