我正在开发一个在线广播应用程序,我设法使用AudioQueueServices播放来自Icecast服务器的流式mp3数据包,我正在努力实现一个录制功能。
由于流是mp3格式的,所以我不能使用AudioFileWritePackets将音频数据包直接写入文件。
为了利用扩展音频的自动转换,我使用ExtAudioWriteFile来写入wav文件。我已经使用FileStreamOpen回调函数AudioFileStream_PropertyListenerProc和目标格式设置了传入数据包的AudioStreamBasicDescription。我填充了manually.The代码,成功地创建了文件并将数据包写入其中,但在回放时,我听到的是白噪声;以下是我的代码
// when the recording button is pressed this function creates the file and setup the asbd
-(void)startRecording{
recording = true;
OSStatus status;
NSURL *baseUrl=[self applicationDocumentsDirectory];//returns the document direcotry of the app
NSURL *audioUrl = [NSURL URLWithString:@"Recorded.wav" relativeToURL:baseUrl];
//asbd setup for the destination file/wav file
AudioStreamBasicDescription dstFormat;
dstFormat.mSampleRate=44100.0;
dstFormat.mFormatID=kAudioFormatLinearPCM; dstFormat.mFormatFlags=kAudioFormatFlagsNativeEndian|kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
dstFormat.mBytesPerPacket=4;
dstFormat.mBytesPerFrame=4;
dstFormat.mFramesPerPacket=1;
dstFormat.mChannelsPerFrame=2;
dstFormat.mBitsPerChannel=16;
dstFormat.mReserved=0;
//creating the file
status = ExtAudioFileCreateWithURL(CFBridgingRetain(audioUrl), kAudioFileWAVEType, &(dstFormat), NULL, kAudioFileFlags_EraseFile, &recordingFilRef);
// tell the EXtAudio File ApI what format we will be sending samples
//recordasbd is the asbd of incoming packets populated in AudioFileStream_PropertyListenerProc
status = ExtAudioFileSetProperty(recordingFilRef, kExtAudioFileProperty_ClientDataFormat, sizeof(recordasbd), &recordasbd);
}
// a handler called by packetproc call back function in AudiofileStreamOpen
- (void)handlePacketsProc:(const void *)inInputData numberBytes:(UInt32)inNumberBytes numberPackets:(UInt32)inNumberPackets packetDescriptions:(AudioStreamPacketDescription *)inPacketDescriptions {
if(recording){
// wrap the destination buffer in an audiobuffer list
convertedData.mNumberBuffers= 1;
convertedData.mBuffers[0].mNumberChannels = recordasbd.mChannelsPerFrame;
convertedData.mBuffers[0].mDataByteSize = inNumberBytes;
convertedData.mBuffers[0].mData = inInputData;
ExtAudioFileWrite(recordingFilRef,recordasbd.mFramesPerPacket * inNumberPackets, &convertedData);
}
}我的问题是:
我的方法正确吗?我可以以这种方式将mp3数据包写入wav文件吗?如果是,我错过了什么??
如果我的方法是错的,请告诉我你认为正确的任何其他方法。朝正确的方向推动对我来说已经足够了
我非常感谢我读过的每一个问题,所以我可以在这个主题上动手,我也仔细查看了苹果转换文件的例子,但我不能弄清楚我错过了什么,提前感谢任何帮助
发布于 2017-05-07 19:08:18
为什么不将原始mp3数据包直接写入文件?根本不使用ExtAudioFile。
它们将形成有效的mp3文件,并且将比等效的wav文件小得多。
https://stackoverflow.com/questions/43830424
复制相似问题