3

我从流中接收 mp3 并转换 pcm 数据。我尝试使用 AVAudioEngine 但无法在没有文件的情况下创建 AVAudioPCMBuffer。

- (void)playAudio:(NSNotification *)notification {
    self.runAudioThread = YES;
    dispatch_queue_t globalDispatchDefaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

    [_player play];

    dispatch_async(globalDispatchDefaultQueue, ^{
        while (_runAudioThread) {
            if (_rawDataArray.count > BUFFER_COUNT) {
                NSMutableData *rawData = [[NSMutableData alloc] init];
                NSArray *rawDataArray = _rawDataArray.copy;

                @synchronized(_rawDataArray) {
                    [self.rawDataArray removeAllObjects];
                }

                for (AudioPacketInfo *audioPacketInfo in rawDataArray) {
                    [rawData appendData:audioPacketInfo.data];
                }

                NSData *mp3Data = [AudioUtil makeMP3Data:rawData];
                if (mp3Data) {
                    NSData *pcmData = [AudioUtil convertToPCMWithData:mp3Data];
                    if (pcmData.length > 0) {
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = paths[0];
                        NSString *filePath = [documentsDirectory stringByAppendingString:@"/temp.mp3"];
                        [[NSFileManager defaultManager] createFileAtPath:filePath contents:mp3Data attributes:nil];

                        AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:[NSURL URLWithString:filePath] error:nil];
                        AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioFile.processingFormat frameCapacity:(AVAudioFrameCount)audioFile.length];

                        [audioFile readIntoBuffer:buffer error:nil];
                        [_player scheduleBuffer:buffer completionHandler:nil];
                    }
                }
            }
        }
    });
}

我该怎么做?

将 mp3 转换为 pcm

4

0 回答 0