Avalonia跨平台入门第十九篇之语音播放
在前面分享的幾篇中咱已經玩耍了Popup、ListBox多選、Grid動態分、RadioButton模板、控件的拖放效果、控件的置頂和置底、控件的鎖定、自定義Window樣式、動畫效果、Expander控件、ListBox折疊列表、聊天窗口、ListBox圖片消息、窗口抖動、語音發送;今天趁著大周末的時間接著去摸索語音的播放效果,最終實現的效果:
播放語音使用了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>后面為了通用性,使用了綁定:
最終簡單的效果先這樣吧;還是有個bug,再次播放語音時,動畫效果失效了;回頭再摸索一下原因吧;編程不息、Bug不止、無Bug、無生活;改bug的冷靜、編碼的激情、完成后的喜悅、挖坑的激動 、填坑的興奮;這也許就是屌絲程序員的樂趣吧;今天就到這里吧;希望自己有動力一步一步堅持下去;生命不息,代碼不止;大家抽空可以看看今天分享的效果,有好的意見和想法,可以在留言板隨意留言;我看到后會第一時間回復大家,多謝大家的一直默默的關注和支持!如果覺得不錯,那就伸出您的小手點個贊并關注一下,多謝您的支持!
總結
以上是生活随笔為你收集整理的Avalonia跨平台入门第十九篇之语音播放的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 大话领域驱动设计——简介
- 下一篇: 在 k8s 以外的分布式环境中使用 Da
