AVAudioEngineを使用してAVAudioFileを再生しようとしています。このコードは主にアップルのデベロッパーのオンラインビデオから取り込まれていますが、再生はありません。フォーラムを通るのに少し時間を費やしましたが、何もその上に光を投げかけていないようです。AVAudioFileがAVAudioEngineで再生されない
私には2つの方法があります。最初のものは、標準のオープンファイルダイアログを呼び出し、オーディオファイルを開き、AVAudioBufferオブジェクト(後で使用する)を割り当てて、オーディオデータで埋めます。 2番目はAVAudioEngineオブジェクトとAVAudioPlayerNodeオブジェクトを設定し、すべてを接続してファイルを再生します。 2つの方法を以下に示します。
- (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を実行しています。
はわずか2行のいずれかを開始(私はplaySoundFileAudioData方法で余分ラインに残してきたことに気づい参照してください「エンジンの接続:」しかし、。 – aseago