1

我正在为 MvvmCross 开发一个从设备到 GetAudio 的插件。但是我在 Touch 实现中遇到了错误。我已经看过这里这里这里

但没有什么能解决我的问题。

好吧,到目前为止我有:

        var audioDelegate = new AudioDelegate();
        audioDelegate.AudioAvailable = ProcessAudio;
        mediaPicker = new MPMediaPickerController(); 
        mediaPicker.Delegate = audioDelegate;
        mediaPicker.AllowsPickingMultipleItems = false;
        modalHost.PresentModalViewController (mediaPicker, true);

要从音频启动选择器,其中 AudioDelegate 是:

    private class AudioDelegate : MPMediaPickerControllerDelegate
    {
        public EventHandler<MvxAudioRecorderEventArgs> AudioAvailable;

        public override void MediaItemsPicked (MPMediaPickerController sender, MPMediaItemCollection mediaItemCollection)
        {
            if (mediaItemCollection.Count < 1) 
            {
                return;
            }
            MvxAudioRecorderEventArgs eventArg = new MvxAudioRecorderEventArgs (mediaItemCollection.Items [0].AssetURL);
            AudioAvailable (this, eventArg);
        }
    }

然后在 ProcessAudio 中:

    private void ProcessMedia(object sender, UIImagePickerMediaPickedEventArgs e)
    {
        var assetURL = e.MediaUrl;
        NSDictionary dictionary = null;
        var assetExtension = e.MediaUrl.Path.Split ('.') [1];

        var songAsset = new AVUrlAsset (assetURL, dictionary);
        var exporter = new AVAssetExportSession (songAsset, AVAssetExportSession.PresetPassthrough.ToString ());
        exporter.OutputFileType = (assetExtension == "mp3") ? "com.apple.quicktime-movie" : AVFileType.AppleM4A;
        var manager = new NSFileManager ();

        var count = 0;
        string filePath = null;
        do 
        {
            var extension = "mov";//( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
            var fileNameNoExtension = "AUD_" + Guid.NewGuid ().ToString ();
            var fileName = string.Format ("{0}({1})", fileNameNoExtension, count);
            filePath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) + "/";
            filePath = filePath + fileName + "." + extension;
            count++;

        } while (manager.FileExists (filePath));

        var outputURL = new NSUrl (filePath);//should be something in objective C... => [NSURL fileURLWithPath:filePath];

        exporter.OutputUrl = outputURL;


        exporter.ExportAsynchronously ( () =>
            {
                var exportStatus = exporter.Status;
                switch (exportStatus) 
                {
                    case AVAssetExportSessionStatus.Unknown:
                    case AVAssetExportSessionStatus.Completed: 
                    {
                        //remove the .mov from file and make it available for callback mediaAvailable()...
                        break;
                    }
                    case AVAssetExportSessionStatus.Waiting:
                    case AVAssetExportSessionStatus.Cancelled:
                    case AVAssetExportSessionStatus.Exporting:
                    case AVAssetExportSessionStatus.Failed: 
                    default:
                    {
                        var exportError = exporter.Error;
                        if(assumeCancelled != null)
                        {
                            assumeCancelled();
                        }
                        break;
                    }
                }
            });


        mediaPicker.DismissViewController(true, () => { });
        modalHost.NativeModalViewControllerDisappearedOnItsOwn();
    }

但它总是与 Status.Failed 错误:

        LocalizedDescription: The operation could not be completed

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

谁能帮我?在此表示感谢,

4

1 回答 1

0

找到了解决方案。正如我认为的错误是在 outputURL 文件中。

将其更改为:

        var count = 0;
        string filePath = null;
        do 
        {
            var extension = "mp3.mov";//( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
            var fileNameNoExtension = "AUD_" + Guid.NewGuid ().ToString ();
            var fileName = (count == 0) ? fileNameNoExtension : string.Format ("{0}({1})", fileNameNoExtension, count);
            filePath = NSBundle.MainBundle.BundlePath + "/../tmp/" + fileName; /* HERE WAS THE FIRST PROBLEM, USE NSBUNDLE... */
            filePath = filePath + fileName + "." + extension;
            count++;

        } while (manager.FileExists (filePath));
        var outputURL = NSUrl.FromFilename(filePath); /* HERE WAAS THE SECOND PROBLEM, CREATE IT WITH FROMFILENAME INSTEAD OF NEW... */

然后在导出中,只需删除 .mov 扩展名...

        var withoutMOVPath = outputURL.Path.Remove(outputURL.Path.Length - 4);
        NSError error = null;
        manager.Move (outputURL.Path, withoutMOVPath, out error);

            if(error != null && assumeCancelled != null)
            {
                assumeCancelled();
                return;
            }

        var mediaStream = new FileStream (withoutMOVPath, FileMode.Open);
        mediaAvailable (mediaStream);
        break;
于 2014-06-18T16:14:18.683 回答