如何在Xamarin Native video Player中将音量设置为0

问题描述 投票:0回答:1

在我的视频渲染器中,我尝试将音量默认设置为0。我尝试使用setVolume,但在我的视频视图中不可用。请提出建议。我希望在视频处于背景模式时将音量值设置为0。我已附上我的VideoRenderer文件供Android参考。

public class VideoPlayerRenderer : ViewRenderer<VideoPlayer, ARelativeLayout>
    {
        VideoView videoView;
        MediaController mediaController;    // Used to display transport controls
        bool isPrepared;

        public VideoPlayerRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> args)
        {
            base.OnElementChanged(args);

            if (args.NewElement != null)
            {
                if (Control == null)
                {
                    // Save the VideoView for future reference
                    videoView = new VideoView(Context);

                    // Put the VideoView in a RelativeLayout
                    ARelativeLayout relativeLayout = new ARelativeLayout(Context);
                    relativeLayout.AddView(videoView);

                    // Center the VideoView in the RelativeLayout
                    ARelativeLayout.LayoutParams layoutParams =
                        new ARelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
                    layoutParams.AddRule(LayoutRules.CenterInParent);
                    videoView.LayoutParameters = layoutParams;

                    // Handle a VideoView event
                    videoView.Prepared += OnVideoViewPrepared;


                    SetNativeControl(relativeLayout);

                    //Update Volume
                    UpdateVolume();
                }

                SetSource();

            }

         private void UpdateVolume()
             {
                var volume = Element.Volume;

                if (volume != int.MinValue)
                {
                    var nativeVolume = (float)Math.Min(100, Math.Max(0, volume)) / 100;

                    //assign my native volume to my video view. 


                }

              }
xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票

sender事件中的VideoView.PreparedMediaPlayer。您可以使用该MediaPlayer实例来控制音量等。

private void OnPrepared(object sender, EventArgs e)
{
    if (sender is MediaPlayer mediaPlayer)
    {
        mediaPlayer.SetVolume(0, 0);
    }
}

或者,如果您愿意,可以实现自己的IOnPreparedListener并传递给VideoView

private class PreparedListener : Java.Lang.Object, MediaPlayer.IOnPreparedListener
{
    public void OnPrepared(MediaPlayer mp)
    {
        mp.SetVolume(0, 0);
    }
}

并将其设置为VideoView上的侦听器,如:

videoView.SetOnPreparedListener(new PreparedListener());
© www.soinside.com 2019 - 2024. All rights reserved.