Created
December 6, 2014 01:24
-
-
Save pa-w/2c0ad6cd1de79a9da1eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.UIKit; | |
using MonoTouch.Foundation; | |
using MonoTouch.AVFoundation; | |
using MonoTouch.CoreAnimation; | |
using MonoTouch.ObjCRuntime; | |
using System.Threading; | |
using System.IO; | |
namespace MyProject { | |
public partial class MyViewController : UIViewController { | |
private AVAudioRecorder recorder; | |
public override void ViewDidLoad () { | |
base.ViewDidLoad (); | |
NSError error; | |
var url = NSUrl.FromFilename ("/dev/nulasdl"); | |
var values = new NSObject[] | |
{ | |
NSNumber.FromFloat ((float)44100), //Sample Rate | |
NSNumber.FromInt32 ((int)MonoTouch.AudioToolbox.AudioFormatType.AppleLossless), //AVFormat | |
NSNumber.FromInt32 (2), | |
NSNumber.FromInt32 ((int)MonoTouch.AVFoundation.AVAudioQuality.Min) | |
}; | |
var keys = new NSObject[] | |
{ | |
AVAudioSettings.AVSampleRateKey, | |
AVAudioSettings.AVFormatIDKey, | |
AVAudioSettings.AVNumberOfChannelsKey, | |
AVAudioSettings.AVEncoderAudioQualityKey | |
}; | |
//var audioSettings = NSDictionary.FromObjectsAndKeys (values, keys); | |
//recorder = new AVAudioRecorder (url, audioSettings, out error); | |
var audioSettings = new AudioSettings (NSDictionary.FromObjectsAndKeys (values, keys)); | |
recorder = AVAudioRecorder.Create(url, audioSettings, out error); | |
if (error == null) { | |
var session = AVAudioSession.SharedInstance (); | |
session.SetCategory (AVAudioSession.CategoryRecord); | |
recorder.PrepareToRecord (); | |
recorder.MeteringEnabled = true; | |
recorder.Record (); | |
CADisplayLink displayLink = CADisplayLink.Create (this, new Selector ("UpdateMetersSelector")); | |
displayLink.AddToRunLoop (NSRunLoop.Current, NSRunLoop.NSRunLoopCommonModes); | |
} else { | |
Console.WriteLine ("Error..."); | |
return; | |
} | |
} | |
[Export ("UpdateMetersSelector")] | |
private void UpdateMeters() | |
{ | |
recorder.UpdateMeters(); | |
Console.WriteLine(recorder.AveragePower (0)); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// 24364-xamarinbug | |
// | |
// Created by Alexander Andronov on 28/11/14. | |
// Copyright (c) 2014 Alexander Andronov. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import <AVFoundation/AVFoundation.h> | |
@interface ViewController () | |
@property (nonatomic, strong) AVAudioRecorder *recorder; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSURL *url = [NSURL fileURLWithPath:@"/dev/nulasdsl"]; | |
NSDictionary *settings = @{AVSampleRateKey: [NSNumber numberWithFloat: 44100.0], | |
AVFormatIDKey: [NSNumber numberWithInt: kAudioFormatAppleLossless], | |
AVNumberOfChannelsKey: [NSNumber numberWithInt: 2], | |
AVEncoderAudioQualityKey: [NSNumber numberWithInt: AVAudioQualityMin]}; | |
NSError *error; | |
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; | |
if(error) { | |
NSLog(@"Ups, could not create recorder %@", error); | |
//return; | |
} | |
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; | |
if (error) { | |
NSLog(@"Error setting category: %@", [error description]); | |
} | |
[self.recorder prepareToRecord]; | |
[self.recorder setMeteringEnabled:YES]; | |
[self.recorder record]; | |
CADisplayLink *displaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateMeters)]; | |
[displaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; | |
} | |
- (void)updateMeters | |
{ | |
[self.recorder updateMeters]; | |
float power = [self.recorder averagePowerForChannel:0]; | |
NSLog([NSString stringWithFormat: @"%f", power]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment