2011-11-12 71 views
2

有沒有人有針對Mac OS X的SIMPLE語音記錄器的示例代碼?我只想記錄我的MacBook Pro內置麥克風的聲音並將其保存到文件中。就這些。Mac OS X簡單語音錄音機

我一直在尋找小時,是的,有一些例子會記錄語音並將其保存到文件,如http://developer.apple.com/library/mac/#samplecode/MYRecorder/Introduction/Intro.html。 Mac OS X的示例代碼似乎比iPhone的類似示例代碼複雜10倍左右。

對於iOS的命令是簡單的:

soundFile =[NSURL FileURLWithPath:[tempDir stringByAppendingString:@"mysound.cap"]]; 
soundSetting = [NSDictionary dictionaryWithObjectsAndKeys: // dictionary setting code left out goes here 
soundRecorder = [[AVAudioRecorder alloc] initWithURL:soundFile settings:soundSetting error:nil]; 
[soundRecorder record]; 
[soundRecorder stop]; 

我認爲這是代碼爲Mac OS X的,這將是爲iPhone版一樣簡單做到這一點。感謝您的幫助。

這裏是代碼(目前玩家將無法正常工作)

#import <Foundation/Foundation.h> 
#import <AVFoundation/AVFoundation.h> 

@interface MyAVFoundationClass : NSObject <AVAudioPlayerDelegate> 
{ 
    AVAudioRecorder *soundRecorder; 

} 

@property (retain) AVAudioRecorder *soundRecorder; 

-(IBAction)stopAudio:(id)sender; 
-(IBAction)recordAudio:(id)sender; 
-(IBAction)playAudio:(id)sender; 

@end 


#import "MyAVFoundationClass.h" 

@implementation MyAVFoundationClass 

@synthesize soundRecorder; 

-(void)awakeFromNib 
{ 
    NSLog(@"awakeFromNib visited"); 
    NSString *tempDir; 
    NSURL *soundFile; 
    NSDictionary *soundSetting; 

    tempDir = @"/Users/broncotrojan/Documents/testvoices/"; 
    soundFile = [NSURL fileURLWithPath: [tempDir stringByAppendingString:@"test1.caf"]];  
    NSLog(@"soundFile: %@",soundFile); 

    soundSetting = [NSDictionary dictionaryWithObjectsAndKeys: 
        [NSNumber numberWithFloat: 44100.0],AVSampleRateKey, 
        [NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey, 
        [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
        [NSNumber numberWithInt: AVAudioQualityHigh],AVEncoderAudioQualityKey, nil]; 

    soundRecorder = [[AVAudioRecorder alloc] initWithURL: soundFile settings: soundSetting error: nil]; 
} 

-(IBAction)stopAudio:(id)sender 
{ 
    NSLog(@"stopAudioVisited"); 
    [soundRecorder stop]; 
} 

-(IBAction)recordAudio:(id)sender 
{ 
    NSLog(@"recordAudio Visited"); 
    [soundRecorder record]; 

} 

-(IBAction)playAudio:(id)sender 
{ 
    NSLog(@"playAudio Visited"); 
    NSURL *soundFile; 
    NSString *tempDir; 
    AVAudioPlayer *audioPlayer; 

    tempDir = @"/Users/broncotrojan/Documents/testvoices/"; 
    soundFile = [NSURL fileURLWithPath: [tempDir stringByAppendingString:@"test1.caf"]]; 
    NSLog(@"soundFile: %@", soundFile); 

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil]; 

    [audioPlayer setDelegate:self]; 
    [audioPlayer play]; 

} 

@end 
+0

你找到任何解決方案? – strange

回答

4

AVFoundation框架獅子是新的,是非常相似的IOS版本。這包括AVAudioRecorder。您可以使用iOS中的代碼,只需很少修改或不進行修改。

文檔是here

+1

在Lion之前,QTKit框架做了同樣的事情,但是多了一些代碼。儘管如此,QTKit的基本文檔包含了一個非常簡單(但完整)的示例,即如何在幾個完全解釋的模塊中記錄音頻*和*視頻。快速文檔搜索可以找到兩個路徑並完成兩個示例代碼。 –

+0

我能夠讓錄音機工作,但我似乎無法讓播放器播放我的文件。這裏是我的代碼: –

+0

你能澄清它不工作嗎? 'NSLog()'語句被擊中了嗎? –

0

您的代碼不播放音頻的原因是audioPlayer變量一旦到達方法塊的末尾就會立即釋放。

因此,將以下變量移動到方法塊的外部,然後它會很好地播放音頻。

AVAudioPlayer *audioPlayer; 

順便說一下,您的代碼段對我來說非常有幫助! :d

0

這裏是Mac的片段:

NSDictionary *soundSetting = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat: 44100.0],AVSampleRateKey, 
          [NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey, 
          [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
          [NSNumber numberWithInt: AVAudioQualityHigh],AVEncoderAudioQualityKey, nil]; 

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0]; 
NSURL* audioFileURL = [NSURL fileURLWithPath: [documentsDirectory stringByAppendingString:@"/test.wav"]]; 

NSError* error; 
AVAudioRecorder* soundRecorder = soundRecorder = [[AVAudioRecorder alloc] initWithURL: audioFileURL settings: soundSetting error: &error]; 

if (error) 
{ 
    NSLog(@"Error! soundRecorder initialization failed..."); 
} 

// start recording 
[soundRecorder record];