在我的视频渲染器中,我尝试将音量默认设置为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.
}
}
sender
事件中的VideoView.Prepared
是MediaPlayer
。您可以使用该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());