我试着用Audio Toolbox和Audio Queues播放一个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;
}
}发布于 2012-07-12 01:04:49
哦,是的,那是2010年,那段可怕的日子里,你不得不自己缓冲样本来播放一个奇怪的音频文件……现在,如果你只想播放一个音频文件,AVAudioPlayer是你的好朋友。
NSError *error = nil;
AVAudioPlayer *myMP3File = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MyFile.mp3",
[[NSBundle mainBundle] resourcePath]]] error:&error];
[myMP3File play];
if(error != nil) NSLog(@"Error mp3 file: %@",error); 这是一个不错的tutorial for AVAudioPlayer。
发布于 2013-06-20 20:38:04
试试这个:
首先将此框架导入到文件AVFoundation/AVFoundation.h中
然后声明一个AVAudioPlayer实例。AVAudioPlayer *音频播放器;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file"
ofType:@"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];https://stackoverflow.com/questions/4018622
复制相似问题