我正在为我的应用程序开发一个audiobackend,它运行在Mono / .NET上。因此我发现,SFML是平台依赖的,因此它适合。但由于许可,没有mp3支持。
所以我想在C#中为自己添加mp3功能。我使用SFML.Net 2.1和Mp3Sharp(http://www.robburke.net/mle/mp3sharp/)。起初我试过这个:
Mp3Stream stream = new Mp3Stream("D:\\tmp\\test.mp3");
Music music = new Music(stream);
music.play();
但那没用。例外:
AudioCuesheetEditor.AudioBackend.AudioManagerSFML: FATAL | SFML.LoadingFailedException: Failed to load music from memory
at SFML.Audio.Music..ctor(Stream stream)
at AudioCuesheetEditor.AudioBackend.AudioManagerSFML.setMusic() in d:\tmp\AudioCuesheetEditor\src\AudioCuesheetEditor\AudioBackend\AudioManagerSFML.cs
所以我考虑过在示例中完成(https://github.com/LaurentGomila/SFML/wiki/Source:-MP3-Player):
我创建了一个扩展SoundStream的类SFMLMp3Stream:
public class SFMLMp3Stream : SoundStream
{
private Mp3Stream stream;
public SFMLMp3Stream()
{
stream = null;
}
public Boolean openFromFile(String _filename)
{
//TODO:Check file existence
stream = new Mp3Stream(_filename);
Initialize((uint)stream.ChannelCount,(uint) stream.Frequency);
return true;
}
#region implemented abstract members of SoundStream
protected override bool OnGetData(out short[] samples)
{
if (stream != null)
{
byte[] buffer = new byte[512];
short[] retVal = new short[512];
stream.Read(buffer,0,buffer.Length);
Buffer.BlockCopy(buffer,0,retVal,0,buffer.Length);
samples = retVal;
return true;
}
else
{
samples = null;
return false;
}
}
protected override void OnSeek(TimeSpan timeOffset)
{
if (stream != null)
{
stream.Seek((long)timeOffset.TotalMilliseconds, SeekOrigin.Current);
}
}
#endregion
}
当我现在尝试这个时:
musicStream = new SFMLMp3Stream();
musicStream.openFromFile(this.objProgram.getObjCuesheet().getAudiofilePath(true));
music = new Music(musicStream);
我收到编译错误,音乐不能从SFMLMp3Stream启动(因为它没有字符串而没有System.IO.Stream。如何扩展SFML用于Mp3用途?到目前为止有人这样做了吗?
感谢您的帮助 斯文