当前,我正在编写我的第一个xamarin跨平台应用程序。 我尝试添加视频背景,并在互联网上找到了以下指南:https://www.junian.net/xamarin-forms-video-background/
但是当我尝试在iOS模拟器上对其进行测试时,出现以下错误:
2019-01-12 02:27:54.095262-0800 RelationshipAppNew.iOS [1587:10185] [播放]用于解决错误Error Domain = AVFoundationErrorDomain Code = -11800“该操作无法完成“ UserInfo = {NSLocalizedFailureReason =发生未知错误(-101),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x6000023a4a20 {Error Domain = NSOSStatusErrorDomain Code = -101”(null)“}}
2019-01-12 02:27:54.095800-0800 RelationshipAppNew.iOS [1587:10185] [回放]❗️解决方案无法解决错误:Error Domain = AVFoundationErrorDomain Code = -11800 “操作无法完成” UserInfo = {NSLocalizedFailureReason =发生未知错误(-101),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x6000023a4a20 {Error Domain = NSOSStatusErrorDomain Code = -101“(null)”}}分辨率错误:(空)
2019-01-12 02:27:54.096289-0800 RelationshipAppNew.iOS [1587:10185] [Playback]❗️Playback失败并出现错误:Error Domain = AVFoundationErrorDomain Code = -11800“ The operation无法完成” UserInfo = {NSLocalizedFailureReason =发生未知错误(-101),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x6000023a4a20 {Error Domain = NSOSStatusErrorDomain Code = -101“(null)”}},无法解决(canResolve:否,allowsItemErrorResolution:否) 2019-01-12 02:27:54.190780-0800 RelationshipAppNew.iOS [1587:10185]通知:NSConcreteNotification 0x60000238a580 {name = MPMoviePlayerPlaybackDidFinishNotification;对象=; userInfo = { MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1; error =“错误域= MediaPlayerErrorDomain代码= -11800 \”操作无法完成\“ UserInfo = {NSLocalizedDescription =操作无法完成}”; }}
2019-01-12 02:27:54.252088-0800 RelationshipAppNew.iOS [1587:10185] FinishReason:PlaybackError
这是iOS渲染器的代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RelationshipAppNew.Controls;
using RelationshipAppNew.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;
using MediaPlayer;
[assembly: ExportRenderer(typeof(Video), typeof(VideoRenderer))]
namespace RelationshipAppNew.iOS.Renderers
{
class VideoRenderer : ViewRenderer<Video, UIView>
{
MPMoviePlayerController videoPlayer;
NSObject notification = null;
protected override void
OnElementChanged(ElementChangedEventArgs<Video> e)
{
base.OnElementChanged(e);
if (Control == null)
{
InitVideoPlayer();
}
if (e.OldElement != null)
{
// Unsubscribe
notification?.Dispose();
}
if (e.NewElement != null)
{
// Subscribe
notification = MPMoviePlayerController.Notifications.ObservePlaybackDidFinish((sender, args) =>
{
/* Access strongly typed args */
Console.WriteLine("Notification: {0}", args.Notification);
Console.WriteLine("FinishReason: {0}", args.FinishReason);
Element?.OnFinishedPlaying?.Invoke();
});
}
}
void InitVideoPlayer()
{
var path = Path.Combine(NSBundle.MainBundle.BundlePath, Element.Source);
if (!NSFileManager.DefaultManager.FileExists(path))
{
Console.WriteLine("Video not exist");
videoPlayer = new MPMoviePlayerController();
videoPlayer.ControlStyle = MPMovieControlStyle.None;
videoPlayer.ScalingMode = MPMovieScalingMode.AspectFill;
videoPlayer.RepeatMode = MPMovieRepeatMode.One;
videoPlayer.View.BackgroundColor = UIColor.Clear;
SetNativeControl(videoPlayer.View);
return;
}
// Load the video from the app bundle.
NSUrl videoURL = new NSUrl(path, false);
// Create and configure the movie player.
videoPlayer = new MPMoviePlayerController(videoURL);
videoPlayer.ControlStyle = MPMovieControlStyle.None;
videoPlayer.ScalingMode = MPMovieScalingMode.AspectFill;
videoPlayer.RepeatMode = Element.Loop ? MPMovieRepeatMode.One : MPMovieRepeatMode.None;
videoPlayer.View.BackgroundColor = UIColor.Clear;
foreach (UIView subView in videoPlayer.View.Subviews)
{
subView.BackgroundColor = UIColor.Clear;
}
videoPlayer.PrepareToPlay();
SetNativeControl(videoPlayer.View);
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null || Control == null)
return;
if (e.PropertyName == Video.SourceProperty.PropertyName)
{
InitVideoPlayer();
}
else if (e.PropertyName == Video.LoopProperty.PropertyName)
{
var liveImage = Element as Video;
if (videoPlayer != null)
videoPlayer.RepeatMode = Element.Loop ? MPMovieRepeatMode.One : MPMovieRepeatMode.None;
}
}
}
}
我认为MPMoviePlayerController出了点问题,但是我找不到故障。