gpt4 book ai didi

avaudioengine - AVAudioFile 无法在 AVAudioEngine 中播放

转载 作者:行者123 更新时间:2023-12-05 00:55:16 28 4
gpt4 key购买 nike

我正在尝试使用 AVAudioEngine 播放 AVAudioFile。代码大部分取自苹果开发者在线视频,但没有回放。花了一些时间浏览论坛,但似乎没有任何线索。

我有两种方法。第一个调用标准的打开文件对话框,打开音频文件,分配一个 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]) {

NSLog(@"problem filling buffer");

}
else
playOrigSoundFileButton.enabled = true;

}

}];}





- (IBAction)playSoundFileAudioData:(id)sender {



AVAudioEngine *engine = [[AVAudioEngine alloc] init]; // set up the audio engine

AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init]; // set up a player node

[engine attachNode:player]; // attach player node to engine

AVAudioMixerNode *mixer = [engine mainMixerNode];
[engine connect:player to:mixer format:theAudioFile.processingFormat]; // connect player node to mixer
[engine connect:player to:mixer format:[mixer outputFormatForBus:0]]; // connect player node to mixer

NSError *error;

if (![engine startAndReturnError:&error]) {
NSLog(@"Problem starting engine ");
}
else {

[player scheduleFile:theAudioFile atTime: nil completionHandler:nil];
[player play];
}

}

我已经检查了函数是否正在执行,音频引擎正在运行并且音频文件已被读取。我也尝试过不同的音频文件格式,包括单声道和立体声。有任何想法吗?

我正在运行 Xcode 7.3.1。

最佳答案

申报您的 AVAudioPlayerNode *player作为全局

引用此链接 Can't play file from documents in AVAudioPlayer

关于avaudioengine - AVAudioFile 无法在 AVAudioEngine 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334152/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com