Xamarin效果第二十二篇之录音效果

在前面文章中简单玩了玩GIS的基本操作Mark相关AR测距加载三维白模可扩展浮动操作;今天抽空再来分享一下录音效果;啥也不说了都在效果里3789676e3e093d11f8e0958d23617e19.png:

1、首次尝试了开源的Plugin.AudioRecorder结果发现没效果,也可能是我的姿势不对ba530e4a0131fa43ac3c94477fe9c9cc.png:

https://github.com/NateRickard/Plugin.AudioRecorder

2、请教大佬大白,送了一句:“反转依赖",然后贴了点代码;我是没听明白什么是反转依赖,最终才知道是使用DependencyService这个类;

说白了就是共享代码可以调用每一个独立平台的原生API

3、共享代码定义要实现的接口:

c114f60112678ab6c122dfcd39de777b.png

4、然后每个平台实现接口,屌丝机下:

07e5792e357a67eace4b7429e9c6ef5d.png

5、注册平台实现,这样Xamarin.Forms才可以在运行时找到它们

aecc78d2255c72538fdc21e695ccedf9.png

6、安卓下录音就用MediaRecorder

//设置麦克风
mMediaRecorder.SetAudioSource(AudioSource.Mic);
mMediaRecorder.SetOutputFormat(OutputFormat.Default);
mMediaRecorder.SetAudioEncoder(AudioEncoder.Default);
mMediaRecorder.SetOutputFile(filePath);
mMediaRecorder.Prepare();
mMediaRecorder.Start();

7、播放录音效果

public void Play(RecordModel model)
{
    if(model != null)
    {
        var player = new MediaPlayer();
        player.Prepared += (s, e) =>
        {
            player.Start();
            model.IsPlaying = true;
        };
        player.SetDataSource(model.Path);
        player.Prepare();
        //播放完成时
        player.Completion += (ss, ee) =>
        {
            model.IsPlaying = false;
        };
    }
}

8、前台所有录音文件还是CollectionView(独爱),他的ItemTemplate:

<Grid HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="50">
    <StackLayout Orientation="Horizontal" Margin="10,4,0,0" Padding="0">
        <!--小话筒-->
        <Label Text="&#xe608;" FontSize="22" BackgroundColor="Transparent" FontFamily="iconfont.ttf#" VerticalOptions="Center" TextColor="{Binding IsPlaying,Converter={StaticResource IsPlayingToTextColorConverter}}" />
        <!--录音保存的名称-->
        <Label Text="{Binding Name}" VerticalOptions="Center" FontSize="18" TextColor="{Binding IsPlaying,Converter={StaticResource IsPlayingToTextColorConverter}}" />
        <!--时长,这里主要StringFormat用法-->
        <Label Text="{Binding Duration,StringFormat='{0}秒'}" Margin="10,0,0,0" VerticalOptions="Center" FontSize="18" TextColor="{Binding IsPlaying,Converter={StaticResource IsPlayingToTextColorConverter}}" />
        <!--播放-->
        <Label Text="&#xe717;" FontSize="25" VerticalOptions="Center" FontFamily="iconfont.ttf#" TextColor="#98A6B1">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MainViewModel}},Path=PlayRecordCommand}" CommandParameter="{Binding}" /> </Label.GestureRecognizers>
        </Label>
    </StackLayout>
</Grid>

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值