Actual combat audiotoolbox--playing audio on iOS platform

Source: Internet
Author: User

  Morning Read about audiotoolbox.framework related information, combined with the online information on the basic use of Audiotoolbox has a general understanding of the previous article Audiotoolbox (a) The most important steps in using Audioqueue to achieve audio playback are:

1. Open Play audio file
2. Get the data format for playing audio files
3. Prepare the queue for playback
4. Move the data in the buffer to the queue
5. Start Playback
6. Queue processing in callback functions

Follow this step to achieve audio playback.

osstatus status;
NSString *path = [[NSBundle mainbundle] pathforresource:@ "music_aac_48" oftype:@ "AAC"];
//Open an audio fileStatus=audiofileopenurl (cfurlref) [Nsurl Fileurlwithpath:path], kaudiofilereadpermission,0, &audioFile); if(Status! =NOERR) { //Error HandlingNSLog (@"* * * Error * * * playaudio-play:path:could not open audio file. Path given was:%@", path); returnNil; }

// get Audio data format    sizeof (DataFormat);     &size, &dataformat)

 //Create an audio queue for playbackAudioqueuenewoutput (&DataFormat, buffercallback, self, nil, nil,0, &queue); //calculate the number of packages that are included in the unit time    if(dataformat.mbytesperpacket==0|| dataformat.mframesperpacket==0) {size=sizeof(maxpacketsize); Audiofilegetproperty (AudioFile, Kaudiofilepropertypacketsizeupperbound,&size, &maxpacketsize); if(MaxPacketSize >gbuffersizebytes) {MaxPacketSize=gbuffersizebytes; }        //calculate the number of packages in a unit of timeNumpacketstoread = gbuffersizebytes/maxpacketsize; Packetdescs=malloc (sizeof(audiostreampacketdescription) *numpacketstoread); }Else{Numpacketstoread= gbuffersizebytes/Dataformat.mbytesperpacket; Packetdescs=Nil; }        //to set up a magic Cookie, see the Introduction to Chapter 27thAudiofilegetproperty (AudioFile, Kaudiofilepropertymagiccookiedata, &size, nil); if(Size >0) {Cookie=malloc (sizeof(Char)*size); Audiofilegetproperty (AudioFile, Kaudiofilepropertymagiccookiedata,&size, cookie);    Audioqueuesetproperty (queue, Kaudioqueueproperty_magiccookie, cookie, size); }        //Create and allocate buffer spacepacketindex=0;  for(i=0; i<num_buffers; i++) {audioqueueallocatebuffer (queue, gbuffersizebytes,&Buffers[i]); //Read package data        if([Self readpacketsintobuffer:buffers[i]]==1) {             Break; }    }//-----------------------------------------------------------------------------(UInt32) Readpacketsintobuffer: (audioqueuebufferref) buffer {UInt32 numbytes,numpackets; //accepting data from a file and saving it in cache (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;//means we didn't read any of the packages.    }    return 0;//0 means normal exit.}

Float32 gain=1.0;     // Set Volume     audioqueuesetparameter (queue, Kaudioqueueparam_volume, gain);     // the queue processing begins and the system begins to call the callback (Callback) function automatically    Audioqueuestart (queue, nil);

//implementation of the callback function (Callback)Static voidBuffercallback (void*inuserdata,audioqueueref Inaq, audioqueuebufferref buffer) {Playaudio* Player= (__bridge playaudio*) Inuserdata; [Player Audioqueueoutputwithqueue:inaq Queuebuffer:buffer];}//implementation of Cache data reading method-(void) Audioqueueoutputwithqueue: (audioqueueref) audioqueue Queuebuffer: (audioqueuebufferref) audioQueueBuffer{        Osstatus status; //Read package dataUInt32 numbytes; UInt32 numpackets=Numpacketstoread; Status= Audiofilereadpackets (AudioFile, NO, &numbytes, Packetdescs, Packetindex,&numpackets, audioqueuebuffer->maudiodata); //when read successfully    if(numpackets>0) {        //set the buffered capacity to the same size as the read audio data (to ensure memory space)Audioqueuebuffer->maudiodatabytesize=numbytes; //to complete the processing of configuring the cache for a queueStatus =Audioqueueenqueuebuffer (Audioqueue, Audioqueuebuffer, Numpackets, Packetdescs); //move the location of the packagePacketindex + =numpackets; }}

Through the above steps and code on the basic use of audiotoolbox.framework have a clear understanding. But this is just playing the local audio file, for the audio file pause, playback and other operations, as well as the network audio stream processing, this is a follow-up learning content.

The demo project of this paper is Audiotoolboxplayaudio: Http://pan.baidu.com/s/1c0pmpBM

Actual combat audiotoolbox--playing audio on iOS platform

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.