Powered By Blogger
Showing posts with label AudioStreamer. Show all posts
Showing posts with label AudioStreamer. Show all posts

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