【问题标题】:GetAudio Plugin for MvvmCross用于 MvvmCross 的 GetAudio 插件
【发布时间】:2014-07-20 19:39:44
【问题描述】:

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

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

嗯,到目前为止我有:

        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)}

谁能帮帮我? 谢谢,

【问题讨论】:

  • 您在播放远程内容吗?它可能与:stackoverflow.com/questions/4101380/… 相关,其中 AVURLAsset 在使用特定资源路径时失败。也许这是一个线索。
  • 嘿cheesebaron,不是真的。该文件已在设备内。它位于本地的音乐应用程序中。它在代码中说 .mov ,只是因为它是我在此处粘贴的链接中的尝试。但我正在尝试的文件是来自 MusicPlayerChooser 的 mp3。有什么线索吗?
  • 已编辑解决方案...
  • 将解决方案作为答案并将其标记为已回答。

标签: ios plugins touch mvvmcross exporter


【解决方案1】:

找到了解决办法。正如我认为的错误是在 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多