Mac上的AVAudioRecorder:操作无法完成

时间:2015-12-10 03:33:59

标签: objective-c macos avaudiorecorder

我希望我现在正确地问这个问题,我之前曾在这里提出过要求,但我希望现在能够非常精确。我在这里找不到合适的答案, iOS解决方案,但不适用于OSX / Mac应用程序

我正在努力让AVAudioRecorder正常工作。这不是iOS应用程序,因此我无法使用UIKit,AVAudioSession等。

//
//  AppDelegate.m
//  NeuerAudioShit
//
//  Created by dominik said on 10/12/2015.
//  Copyright © 2015 dominik said. All rights reserved.
//

#import "AppDelegate.h"


@interface AppDelegate ()
@property (weak) IBOutlet NSButton *playButton;
@property (weak) IBOutlet NSButton *button;
@property (weak) IBOutlet NSWindow *window;
@property (strong) AVAudioRecorder* audioRecorder;
@property (strong) AVAudioPlayer* player;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

- (IBAction)playRecording:(id)sender {

    NSURL *url = [NSURL URLWithString:@"/Users/dominik/Coding/file.mp3"];


    _player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    [_player setVolume: 1.0];
    [_player play];
}
- (IBAction)buttonPressed:(id)sender {


    NSLog(@"Hello World");
    NSURL *url = [NSURL URLWithString:@"/Users/dominik/Coding/file.mp3"];

    NSDictionary *recordSettings = [NSDictionary
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt: kAudioFormatAppleIMA4],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;
    _audioRecorder = [[AVAudioRecorder alloc]
                      initWithURL:url
                      settings:recordSettings
                      error:&error];
    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    }
    else {
        NSLog(@"jo");
    }

    [_audioRecorder setDelegate:self];

    if ([_audioRecorder prepareToRecord] == YES){
            [_audioRecorder record];
    }else {
        int errorCode = CFSwapInt32HostToBig ([error code]);
        NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);

    }

}



@end

当我调用记录函数时,它在日志中给出了以下输出:

2015-12-10 04:21:39.306 NeuerAudioShit[29099:2206838] Hello World
2015-12-10 04:21:39.699 NeuerAudioShit[29099:2206838] error: The operation couldn’t be completed. (OSStatus error 1718449215.)
2015-12-10 04:21:39.699 NeuerAudioShit[29099:2206838] Error: The operation couldn’t be completed. (OSStatus error 1718449215.) [fmt?])

1 个答案:

答案 0 :(得分:1)

出于某种原因,AVAudioRecorder在使用文件网址进行录制时需要适当的扩展名。

因此,如果您在已设置网址的两行中将.mp3更改为.ima4,那么您的示例将无误地运行。