iOS视频无法在Android上播放

时间:2019-07-09 13:55:13

标签: c# video xamarin.android xamarin.ios chat

我们正在xamarin本机上构建一个移动聊天应用程序。我们在视频方面遇到问题 由于某些编码问题,从IOS应用程序发送的视频无法在android设备的默认媒体播放器上播放。

为解决此问题,我们正在服务器上转换视频,而该服务器对此进行了大量处理。

建立此过程的方法是什么?也许在发送视频之前先在设备上对视频进行编码?

谢谢。

我们使用从建议中转换而来的内容,但未进行转换,并且选择器卡在了视频快照上:


Picker = new UIImagePickerController()
            {
                SourceType = type,
                MediaTypes = UIImagePickerController.AvailableMediaTypes(type),
                VideoExportPreset = AVAssetExportSessionPreset.Passthrough.GetConstant().ToString(),
                VideoQuality = UIImagePickerControllerQualityType.Medium,
                //AllowsImageEditing = false,
                VideoMaximumDuration = 120f

            };


 var srcUrl = e.Info[UIImagePickerController.MediaURL] as NSUrl;

                    AVUrlAsset avAsset = new AVUrlAsset(srcUrl, new NSDictionary());
                    string[] compatiblePresets = AVAssetExportSession.ExportPresetsCompatibleWithAsset(avAsset);

                    if (compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
                    {
                        AVAssetExportSession exportSession = new AVAssetExportSession(avAsset, AVAssetExportSessionPreset.Passthrough);

                        //var destFile = fileService.MoveFileToMyDocument(srcUrl.Path, ChatMessageType.Video);
                        string destFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString() + ".mp4");

                        exportSession.OutputUrl = new NSUrl(destFile);
                        exportSession.OutputFileType = AVFileType.Mpeg4;
                        exportSession.ShouldOptimizeForNetworkUse = true;

                        exportSession.ExportAsynchronously(() =>
                        {
                            switch (exportSession.Status)
                            {
                                case AVAssetExportSessionStatus.Failed:
                                    //If this fails, it might be because the temorary
                                    //files weren't cleaned and this isn't overwriting
                                    //the temporary filename
                                    Console.WriteLine(@"Export failed: " + exportSession.Error.LocalizedDescription);
                                    //Perform any failed logic
                                    Picker.DismissViewControllerAsync(true);
                                    break;
                                case AVAssetExportSessionStatus.Cancelled:
                                    Console.WriteLine(@"Export canceled");
                                    //Perform any cancel logic
                                    Picker.DismissViewControllerAsync(true);
                                    break;
                                default:
                                    //This should have been a success                                
                                    //File should be saved at filePath
                                    //Upload file from here
                                    var url = fileService.GetFullPath(exportSession.OutputUrl.ToString());
                                    SendMedia(url, ChatMessageType.Video);
                                    break;
                            }
                        });
                    }

这在日志中显示:

System.NotSupportedException: Specified method is not supported.
  at Mono.Debugger.Soft.VirtualMachine.EncodeValue (Mono.Debugger.Soft.Value v, System.Collections.Generic.List`1[T] duplicates) [0x000df] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:686 
  at Mono.Debugger.Soft.VirtualMachine.EncodeValues (System.Collections.Generic.IList`1[T] values, System.Collections.Generic.List`1[T] duplicates) [0x00010] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:693 
  at Mono.Debugger.Soft.ObjectMirror.BeginInvokeMethod (Mono.Debugger.Soft.VirtualMachine vm, Mono.Debugger.Soft.ThreadMirror thread, Mono.Debugger.Soft.MethodMirror method, Mono.Debugger.Soft.Value this_obj, System.Collections.Generic.IList`1[T] arguments, Mono.Debugger.Soft.InvokeOptions options, System.AsyncCallback callback, System.Object state) [0x00094] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/ObjectMirror.cs:311 
  at Mono.Debugger.Soft.ObjectMirror.InvokeMethod (Mono.Debugger.Soft.VirtualMachine vm, Mono.Debugger.Soft.ThreadMirror thread, Mono.Debugger.Soft.MethodMirror method, Mono.Debugger.Soft.Value this_obj, System.Collections.Generic.IList`1[T] arguments, Mono.Debugger.Soft.InvokeOptions options) [0x00000] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/ObjectMirror.cs:387 
  at Mono.Debugger.Soft.TypeMirror.NewInstance (Mono.Debugger.Soft.ThreadMirror thread, Mono.Debugger.Soft.MethodMirror method, System.Collections.Generic.IList`1[T] arguments, Mono.Debugger.Soft.InvokeOptions options) [0x00026] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/TypeMirror.cs:837 
  at Mono.Debugger.Soft.TypeMirror.NewInstance (Mono.Debugger.Soft.ThreadMirror thread, Mono.Debugger.Soft.MethodMirror method, System.Collections.Generic.IList`1[T] arguments) [0x00000] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugger.Soft/Mono.Debugger.Soft/TypeMirror.cs:827 
  at Mono.Debugging.Soft.SoftDebuggerAdaptor.CreateValue (Mono.Debugging.Evaluation.EvaluationContext ctx, System.Object type, System.Object[] argValues) [0x0019e] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugging.Soft/SoftDebuggerAdaptor.cs:524 
  at Mono.Debugging.Evaluation.NRefactoryExpressionEvaluatorVisitor.VisitObjectCreateExpression (ICSharpCode.NRefactory.CSharp.ObjectCreateExpression objectCreateExpression) [0x0005e] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugging/Mono.Debugging.Evaluation/NRefactoryExpressionEvaluatorVisitor.cs:1081 
  at ICSharpCode.NRefactory.CSharp.ObjectCreateExpression.AcceptVisitor[T] (ICSharpCode.NRefactory.CSharp.IAstVisitor`1[S] visitor) [0x00000] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/ObjectCreateExpression.cs:90 
  at Mono.Debugging.Evaluation.NRefactoryExpressionEvaluator.Evaluate (Mono.Debugging.Evaluation.EvaluationContext ctx, System.String expression, System.Object expectedType) [0x00157] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugging/Mono.Debugging.Evaluation/NRefactoryExpressionEvaluator.cs:82 
  at Mono.Debugging.Evaluation.ExpressionEvaluator.Evaluate (Mono.Debugging.Evaluation.EvaluationContext ctx, System.String exp) [0x00000] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugging/Mono.Debugging.Evaluation/ExpressionEvaluator.cs:43 
  at Mono.Debugging.Evaluation.ObjectValueAdaptor.GetExpressionValue (Mono.Debugging.Evaluation.EvaluationContext ctx, System.String exp) [0x00000] in /Users/vsts/agent/2.153.2/work/1/s/monodevelop/main/external/debugger-libs/Mono.Debugging/Mono.Debugging.Evaluation/ObjectValueAdaptor.cs:1416 Thread started:  #13
Thread finished: <Thread Pool> #6
2019-07-10 10:14:25.501 MyApp[319:18031] Export failed: The operation could not be completed

1 个答案:

答案 0 :(得分:0)

据我所知,由于iPhone拥有的视频类型为MOV,因此会出现此问题,但是当您在Android上播放该视频类型时,由于默认情况下它不支持Android,因此您需要像VLC这样的视频播放器为它工作。如果您问我,解决此问题的最佳方法是视频转换,可以在Xamarin iOS中使用AVAssetExportSession

您所做的是这样的:

        var asset = AVAsset.FromUrl( NSUrl.FromFilename( sourceFilePath ) )

        AVAssetExportSession export = new AVAssetExportSession (asset, AVAssetExportSession.PresetLowQuality );

        export.OutputUrl = new NSUrl ( downloadFilePath );
        export.OutputFileType = AVFileType.Mpeg4;
        export.ShouldOptimizeForNetworkUse = true;

        export.ExportAsynchronously(  ( ) => 
        {
            System.Diagnostics.Debug.WriteLine( export.Error.LocalizedDescription );
        });

您可以检查此Xamarin Forums discussion以获得更多详细信息。

您还可以检查此StackOverflow问题,该问题处理类似MOV to Mp4 video conversion iPhone programmatically