我目前正在研究 SFML.Net 以扩展对 mp3 的支持。因此,我编写了一个使用 NLayer MpegFile 解码 mp3 的 Stream 类。
public class Mp3StreamSFML : SoundStream
{
private MpegFile mp3file;
private int currentBufferSize;
private short[] currentBuffer;
public Mp3StreamSFML(String _filename)
{
mp3file = new MpegFile(_filename);
Initialize((uint)mp3file.Channels, (uint)mp3file.SampleRate);
currentBufferSize = 0;
currentBuffer = new short[currentBufferSize];
}
#region implemented abstract members of SoundStream
protected override bool OnGetData(out short[] samples)
{
if (currentBufferSize <= mp3file.Position)
{
byte[] buffer = new byte[512];
if (mp3file.ReadSamples(buffer, 0, buffer.Length) > 0)
{
Array.Resize(ref currentBuffer, currentBuffer.Length + (buffer.Length / 2));
Buffer.BlockCopy(buffer, 0, currentBuffer, currentBufferSize, buffer.Length);
currentBufferSize = currentBuffer.Length;
}
samples = currentBuffer;
return true;
}
else
{
samples = currentBuffer;
return false;
}
}
protected override void OnSeek(TimeSpan timeOffset)
{
mp3file.Position = (long)timeOffset.TotalSeconds;
}
#endregion
}
我这样使用它:
try
{
stream = new Mp3StreamSFML(this.objProgram.getObjCuesheet().getAudiofilePath(true));
stream.Play();
log.debug("samplerate = " + stream.SampleRate);
}
catch(Exception ex)
{
log.fatal(ex.ToString());
}
不幸的是,没有播放正确的声音,它只是“断断续续”,听起来很奇怪。我做错了什么?似乎是两个框架之间的问题。
谢谢你的帮助。斯文