【问题标题】:Audio Toolbox playing an mp3 file (iOS)播放 mp3 文件的音频工具箱 (iOS)
【发布时间】:2011-04-30 10:29:40
【问题描述】:

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

    }

}

【问题讨论】:

    标签: audio ios queue mp3 toolbox


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个:

      首先将此框架导入到您的文件 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多