The audio Queue Service provides the possibility of filling the audio data block into the audio queue service buffer to play the sound. This method is similar.
The waveoutwrite method in windows. In this way, we can use this method to play the audio data transmitted from the network.
We need to fill the buffer in the aqoutputcallback callback provided by the queue service. Here we can fill in the data transmitted from the network.
See the Code:
Audiostreambasicdescription format; // audio format settings, which must be consistent with the configuration in the collection set
Memset (& format, 0, sizeof (Format ));
Format. msamplerate = 44100; // sampling rate (Stereo = 8000)
Format. mformatid = kaudioformatlinearpcm; // PCM format
Format. mformatflags = klinearpcmformatflagissignedinteger | klinearpcmformatflagispacked;
Format. mchannelsperframe = 1; // 1: Mono; 2: stereo
Format. mbitsperchannel = 16; // number of digits occupied by voice at each sampling point
Format. mbytesperframe = (format. mbitsperchannel/* format. mchannelsperframe;
Format. mframesperpacket = 1;
Format. mbytesperpacket = format. mbytesperframe * format. mframesperpacket;
Audioqueueref queue;
Audioqueuenewoutput (& format,
Aqplayer: aqoutputcallback,
This, // opaque reference to whatever you like
Cfrunloopgetcurrent (),
Kcfrunloopcommonmodes,
0,
& Queue );
Const int buffersize = 0xa000; // 48 k-around und 1/2 sec of 44 kHz 16 bit mono PCM
For (INT I = 0; I <knumberbuffers; ++ I)
Audioqueueallocatebufferwithpacketdescriptions (queue, buffersize, 0, & mbuffers [I]);
Audioqueuesetparameter (queue, kaudioqueueparam_volume, 1.0 );
Uint32 Category = kaudiosessioncategory_mediaplayback;
Audiosessionsetproperty (kaudiosessionproperty_audiocategory, sizeof (category), & category );
Audiosessionsetactive (true );
// Prime the queue with some data before starting
For (INT I = 0; I <knumberbuffers; ++ I)
Outputcallback (queue, mbuffers [I]);
Audioqueuestart (queue, null );
Enter data in callback:
Void outputcallback (void * inuserdata, audioqueueref inaq, audioqueuebufferref incompleteaqbuffer ){
// Fill
// Aqplayer * That = (aqplayer *) inuserdata;
Incompleteaqbuffer-> maudiodatabytesize = Next-> maudiodatabytescapacity;
For (INT I = 0; I <incompleteaqbuffer-> maudiodatabytesize; ++ I)
Next-> maudiodata [I] = rand ();
Audioqueueenqueuebuffer (queue, incompleteaqbuffer, 0, null );
}