Powered By Blogger

Tuesday, October 16, 2012

How to integrate live audio streaming in iPhone?

Hi everyone,
I think, all of you heard about live audio/video streaming. I'm going to share "Live Audio Streaming" example with you.
Please check this :

Step 1: First of all, you have to download these two files(AudioStreamer.h, AudioStreamer.m) from http://code.google.com/p/audiostreamer-meta/source/browse/trunk/Classes

Step 2: Add necessary frameworks and above two files into your project.

Step 3:
Implement below code to your project file.

@class AudioStreamer;

@interface CallerTuneListViewController : UIViewController
{
    AudioStreamer *streamer;
}

#import "CallerTuneListViewController.h"

#import "AudioStreamer.h"
#import <QuartzCore/CoreAnimation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <CFNetwork/CFNetwork.h>
#import <AudioToolbox/AudioToolbox.h>

@implementation CallerTuneListViewController

#pragma mark ---------------------------------
#pragma mark View LifeCycle

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([streamer isPlaying]) {
        [self destroyStreamer];
    }
}

#pragma mark ---------------------------------
#pragma mark Memory Management

- (void)dealloc
{
    [self destroyStreamer];
    [super dealloc];
}

#pragma mark ---------------------------------
#pragma mark UIButton Actions

-(IBAction)playLiveStream:(id)sender
{
            if (![streamer isPlaying])
                {
                    [self createStreamer:AudioFilePath];
                    [streamer start];
                }
                else
                {
                    [streamer stop];
                }
}


#pragma mark ---------------------------------
#pragma mark AudioStreamer Methods

// Removes the streamer, the UI update timer and the change notification
- (void)destroyStreamer
{
    if (streamer)
    {
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:ASStatusChangedNotification
         object:streamer];
      
        [streamer stop];
        [streamer release];
        streamer = nil;
    }
}

// Creates or recreates the AudioStreamer object.
- (void)createStreamer:(NSString *)audioPath
{
    if (streamer)
    {
        return;
    }
  
    [self destroyStreamer];
  
    NSURL *url = [NSURL URLWithString:audioPath];
    streamer = [[AudioStreamer alloc] initWithURL:url];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:ASStatusChangedNotification
     object:streamer];
}

// Invoked when the AudioStreamer
// reports that its playback status has changed.
- (void)playbackStateChanged:(NSNotification *)aNotification
{
    if ([streamer isWaiting])
    {
    }
    else if ([streamer isPlaying])
    {
    }
    else if ([streamer isIdle])
    {
        [self destroyStreamer];
    }
}

I hope this will be useful to all...

Thanks ,
Nilesh M. Prajapati

7 comments:

  1. hey Nilesh,

    Good post , am adding MATT's Audiostreamer in my project .
    The thing is that i have an array of remote mp3 urls ,i want to pass it in this streamer , how to achieve it ? help needed!!!

    ReplyDelete
  2. Thanks for your complement..

    As I write code for "createStreamer", using that function you can create your audio streamer as just adding mp3 url one by one.

    ReplyDelete
  3. can you provibe the sample code please

    ReplyDelete
  4. Hi,
    You can go with Google code link .. and follow the steps which I had mentioned in this post.

    Good luck.

    Thanks,
    Nilesh Prajapati

    ReplyDelete
  5. hi,
    can we save stream coming from server

    ReplyDelete
    Replies
    1. Hi,
      Yes it is possible to store stream coming from server. But it also depends on the contents' privacy too.

      Delete
  6. Hi Nilesh How do i Broadcast Audio which stored in Music like Radio and other user can listen real time can you please share your idea or link

    ReplyDelete