最近在做iphone上的流媒體播放,需要用到播放音頻流,參考了好多博客、網站,最終算是把這個比較難弄的問題解決了。
這篇文章是播放音頻文件的,我會專門用一篇文章來介紹如何用AudioQueue來播放raw pcm data,相信這是大多數ios開發同胞需要的吧。
在此分享出來,希望能幫助到真正需要的人,畢竟一個人的力量是有限的,還是要共同學習、共同進步。
1.playAudio.h
聲明了一個Objective-C類
//
// playAudio.h
// ffmpegPlayAudio
//
// Created by infomedia xuanyuanchen on 12-3-26.
// Copyright (c) 2012年 xuanyuanchen. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AudioToolbox/AudioFile.h>
#define NUM_BUFFERS 3
@interface playAudio : NSObject{
//播放音頻文件ID
AudioFileID audioFile;
//音頻流描述對象
AudioStreamBasicDescription dataFormat;
//音頻隊列
AudioQueueRef queue;
SInt64 packetIndex;
UInt32 numPacketsToRead;
UInt32 bufferByteSize;
AudioStreamPacketDescription *packetDescs;
AudioQueueBufferRef buffers[NUM_BUFFERS];
}
//定義隊列為實例屬性
@property AudioQueueRef queue;
//播放方法定義
-(id)initWithAudio:(NSString *) path;
//定義緩存數據讀取方法
-(void) audioQueueOutputWithQueue:(AudioQueueRef)audioQueue
queueBuffer:(AudioQueueBufferRef)audioQueueBuffer;
-(UInt32)readPacketsIntoBuffer:(AudioQueueBufferRef)buffer;
//定義回調(Callback)函數
static void BufferCallack(void *inUserData,AudioQueueRef inAQ,
AudioQueueBufferRef buffer);
@end
2.playAudio.m
playAudio的實現
//
// playAudio.m
// ffmpegPlayAudio
//
// Created by infomedia infomedia on 12-3-26.
// Copyright (c) 2012年 infomedia. All rights reserved.
//
#import "playAudio.h"
//實際測試中發現,這個gBufferSizeBytes=0x10000;對於壓縮的音頻格式(mp3/aac等)沒有任何問題,但是如果輸入的音頻文件格式是wav,會出現只播放幾秒便暫停的現象;而手機又不可能去申請更大的內存去處理wav文件,不知到大家能有什麼好的方法給點建議
static UInt32 gBufferSizeBytes=0x10000;//It muse be pow(2,x)
@implementation playAudio
@synthesize queue;
//回調函數(Callback)的實現
static void BufferCallback(void *inUserData,AudioQueueRef inAQ,
AudioQueueBufferRef buffer){
playAudio* player=(__bridge playAudio*)inUserData;
[player audioQueueOutputWithQueue:inAQ queueBuffer:buffer];
}
//緩存數據讀取方法的實現
-(void) audioQueueOutputWithQueue:(AudioQueueRef)audioQueue queueBuffer:(AudioQueueBufferRef)audioQueueBuffer{
OSStatus status;
//讀取包數據www.2cto.com
UInt32 numBytes;
UInt32 numPackets=numPacketsToRead;
status = AudioFileReadPackets(audioFile, NO, &numBytes, packetDescs, packetIndex,&numPackets, audioQueueBuffer->mAudioData);
//成功讀取時
if (numPackets>0) {
//將緩沖的容量設置為與讀取的音頻數據一樣大小(確保內存空間)
audioQueueBuffer->mAudioDataByteSize=numBytes;
//完成給隊列配置緩存的處理
status = AudioQueueEnqueueBuffer(audioQueue, audioQueueBuffer, numPackets, packetDescs);
//移動包的位置
packetIndex += numPackets;
}
}
//音頻播放的初始化、實現
//在ViewController中聲明一個PlayAudio對象,並用下面的方法來初始化
//self.audio=[[playAudioalloc]initWithAudio:@"/Users/xuanyuanchen/audio/daolang.mp3"];
-(id) initWithAudio:(NSString *)path{
if (!(self=[super init])) return nil;
UInt32 size,maxPacketSize;
char *cookie;
int i;
OSStatus status;
//打開音頻文件
status=AudioFileOpenURL((CFURLRef)[NSURL fileURLWithPath:path], kAudioFileReadPermission, 0, &audioFile);
if (status != noErr) {
//錯誤處理
NSLog(@"*** Error *** PlayAudio - play:Path: could not open audio file. Path given was: %@", path);
return nil;
}
for (int i=0; i<NUM_BUFFERS; i++) {
AudioQueueEnqueueBuffer(queue, buffers[i], 0, nil);
}
//取得音頻數據格式
size = sizeof(dataFormat);
AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &size, &dataFormat);
//創建播放用的音頻隊列
AudioQueueNewOutput(&dataFormat, BufferCallback, self,
nil, nil, 0, &queue);
//計算單位時間包含的包數
if (dataFormat.mBytesPerPacket==0 || dataFormat.mFramesPerPacket==0) {
size=sizeof(maxPacketSize);
AudioFileGetProperty(audioFile, kAudioFilePropertyPacketSizeUpperBound, &size, &maxPacketSize);
if (maxPacketSize > gBufferSizeBytes) {
maxPacketSize= gBufferSizeBytes;
}
//算出單位時間內含有的包數
numPacketsToRead = gBufferSizeBytes/maxPacketSize;
packetDescs=malloc(sizeof(AudioStreamPacketDescription)*numPacketsToRead);
}else {
numPacketsToRead= gBufferSizeBytes/dataFormat.mBytesPerPacket;
packetDescs=nil;
}
//設置Magic Cookie,參見第二十七章的相關介紹
AudioFileGetProperty(audioFile, kAudioFilePropertyMagicCookieData, &size, nil);
if (size >0) {
cookie=malloc(sizeof(char)*size);
AudioFileGetProperty(audioFile, kAudioFilePropertyMagicCookieData, &size, cookie);
AudioQueueSetProperty(queue, kAudioQueueProperty_MagicCookie, cookie, size);
}
//創建並分配緩沖空間
packetIndex=0;
for (i=0; i<NUM_BUFFERS; i++) {
AudioQueueAllocateBuffer(queue, gBufferSizeBytes, &buffers[i]);
//讀取包數據
if ([self readPacketsIntoBuffer:buffers[i]]==1) {
break;
}
}
Float32 gain=1.0;
//設置音量
AudioQueueSetParameter(queue, kAudioQueueParam_Volume, gain);
//隊列處理開始,此後系統開始自動調用回調(Callback)函數
AudioQueueStart(queue, nil);
return self;
}
-(UInt32)readPacketsIntoBuffer:(AudioQueueBufferRef)buffer {
UInt32 numBytes,numPackets;
//從文件中接受數據並保存到緩存(buffer)中
numPackets = numPacketsToRead;
AudioFileReadPackets(audioFile, NO, &numBytes, packetDescs, packetIndex, &numPackets, buffer->mAudioData);
if(numPackets >0){
buffer->mAudioDataByteSize=numBytes;
AudioQueueEnqueueBuffer(queue, buffer, (packetDescs ? numPackets : 0), packetDescs);
packetIndex += numPackets;
}
else{
return 1;//意味著我們沒有讀到任何的包
}
return 0;//0代表正常的退出
}
@end
這裡只是實現了最簡單的通過AudioQueue播放音頻文件。代碼寫的比較簡潔,相信搭建應該都能理解吧。
如果有需要的朋友,我可以把源碼傳上來共搭建分享,其實這個工程也比較簡單。
源碼下載:http://up.2cto.com/2012/0406/20120406032317286.zip
摘自 xuanyuanchen