音频工具箱播放mp3文件(iOS)

时间:2010-10-25 20:33:43

标签: audio ios queue mp3 toolbox

我试图用音频工具箱和音频队列播放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;

    }

}

2 个答案:

答案 0 :(得分:0)

哦,是的,那是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

答案 1 :(得分:0)

试试这个:

首先将此框架导入您的文件AVFoundation / AVFoundation.h

然后声明AVAudioPlayer的一个实例。 AVAudioPlayer * audioPlayer;

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];