【发布时间】:2015-12-10 03:33:58
【问题描述】:
我希望我现在问这个问题是正确的,我之前在这里问过并且被否决了很多,但我希望现在非常准确。我还没有在这里找到可行的答案,仅限 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?])
【问题讨论】:
标签: objective-c macos avaudiorecorder