Stream online music in Objective c .

Learn how to stream audio files online , similar to the one you will see in Saavn or SoundCloud. Online streaming is very popular these days as it saves a lot of data of your device(iPhones and iPads).


In this tutorial i will tell you about streaming audio over internet without downloading the content .

Step 1: 

Open Xcode and create a new project.


Step 2: 

Add AVFoundation Framework to your project.

For adding framework go to your Project >>> Selects Targets  >>> Go to Build Settings  >>> Click on  + button and add AVFoundation Framework to your project.


Step 3:

Import the AVFoundation framework in your ViewController.m file by :

#import <AVFoundation/AVFoundation.h>


Step 4:

Create a UIButton so that we can play or pause the audio .

  1. Go to storyboard and drag the UIButton .
  2. Make its connection with the ViewController.

Step 5:

In ViewController.m add global elements by :


@interface ViewController ()
{
__weak IBOutlet UIButton *playButton; // Outlet of your UIButton
int count; // Count is used to change the title of button (Play/Pause)
AVPlayer *player; // AVPlayer used to play audio files
AVPlayerItem *playerItem; // set a item for the AVPlayer
}
@end
view raw LiveStreamGlobal hosted with ā¤ by GitHub


Step 6:

Add the following code to initialize AVPlayer and add online streaming in it.


//  FOR LIVE STREAMING 
NSURL *url = [NSURL URLWithString:@"http://s48.ve.vc/data/48/35977/276829/Tutti_Yaari_-_A_Kay_-_48Kbps_-_www.DjPunjab.Com.mp3"]; //Add any link of audio file which you want to play
playerItem = [AVPlayerItem playerItemWithURL:url]; // add url to playerItem
player = [AVPlayer playerWithPlayerItem:playerItem]; // add player item to AVAudioPlayer
player = [AVPlayer playerWithURL:url];
count = 0; // intialize count to zero
}
- (IBAction)playAction:(id)sender {
count++; // incremnets the count
[player play]; // make avplyer play the sound
[playButton setTitle:@"Pause" forState:UIControlStateNormal]; // change the Button`s text
if (count == 2) {
count = 0;
[playButton setTitle:@"Play" forState:UIControlStateNormal]; // change the Button`s text
[player pause]; // make the player pause
}
}
view raw streamOnline hosted with ā¤ by GitHub



Add url of any audio file you want to play.





Step 7:


Last step in this tutorial is to Add Transport Security Settings in your application.

If you are using Xcode 7 , then add the Add Transport Security Settings in info.plist
and under Add Transport Security Settings  >> make Allow Arbitrary Loads to YES as shown below:



Add Transport Security Settings







Thats all about the Live streaming in iOS applications .



Thank you  :)

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. the url link only works once how can i get it to play unlimited times

    ReplyDelete
    Replies
    1. Set the Property numberOfLoops to -1 and it would go into infinite loop.

      Delete
  3. Hello sir,
    hwo we implement seek bar.

    ReplyDelete
  4. can you upload about coredata in objective c

    ReplyDelete

Post a Comment

Popular Posts