自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

了解iPhone游戲開發(fā)中聲音處理流播放文件

移動開發(fā) iOS 游戲開發(fā)
本文主要是了解iPhone游戲開發(fā)中聲音處理流播放文件,好處是可以快速的開始播放,減少讀文件的過程,適合大文件特別是背景音樂的播放。

了解iPhone游戲開發(fā)中聲音處理流播放文件是本文介紹的內(nèi)容,流播放文件即用AudioStream 和 AudioQueue 來播放文件。好處是可以快速的開始播放,減少讀文件的過程,適合大文件特別是背景音樂的播放。壞處是一次只能播放一個文件,如果要換播放文件,中間需要一定的時間。但是因為iPhone文件讀取時間只有10秒,對于資源較大的文件,只能考慮這個方式了。

下面我將分享一下我在這方面的一點經(jīng)驗:1. 單個文件播放2. 在線文件播放

1. 單個文件播放

  1. BOOL isPlaying;    
  2. /*-------------------USED FOR LOCAL FILE--------------------*/    
  3. AudioFileID audioFile;    
  4. AudioStreamBasicDescription dataFormat;    
  5. AudioStreamPacketDescription *packetDescs;    
  6. UInt64 packetIndex;    
  7. UInt32 numPacketsToRead;    
  8. BOOL repeat;    
  9. BOOL trackClosed;    
  10. /*--------------------USED FOR PUBLIC------------------------*/    
  11. BOOL trackEnded;    
  12.     
  13. AudioQueueRef queue;    
  14. AudioQueueBufferRef buffers[NUM_QUEUE_BUFFERS];   

以上是需要定義的為單獨文件播放的所需要的元素??梢远x在類里面。

2. 在線文件播放

  1. NSURL *url;    
  2. AudioFileStreamID audioFileStream; // the audio file stream parser    
  3. AudioStreamPacketDescription packetDescsQueue[kAQMaxPacketDescs]; // packet descriptions for enqueuing audio   
  4. CFReadStreamRef stream;    
  5. unsigned int fillBufferIndex; // the index of the audioQueueBuffer that is being filled    
  6. size_t bytesFilled; // how many bytes have been filled    
  7. size_t packetsFilled; // how many packets have been filled    
  8. bool inuse[kNumAQBufs]; // flags to indicate that a buffer is still in use    
  9. bool started; // flag to indicate that the queue has been started    
  10. bool failed; // flag to indicate an error occurred    
  11. bool discontinuous; // flag to trigger bug-avoidance    
  12. pthread_mutex_t mutex; // a mutex to protect the inuse flags    
  13. pthread_cond_t cond; // a condition varable for handling the inuse flags    
  14. pthread_mutex_t mutex2; // a mutex to protect the AudioQueue buffer    
  15. BOOL trackEnded;    
  16. AudioQueueRef queue;    
  17. AudioQueueBufferRef buffers[NUM_QUEUE_BUFFERS];   

利用http1.1協(xié)議播放在線文件。以上是在線文件播放所需要的參數(shù)。

  1. #define NUM_QUEUE_BUFFERS 3    
  2. #define kNumAQBufs 6 // number of audio queue buffers we allocate    
  3. #define kAQBufSize 32 * 1024 // number of bytes in each audio queue buffer    
  4. #define kAQMaxPacketDescs 512 // number of packet descriptions in our array   

這里是定義的一些參數(shù),NUM_QUEUE_BUFFERS 用于播放本地文件,而 kNumAQBufs 用于播放在線文件。

3. 本地文件初始化

  1. - (id)initWithPath:(NSString*)path    
  2. {    
  3. UInt32 size, maxPacketSize;    
  4. char *cookie;    
  5. int i;    
  6.     
  7. if (kxxxTrackActive)    
  8. {    
  9. NSLog(@"Other music is playing.");    
  10. return nil;    
  11. }    
  12.     
  13. if (path == nil) return nil;    
  14. if(!(self = [super init])) return nil;    
  15.     
  16. // try to open up the file using the specified path    
  17. if (noErr != AudioFileOpenURL((CFURLRef)[NSURL fileURLWithPath:path], 0x01, 0, &audioFile))    
  18. {    
  19. NSLog(@"File can not be opened!");    
  20. return nil;    
  21. }    
  22.     
  23. // get the data format of the file    
  24. size = sizeof(dataFormat);    
  25. AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &size, &dataFormat);    
  26.     
  27. // create a new playback queue using the specified data format and buffer callback    
  28. AudioQueueNewOutput(&dataFormat, BufferCallback, self, nil, nil, 0, &queue);    
  29.     
  30. // calculate number of packets to read and allocate space for packet descriptions if needed    
  31. if (dataFormat.mBytesPerPacket == 0 || dataFormat.mFramesPerPacket == 0)    
  32. {    
  33. // Ask Core Audio to give us a conservative estimate of the largest packet    
  34. size = sizeof(maxPacketSize);    
  35. AudioFileGetProperty(audioFile, kAudioFilePropertyPacketSizeUpperBound, &size, &maxPacketSize);    
  36. if (maxPacketSize > kxxxBufferSizeBytes)    
  37. {    
  38. /*Limitation for the maximum buffer size*/    
  39. maxPacketSize = kxxxBufferSizeBytes;    
  40. NSLog(@"Size out of bounds!");    
  41. }    
  42. // calculate how many packs to read    
  43. numPacketsToRead = kxxxBufferSizeBytes / maxPacketSize;    
  44.     
  45. // will need a packet description for each packet to allocate space accordingly    
  46. packetDescs = malloc(sizeof(AudioStreamPacketDescription) * numPacketsToRead);    
  47. }    
  48. else    
  49. {    
  50. // constant bitrate    
  51. numPacketsToRead = kxxxBufferSizeBytes / dataFormat.mBytesPerPacket;    
  52.     
  53. // don't need packet descriptions for CBR data    
  54. packetDescs = nil;    
  55. }    
  56.     
  57. // see if file uses a magic cookie (a magic cookie is meta data which some formats use)    
  58. AudioFileGetPropertyInfo(audioFile, kAudioFilePropertyMagicCookieData, &size, nil);    
  59. if (size > 0)    
  60. {    
  61. // copy the cookie data from the file into the audio queue    
  62. cookie = malloc(sizeof(char) * size);    
  63. AudioFileGetProperty(audioFile, kAudioFilePropertyMagicCookieData, &size, cookie);    
  64. AudioQueueSetProperty(queue, kAudioQueueProperty_MagicCookie, cookie, size);    
  65. free(cookie);    
  66. }    
  67.     
  68. // we want to know when the playing state changes so we can properly dispose of the audio queue when it's done    
  69. AudioQueueAddPropertyListener(queue, kAudioQueueProperty_IsRunning, propertyListenerCallback, self);    
  70.     
  71. // allocate and prime buffers with some data    
  72. packetIndex = 0;    
  73. for (i = 0; i < NUM_QUEUE_BUFFERS; i++)    
  74. {    
  75. AudioQueueAllocateBuffer(queue, kxxxBufferSizeBytes, &buffers);    
  76. if ([self readPacketsIntoBuffer:buffers] == 0)    
  77. {    
  78. // this might happen if the file was so short that it needed less buffers than we planned on using    
  79. break;    
  80. }    
  81. }    
  82. repeat = NO;    
  83. trackClosed = NO;    
  84. trackEnded = NO;    
  85. kxxxTrackActive = YES;    
  86. return self;    
  87. }   

4. 在線文件初始化

  1. - (id)initWithURL:(NSURL*)newUrl    
  2. {    
  3. self = [super init];    
  4. if (self != nil)    
  5. {    
  6. url = [newUrl retain];    
  7. }    
  8. return self;    
  9. }   

算了,廢話不多說了,直接上代碼,等以后有時間了再逐一解釋。

小結(jié)了解iPhone游戲開發(fā)中聲音處理流播放文件的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2012-12-27 14:29:38

Android開發(fā)流媒體

2011-07-22 15:59:15

iPhone 聲音 文件

2011-08-04 17:19:49

iPhone開發(fā) Xcode 文檔

2011-08-22 15:15:49

iPhone開發(fā)NSMutableAr排序

2011-08-10 15:58:58

iPhone視頻

2011-07-18 11:07:12

iPhone 游戲 引擎

2011-07-18 10:53:09

2011-07-18 12:29:10

2011-07-18 11:39:58

iPhone 游戲 引擎

2011-07-18 11:23:29

iPhone 游戲 動畫

2011-08-02 13:35:41

iOS開發(fā) Get Post

2011-08-12 14:33:06

iPhone緩存文件

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2011-08-08 18:19:09

iPhone音頻播放

2011-08-09 14:42:07

iPhonePCM播放器

2020-09-28 06:50:02

DuerOS 智能語音

2011-07-08 20:32:57

iPhone midi

2010-01-07 18:22:40

VB.NET聲音播放

2011-08-01 14:34:06

iPhone 聲音 音頻

2011-08-02 10:36:02

iOS開發(fā) SDK 多媒體
點贊
收藏

51CTO技術(shù)棧公眾號