0

我目前正在寻找某种方法来扩展 SFML.Net 以使用 MP3。所以ioctllr推荐了NLayer,我想试试。这是我的方法:

namespace AudioCuesheetEditor.AudioBackend
{
public class SFMLMp3Stream : Stream
{
    private static readonly Logfile log = Logfile.getLogfile(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    private MpegFile file;

    public SFMLMp3Stream(String _filename)
    {
        //TODO:Check file existence
        file = new MpegFile(_filename);
        log.debug("file = " + file.ToString());
    }

    #region implemented abstract members of Stream

    public override void Flush()
    {
        file.Position = 0;
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        switch (origin)
        {
            case SeekOrigin.Begin:
                file.Position = offset;
                break;
            case SeekOrigin.Current:
                file.Position = file.Position + offset;
                break;
            case SeekOrigin.End:
                break;
        }
        return file.Position;
    }

    public override void SetLength(long value)
    {
        //TODO?
        int i = 1;
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return file.ReadSamples(buffer, offset, count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        throw new NotImplementedException();
    }

    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanSeek
    {
        get
        {
            return file.CanSeek;
        }
    }

    public override bool CanWrite
    {
        get
        {
            //Writing to stream is prohibited
            return false;
        }
    }

    public override long Length
    {
        get
        {
            return file.Length;
        }
    }

    public override long Position
    {
        get
        {
            return file.Position;
        }
        set
        {
            file.Position = value;
        }
    }

    #endregion
}

}

这样使用:

musicStream = new SFMLMp3Stream(this.objProgram.getObjCuesheet().getAudiofilePath(true));
                try
                {
                    music = new Music(musicStream);
                }
                catch(Exception ex)
                {
                    log.fatal(ex.ToString());
                }

我从 SFML 得到了例外:

05.06.2014 10:34:28 AudioCuesheetEditor.AudioBackend.AudioManagerSFML: FATAL |    SFML.LoadingFailedException: Failed to load music from memory
bei SFML.Audio.Music..ctor(Stream stream)
bei AudioCuesheetEditor.AudioBackend.AudioManagerSFML.setMusic() in d:\tmp\AudioCuesheetEditor\src\AudioCuesheetEditor\AudioBackend\AudioManagerSFML.cs:Zeile 50.

我在 SFML 中读到,它需要 PCM 16 位有符号整数(http://en.sfml-dev.org/forums/index.php?topic=1406.0)。NLayer 支持这个吗?我需要如何更改代码才能正常工作?

谢谢你的帮助。斯文

4

1 回答 1

0

Unhappiliy Music 课程不提供阅读 mp3 流。

于 2014-06-06T13:26:38.030 回答