使用ExtAudioFileWrite将Streamed mp3数据包的缓冲区写入wav文件ios

问题描述:

我正在使用在线广播应用程序我设法使用AudioQueueServices播放来自Icecast服务器的流式数据包,我正在努力实现录制功能。使用ExtAudioFileWrite将Streamed mp3数据包的缓冲区写入wav文件ios

由于流式传输采用mp3格式,因此无法使用AudioFileWritePackets将音频数据包直接写入文件。

利用扩展音频的自动转换我正在使用ExtAudioWriteFile写入wav文件。我使用FileStreamOpen回调函数AudioFileStream_PropertyListenerProc和我手动填充的目标格式设置了传入数据包的AudioStreamBasicDescription。代码成功创建文件并将数据包写入它,但回放时我听到的是白噪声; 这里是我的代码

// 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文件,如果是这样我失去了什么?

如果我的做法是错了,请告诉我,你认为任何其他方式是在正确的方向轻移right.A是绰绰有余我

我为我读我每次SO问题的任何帮助,非常感谢可以得到我的手在这个话题上,我也密切关注苹果转换文件的例子,但我不知道我是什么想法 预先感谢您的任何帮助

为什么不直接写入原始mp3数据包到文件?根本没有使用ExtAudioFile

他们将形成一个有效的mp3文件,并将比等效的wav文件小得多。

+0

我想写给mp3,但由于版权问题苹果不支持mp3 .AudiofileCreateWithurl返回状态'fmt?'(kAudioConverterErr_FormatNotSupported),这就是为什么我想将它转换为wav。 –

+0

我的意思是使用'fwrite','write'或'NSFileHandle'方法将原始数据包写入文件。 Apple不支持编码mp3数据,原因是他们没有指定,但是由于您已经有mp3数据,因此您不需要编码器。他们支持对它进行解码。 –