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 .
- Go to storyboard and drag the UIButton .
- Make its connection with the ViewController.
Step 5:
In ViewController.m add global elements by :
Step 6:
Add the following code to initialize AVPlayer and add online streaming in it.
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:
Thats all about the Live streaming in iOS applications .
Thank you :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Step 6:
Add the following code to initialize AVPlayer and add online streaming in it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//  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 | |
} | |
} |
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 :)
thnkz for the post :)
ReplyDeleteThank you for reading... :)
DeleteThis comment has been removed by the author.
ReplyDeletethe url link only works once how can i get it to play unlimited times
ReplyDeleteSet the Property numberOfLoops to -1 and it would go into infinite loop.
DeleteHello sir,
ReplyDeletehwo we implement seek bar.
It's Very nice , Thanks for sharing
ReplyDeleteiOS app development Online Training Hyderabad
can you upload about coredata in objective c
ReplyDelete