【问题标题】:AVAudioRecorder on Mac: The operation couldn't be completedMac 上的 AVAudioRecorder:操作无法完成
【发布时间】: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


    【解决方案1】:

    由于某种原因,AVAudioRecorder 在使用文件 URL 进行录制时需要正确的扩展名。

    因此,如果您在设置 URL 的两行中将 .mp3 更改为 .ima4,那么您的示例将不会出错。

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 2019-05-27
      • 2013-10-31
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多