前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Avalonia跨平台入门第四十篇之再玩语音播放

Avalonia跨平台入门第四十篇之再玩语音播放

作者头像
WPF程序员
发布2024-07-19 13:26:45
640
发布2024-07-19 13:26:45

前面分享了一下Avalonia跨平台入门第三十九篇之再耍CEF;结果在我准备用Avalonia11的版本去玩耍时,直接给我提示:

1、果然不能用,为了新版本赶紧换思路:

代码语言:javascript
复制
https://github.com/bkempinski/CefNet.Avalonia

2、来看看现在在Ubuntu下运行的最终效果:

3、接下来才是正题:语音播放,参考Avalonia跨平台入门第十九篇之语音播放:

4、初始化OpenAL:

代码语言:javascript
复制
private void InitializeOpenAL()
{
    var device = ALC.OpenDevice(null);
    var context = ALC.CreateContext(device, (int[])null);
    ALC.MakeContextCurrent(context);
    _buffer = AL.GenBuffer();
    _source = AL.GenSource();
    GCHandle handle = GCHandle.Alloc(_audioData, GCHandleType.Pinned);
    try
    {
        AL.BufferData(_buffer, _format, handle.AddrOfPinnedObject(), _audioData.Length, _frequency);
    }
    finally
    {
        handle.Free();
    }
    AL.Source(_source, ALSourcei.Buffer, _buffer);
    AL.Source(_source, ALSourceb.Looping, true);
}

5、关于读取 Wav文件:

代码语言:javascript
复制
private void LoadWavFile(string filePath)
{
    using (FileStream fs = File.OpenRead(filePath))
    using (BinaryReader reader = new BinaryReader(fs))
    {
        // RIFF header
        reader.ReadChars(4); // "RIFF"
        reader.ReadInt32();  // Chunk size
        reader.ReadChars(4); // "WAVE"

        // Format header
        reader.ReadChars(4); // "fmt "
        int fmtChunkSize = reader.ReadInt32();  // Subchunk1 size
        int audioFormat = reader.ReadInt16();  // Audio format (1 = PCM)
        int numChannels = reader.ReadInt16();  // Number of channels
        _frequency = reader.ReadInt32();       // Sample rate
        reader.ReadInt32();  // Byte rate
        reader.ReadInt16();  // Block align
        int bitsPerSample = reader.ReadInt16(); // Bits per sample

        // Validate format
        if (audioFormat != 1)
            throw new NotSupportedException("Only PCM WAV files are supported.");

        _format = (numChannels == 1 && bitsPerSample == 8) ? ALFormat.Mono8 :
                  (numChannels == 1 && bitsPerSample == 16) ? ALFormat.Mono16 :
                  (numChannels == 2 && bitsPerSample == 8) ? ALFormat.Stereo8 :
                  (numChannels == 2 && bitsPerSample == 16) ? ALFormat.Stereo16 :
                  throw new NotSupportedException("The specified WAV format is not supported.");

        // Move to data chunk
        reader.ReadChars(4); // "data"
        int dataSize = reader.ReadInt32();
        _audioData = reader.ReadBytes(dataSize);
    }
}

6、结果又出现X11问题:

7、解决方案:把依赖复制到输出目录:

代码语言:javascript
复制
OpenAL32.dll
wrap_oal.dll

8、在乌班图下出现问题:

9、解决方案:

代码语言:javascript
复制
sudo apt-get install libopenal-dev /y

最终简单的效果先这样吧;以后有时间的话,可以再去摸索一下更复杂的效果;编程不息、Bug不止、无Bug、无生活;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-07-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WPF程序员 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档