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

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

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

https://github.com/bkempinski/CefNet.Avalonia

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

377f9ce44120da5db1146f82bddb47b2.gif

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

f0599014be02696454737fbeb44713d4.png

4、初始化OpenAL:

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文件:

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问题:

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

OpenAL32.dll
wrap_oal.dll

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

804943cb85fb56b1a14d12107c61f258.png9、解决方案:

sudo apt-get install libopenal-dev /y

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值