2011-11-12 7 views
2

Mac OS X用のシンプルなボイスレコーダーのサンプルコードはありますか?私は自分の声を自分のMacBook Proの内蔵マイクから録音してファイルに保存したいと考えています。それだけです。Mac OS Xシンプルなボイスレコーダー

私は何時間も探しています。音声を録音してhttp://developer.apple.com/library/mac/#samplecode/MYRecorder/Introduction/Intro.htmlなどのファイルに保存するいくつかの例があります。 Mac OS Xのサンプルコードは、iPhoneの同様のサンプルコードよりも約10倍複雑です。私はiPhone版と同じくらい簡単になりますのMac OS Xのためにこれを行うためのコードがあると思い

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]; 

:iOS用

のコマンドは同じくらい簡単です。ご協力ありがとうございました。ここで

コード(現在プレイヤーが動作しません)

#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];