Learn about audio processing stream playback files in iPhone game development

Source: Internet
Author: User

UnderstandingIPhoneSound processing in Game DevelopmentStream playback FileIs the content of this article,Stream playback FileUse AudioStream and AudioQueue for playback.File. The advantage is that you can quickly start playing and reduce reading.FileSuitable for largeFileEspecially for playing background music. The disadvantage is that only one video can be played at a time.FileIf you want to change the playback modeFileIt takes some time in the middle. But because of the iPhone'sFileThe read time is only 10 seconds.FileYou can only consider this method.

I will share my experience in this area: 1. Single file playback 2. Online file playback

1. Play a single file

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

The preceding elements must be defined as the elements required for playing a single file. It can be defined in the class.

2. Online file playback

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

Use http1.1 to play online files. The preceding parameters are required for online file playback.

 
 
  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   

Here are some defined parameters. NUM_QUEUE_BUFFERS is used to play a local file, while kNumAQBufs is used to play an online file.

3. Local file Initialization

 
 
  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. OnlineFileInitialization

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

Forget it. If you don't talk much about it, go directly to the code and explain it one by one later.

SummaryIPhoneSound processing in Game DevelopmentStream playback FileI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.