2014-04-28 46 views
0

我正在开发一个插件来录制Droid,Touch和Phone项目的音频。我在.Droid项目中做到了,它非常完美,工作率达到100%。插件录制音频

在.Touch中,我必须实现我的ViewController,并且这个将录制音频,并且很快完成,它必须返回mediaFile。

什么我迄今所做的是:

插件接口

public interface IMvxAudioChooserService 
{ 
    void RecordAudio(Action<Stream> audioAvailable, Action assumeCancelled); 
} 

插件触摸实现

​​

MvxAudioRecorderViewController

public partial class MvxRecordAudioViewController : MvxViewController 
{ 
    private AVAudioRecorder audioRecorder; 
    private NSDictionary mediaSettings; 
    private NSError audioError = new NSError(); 
    private NSUrl mediaURL; 
    private Stopwatch stopWatch; 
    public string FolderName; 
    public EventHandler<MvxAudioRecorderEventArgs> AudioAvailable; 


    public MvxRecordAudioViewController() : base ("MvxRecordAudioViewController", null) 
    { 
     SetDictionarySettingsForAudio(); 
    } 


    private void SetDictionarySettingsForAudio() 
    { 
     //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary 
     NSObject[] keys = new NSObject[] 
     { 
      AVAudioSettings.AVSampleRateKey, 
      AVAudioSettings.AVFormatIDKey, 
      AVAudioSettings.AVNumberOfChannelsKey, 
      AVAudioSettings.AVLinearPCMBitDepthKey, 
      AVAudioSettings.AVLinearPCMIsBigEndianKey, 
      AVAudioSettings.AVLinearPCMIsFloatKey 
     }; 

     NSObject[] values = new NSObject[] 
     { 
      NSNumber.FromFloat (44100.0f), //Sample Rate 
      NSNumber.FromInt32 ((int)MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC), //AVFormat 
      NSNumber.FromInt32 (2), //Channels 
      NSNumber.FromInt32 (16), //PCMBitDepth 
      NSNumber.FromBoolean (false), //IsBigEndianKey 
      NSNumber.FromBoolean (false) //IsFloatKey 
     }; 

     mediaSettings = NSDictionary.FromObjectsAndKeys (values, keys); 
    } 

    public override void DidReceiveMemoryWarning() 
    { 
     // Releases the view if it doesn't have a superview. 
     base.DidReceiveMemoryWarning(); 

     // Release any cached data, images, etc that aren't in use. 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     // Perform any additional setup after loading the view, typically from a nib. 
    } 

    partial void Record (MonoTouch.Foundation.NSObject sender) 
    { 
     var session = AVAudioSession.SharedInstance(); 

     NSError error = null; 
     session.SetCategory(AVAudioSession.CategoryRecord, out error); 
     if(error != null) 
     { 
      Console.WriteLine(error); 
      return; 
     } 

     session.SetActive(true, out error); 
     if(error != null) 
     { 
      Console.WriteLine(error); 
      return; 
     } 

     if(!PrepareAudioRecording()) 
     { 
      isRecording.Text = "Error preparing"; 
      return; 
     } 

     if(!audioRecorder.Record()) 
     { 
      isRecording.Text = "Error preparing"; 
      return; 
     } 

     this.stopWatch = new Stopwatch(); 
     this.stopWatch.Start(); 
     this.isRecording.Text = "Recording"; 
     this.recordButton.Enabled = false; 
     this.stopButton.Enabled = true; 
    } 

    partial void Stop (MonoTouch.Foundation.NSObject sender) 
    { 
     audioRecorder.Stop(); 
     AudioAvailable.Invoke (this, new MvxAudioRecorderEventArgs(this.mediaURL)); 
     DismissViewController (true,() => {}); 
    } 

    bool PrepareAudioRecording() 
    { 
     //Declare string for application temp path and tack on the file extension 
     this.mediaURL = NSUrl.FromFilename(NSBundle.MainBundle.BundlePath + ((FolderName != "") ? "/" + FolderName : "") + "/AUD" + Guid.NewGuid() + ".aac"); 

     //Set recorder parameters 
     NSError error; 
     audioRecorder = AVAudioRecorder.ToUrl(mediaURL, mediaSettings, out error); 
     if((audioRecorder == null) || (error != null)) 
     { 
      Console.WriteLine(error); 
      return false; 
     } 

     //Set Recorder to Prepare To Record 
     if(!audioRecorder.PrepareToRecord()) 
     { 
      audioRecorder.Dispose(); 
      audioRecorder = null; 
      return false; 
     } 

     audioRecorder.FinishedRecording += delegate (object sender, AVStatusEventArgs e) { 
      audioRecorder.Dispose(); 
      audioRecorder = null; 
      Console.WriteLine("Done Recording (status: {0})", e.Status); 
     }; 

     return true; 
    } 

    public class MvxAudioRecorderEventArgs : EventArgs 
    { 
     public NSUrl MediaUrl; 

     public MvxAudioRecorderEventArgs(NSUrl mediaUrl) 
     { 
      this.MediaUrl = mediaUrl; 
     } 
    } 
} 

但随后,调用audioService.RecordAudio()的时候,它是不开放的视图 - 控制,并且在输出它跟踪:问候

Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction 

感谢, 加布里埃尔

回答

0

由于您的记录ViewController没有使用任何绑定,并没有ViewModel,那么为什么不从UIViewController而不是MvxViewController继承 - 这将意味着MvvmCross不会为它查找MvxViewModelRequest或ViewModel。