Avalonia跨平台入门第十九篇之语音播放

在前面分享的几篇中咱已经玩耍了PopupListBox多选Grid动态分RadioButton模板控件的拖放效果控件的置顶和置底控件的锁定自定义Window样式动画效果Expander控件ListBox折叠列表聊天窗口ListBox图片消息窗口抖动语音发送;今天趁着大周末的时间接着去摸索语音的播放效果,最终实现的效果:

c145e98c47c833e93d21bd2f9b7b2f7e.gif

播放语音使用了OpenAL:

public static void PlayVoice(string filename)
{
    using(AudioContext context = new AudioContext())
    {
        int buffer = AL.GenBuffer();
        int source = AL.GenSource();
        int state;
        int channels, bits_per_sample, sample_rate;
        byte[] sound_data = LoadWave(File.Open(filename, FileMode.Open), out channels, out bits_per_sample, out sample_rate);
        AL.BufferData(buffer, GetSoundFormat(channels, bits_per_sample), sound_data, sound_data.Length, sample_rate);
        AL.Source(source, ALSourcei.Buffer, buffer);
        AL.SourcePlay(source);
        Trace.Write("Playing");
        // Query the source to find out when it stops playing.
        do {
            Thread.Sleep(250);
            Trace.Write(".");
            AL.GetSource(source, ALGetSourcei.SourceState, out state);
        }
        while ((ALSourceState) state == ALSourceState.Playing);
        Trace.WriteLine("");
        AL.SourceStop(source);
        AL.DeleteSource(source);
        AL.DeleteBuffer(buffer);
    }
}

1、至于播放语音的动画,一开始想到用OpacityMask试试,结果失败了;

2、想着数据触发器绑定样式,结果貌似不支持

3、后面想到上面覆盖Rectangle控制他的Margin实现;

3、然后找到Avalonia.Xaml.Behaviors 这个类库,结果也没成功;

4、后来发现Github上的一种解决方案

https://github.com/AvaloniaUI/Avalonia/issues/2427 
用#5710实现
<Button Classes.myClass= "{Binding xxx}" />

然后就有了最初的一版写法:

<Rectangle Name="RecOverLayer" Margin="70,0,0,0" Width="35" Height="35" Fill="#E5E5E5"
                   Classes.MyAnimation="{Binding $parent[Controls:ChatBubble].IsStartPlay,Mode=TwoWay}">
    <Rectangle.Styles>
        <Style Selector="Rectangle.MyAnimation">
            <Style.Animations>
                <Animation Duration="0:0:1"
                           IterationCount="{Binding Duration,Converter={StaticResource PlayDurationToIterationCountConverter}}" 
                           FillMode="Both" PlaybackDirection="Reverse">
                    <KeyFrame Cue="100%">
                        <Setter Property="Margin" Value="0"/>
                    </KeyFrame>
                </Animation>
            </Style.Animations>
            </Style>
    </Rectangle.Styles>
</Rectangle>

后面为了通用性,使用了绑定:

<Rectangle Name="RecOverLayer" Margin="{Binding Role,Converter={StaticResource ChatRoleTypeToRectangleMarginConverter}}" Width="35" Height="35" Fill="{Binding Role,Converter={StaticResource ChatRoleTypeToRectangleFillConverter}}" Classes.MyAnimation="{Binding $parent[Controls:ChatBubble].IsStartPlay}">
    <Rectangle.Styles>
        <Style Selector="Rectangle.MyAnimation">
            <Style.Animations>
                <Animation Duration="0:0:1" IterationCount="{Binding Duration,Converter={StaticResource PlayDurationToIterationCountConverter}}" FillMode="Both" PlaybackDirection="Reverse">
                    <KeyFrame Cue="100%">
                        <Setter Property="Margin" Value="0" />
                    </KeyFrame>
                </Animation>
            </Style.Animations>
            </Style>
    </Rectangle.Styles>
</Rectangle>

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个问题通常是由于Emgu.CV库在加载C++库时发生的错误导致的。为了解决这个问题,你可以尝试以下步骤: 1.确保你已经按照Emgu.CV的安装指南进行了安装,并且已经添加了所需的引用。 2.在你的项目中添加以下NativeLibrary.SetDllImportResolver代码,该代码会解决安卓项目下无法找到C++库的问题。 ```csharp using System.Runtime.InteropServices; using Emgu.CV; using NativeLibrary = System.Runtime.InteropServices.NativeLibrary; public static class Bootstrapper { public static void Init() { NativeLibrary.SetDllImportResolver(typeof(CvInvoke).Assembly, DllImportResolver); } private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (libraryName == "libgdiplus") { return LoadLibgdiplus(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libgdiplus.so")); } return IntPtr.Zero; } [DllImport("libdl.so")] private static extern IntPtr dlopen(string filename, int flags); [DllImport("libdl.so")] private static extern IntPtr dlerror(); private static IntPtr LoadLibgdiplus(string path) { IntPtr lib = dlopen(path, RTLD_NOW); if (lib == IntPtr.Zero) { var error = Marshal.PtrToStringAnsi(dlerror()); throw new Exception($"Failed to load libgdiplus from '{path}'. {error}"); } return lib; } private const int RTLD_NOW = 2; } ``` 3.在你的Android项目的MainActivity.cs文件中添加以下代码,用于在应用程序启动时初始化Emgu.CV。 ```csharp protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bootstrapper.Init(); // Initialize Emgu.CV // Other initialization code } ``` 通过这些步骤,你应该可以解决System.TypeInitializationException: The type initializer for 'Emgu.CV.CvInvoke' threw an exception问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值