Radio App Example iPhone

I was working on an application to play audio stream from an online radio. Here is some hints for the developers who want to do similar types of work.

Initially we have two major task:
1. Read stream data.
2. Play the stream data.

From apple developer documentation:

http://developer.apple.com/iphone/library/codinghowtos/AudioAndVideo/index.html#STREAMING

How do I play streamed audio?

To play streamed audio, you connect to a network stream using the CFNetwork interfaces from Core Foundation, such as those in CFHTTPMessage. You then parse the network packets into audio packets using Audio File Stream Services (AudioToolbox/AudioFileStream.h). Finally, you play the audio packets using Audio Queue Services (AudioToolbox/AudioQueue.h). You can also use Audio File Stream Services to parse audio packets from an on-disk file.

Task 1: Working with HTTP stream

Read “CFNetwork Programming Guide” specially the “Communicating with HTTP Servers” part.

Use the callback

if (CFReadStreamSetClient(myReadStream, registeredEvents, myCallBack, &myContext) {...}

A sample callback:

static void myCallBack
(CFReadStreamRef stream, CFStreamEventType type, void *clientCallBackInfo) {
if(type == kCFStreamEventHasBytesAvailable) {
UInt8 buffer[2048];
CFIndex bytesRead = CFReadStreamRead(stream, buffer, sizeof(buffer));

if (bytesRead > 0) {
//nothing
}
else if (bytesRead) {
NSString* to_add = [NSString stringWithCString: (char*)buffer length: bytesRead];
NSLog(@"%@", to_add);
}
}
}

Run the application. If you can see the HTML tag in your debug console, then your network stream reading is working fine.

Task 2: Working with audio

Read “Audio File Stream Services Reference” to get basic idea about the streaming audio.

Check the “AudioFileStreamExample” from:
http://developer.apple.com/Mac/library/samplecode/AudioFileStreamExample/index.html

Merge the “AudioFileStreamParseBytes” with the “myCallBack” method.

Download Sample Code:
http://www.mymacbd.com/forum/viewtopic.php?id=26

Leave a Reply

Your email address will not be published.