小编ase*_*go 的帖子

AVAudioFile无法在AVAudioEngine中播放

我正在尝试使用AVAudioEngine播放AVAudioFile。该代码主要是从Apple Developer在线视频中获取和改编的,但是没有回放。已经花了一些时间浏览这些论坛,但是似乎没有任何启示。

我有两种方法。第一个调用标准打开文件对话框,打开音频文件,分配一个AVAudioBuffer对象(稍后将使用)并用音频数据填充它。第二个设置AVAudioEngine和AVAudioPlayerNode对象,连接所有内容并播放文件。下面列出了这两种方法。

- (IBAction)getSoundFileAudioData:(id)sender {


    NSOpenPanel* openPanel = [NSOpenPanel openPanel];

    openPanel.title = @"Choose a .caf file";
    openPanel.showsResizeIndicator = YES;
    openPanel.showsHiddenFiles = NO;
    openPanel.canChooseDirectories = NO;
    openPanel.canCreateDirectories = YES;
    openPanel.allowsMultipleSelection = NO;
    openPanel.allowedFileTypes = @[@"caf"];

    [openPanel beginWithCompletionHandler:^(NSInteger result){
       if (result == NSFileHandlingPanelOKButton) {
        NSURL*  theAudioFileURL = [[openPanel URLs] objectAtIndex:0];

        // Open  the document.

        theAudioFile = [[AVAudioFile alloc] initForReading:theAudioFileURL error:nil];

        AVAudioFormat *format = theAudioFile.processingFormat;
        AVAudioFrameCount capacity = (AVAudioFrameCount)theAudioFile.length;

        theAudioBuffer = [[AVAudioPCMBuffer alloc] 
             initWithPCMFormat:format frameCapacity:capacity];
        NSError *error;
        if (![theAudioFile readIntoBuffer:theAudioBuffer error:&error])  { …
Run Code Online (Sandbox Code Playgroud)

avaudioengine avaudiopcmbuffer avaudiofile

4
推荐指数
1
解决办法
685
查看次数