我尝试使用音频工具箱和音频队列播放 mp3 文件,但无论我尝试什么,我都没有播放任何东西(无论是在模拟器中还是在设备上)。当我在模拟器中运行它时,它告诉我:
"Prime 失败 (-66674);将停止 (0/0 帧)",
所以我认为某处有一个基本错误,可能在 AudioEnqueueBuffer 函数中,因为之后队列中似乎没有任何内容。当我询问状态时,我总是得到0。
很抱歉我发布了这么多代码,但我只是不确定错误在哪里。
请帮我。
- (void)startPlayback{
NSString *fileString = [[NSBundle mainBundle] pathForResource:@"musik" ofType:@"mp3"];
const char *filePathAsCString = [fileString UTF8String];
CFURLRef fileURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8*)filePathAsCString, strlen(filePathAsCString), false);
playState.currentPacket = 0;
playState.dataFormat->mSampleRate = kAudioStreamAnyRate;
playState.dataFormat->mFormatID = kAudioFormatMPEGLayer3;
playState.dataFormat->mFramesPerPacket = 0;
playState.dataFormat->mChannelsPerFrame = 2;
playState.dataFormat->mBytesPerFrame = 0;
playState.dataFormat->mBytesPerPacket = 0;
playState.dataFormat->mBitsPerChannel = 0;
playState.dataFormat->mReserved = 0;
playState.dataFormat->mFormatFlags = 0;;
OSStatus status;
status = AudioFileOpenURL(fileURL, kAudioFileReadPermission, kAudioFileMP3Type, &playState.audioFile);
if(status == 0)
{
status = AudioQueueNewOutput(
&playState.dataFormat,
MyAudioOutputCallback,
&playState,
CFRunLoopGetCurrent(),
0,
0,
&playState.queue);
if(status == 0)
{
playState.playing = true;
for(int i = 0; i < 3 && playState.playing; i++)
{
if(playState.playing)
{
AudioQueueAllocateBufferWithPacketDescriptions(playState.queue, 64000, sizeof(AudioStreamPacketDescription)*75, &playState.buffers[i]);
MyAudioOutputCallback(&playState, playState.queue, playState.buffers[i]);
}
}
if(playState.playing)
{
status = AudioQueueStart(playState.queue, NULL);
if(status == 0)
{
NSLog(@"Playing");
}
}
}
}
if(status != 0)
{
NSLog(@"Play failed");
}
}
这里是回调函数:
void MyAudioOutputCallback( void *inUserData,
AudioQueueRef outAQ,
AudioQueueBufferRef outBuffer)
{
PlayState *playState= (PlayState *)inUserData;
UInt32 bytesRead;
UInt32 numPackets = 75;
OSStatus status;
status = AudioFileReadPackets(playState->audioFile, false, &bytesRead, outBuffer->mPacketDescriptions, playState->currentPacket, &numPackets, outBuffer->mAudioData);
if(numPackets)
{
outBuffer->mAudioDataByteSize = bytesRead;
outBuffer->mPacketDescriptionCount = numPackets;
status = AudioQueueEnqueueBuffer(
playState->queue,
outBuffer,
0,
0);
NSLog(@"EnqueueStatus: %d", status);
playState->currentPacket += numPackets;
}
}