ios - 使用 Monotouch 修剪视频失败并显示 "The operation could not be completed"

标签 ios xamarin xamarin.ios avassetexportsession avasset

我正在尝试以编程方式将视频修剪为 5 秒。这是我的实现。

AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,AVAssetExportSession.PresetLowQuality.ToString());
            int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
            string filename;
            if (SystemVersion >= 8)
            {
                var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
                filename = Path.Combine(documents, "trimmed.mov");
            }
            else
            {
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
                filename = Path.Combine(documents, "trimmed.mov");
            }
            outputUrl=new NSUrl(filename);
            exportSession.OutputUrl = outputUrl;

            CMTime start = new CMTime((long)1, 1);

            CMTime duration = new CMTime((long)5, 1);

            CMTimeRange range = new CMTimeRange();
            range.Start=start;
            range.Duration=duration;
            exportSession.TimeRange = range;

            exportSession.OutputFileType = AVFileType.QuickTimeMovie;
            ExportTrimmedVideo( exportSession);
async void ExportTrimmedVideo(AVAssetExportSession exportSession)
    {
        await exportSession.ExportTaskAsync ();
        if (exportSession.Status == AVAssetExportSessionStatus.Completed) {
            InvokeOnMainThread (() => {
                new UIAlertView ("Export Sucess", "Video is trimmed", null, "O K").Show ();
            });
        }
        else 
        {
            InvokeOnMainThread (() => {
                new UIAlertView ("Export Falure", exportSession.Error.Description, null, "O K").Show ();
            });
        }
}

但完成后我将获得已归档状态。完整的NSError描述如下

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x7cebcf80 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x7cb08410 "The operation couldn’t be completed. (OSStatus error -12105.)", NSLocalizedFailureReason=An unknown error occurred (-12105)}

我可能做错了什么?

编辑

我已经提到了苹果的documentation修剪视频并修改了上面的代码,但没有任何积极效果,如下所示。

var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
            var preset="";
            if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
            {
                preset="AVAssetExportPresetLowQuality";
            }
            else
            {
                preset=compatiblePresets.FirstOrDefault();
            }
            AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,preset);
            int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
            string filename;
            if (SystemVersion >= 8)
            {
                var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
                filename = Path.Combine(documents, "trimmed.mov");
            }
            else
            {
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
                filename = Path.Combine(documents, "trimmed.mov");
            }
            outputUrl=new NSUrl(filename);
            exportSession.OutputUrl = outputUrl;
            exportSession.OutputFileType = AVFileType.QuickTimeMovie;
            CMTime start = new CMTime((long)1, 600);

            CMTime duration = new CMTime((long)5, 600);

            CMTimeRange range = new CMTimeRange();
            range.Start=start;
            range.Duration=duration;
            exportSession.TimeRange = range;


            ExportTrimmedVideo( exportSession);

最佳答案

试试下面的代码。我修改了 exportSession.OutputUrl 以及您如何初始化 CMTimeRange。你要把它剪成 4 秒的片段吗?

var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
var preset="";

if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
{
    preset="AVAssetExportPresetLowQuality";
}
else
{
    preset=compatiblePresets.FirstOrDefault();
}

using (var exportSession = new AVAssetExportSession(videoAsset, preset))
{
    int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
    string filename;
    if (SystemVersion >= 8)
    {
        var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
        filename = Path.Combine(documents, "trimmed.mov");
    }
    else
    {
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
        filename = Path.Combine(documents, "trimmed.mov");
    }

    exportSession.OutputUrl = NSUrl.FromFilename(filename);
    exportSession.OutputFileType = AVFileType.QuickTimeMovie;

    var range = new CMTimeRange();
    range.Start = CMTime.FromSeconds (1, videoAsset.Duration.TimeScale);
    range.Duration = CMTime.FromSeconds (5, videoAsset.Duration.TimeScale);
    exportSession.TimeRange = range;
}

ExportTrimmedVideo( exportSession);

关于ios - 使用 Monotouch 修剪视频失败并显示 "The operation could not be completed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543295/

相关文章:

c# - 尝试使用 ConfuserEx 混淆 Xamarin.android dll 时出错

azure - Xamarin.iOS 无法在 iOS 13.1.2 中向 Azure 通知中心注册

Xamarin Assets 与可绘制资源

ios - 在 iOS 应用程序中报告事件的最佳实践

IOS:如何填充 Nib 内的 UITableView?

xamarin - 无法确定 Xamarin.Android 中 native 库的 abi

xaml - 如何建议从 Xamarin.Forms XAML 中的隐式样式继承?

C# Monotouch/Xamarin.iOS - AVPlayer 设置音量

ios - 使用未声明的标识符 'startCameraControllerFromViewController'

ios - 为什么 UIRefreshControl 会跳?