Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a music file using URL link in iPhone?

I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct me?

-(void)viewDidLoad 

{

[super viewDidLoad];
NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url

NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;


audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_objectData error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             

else 
    [audioPlayer play];
}
like image 673
Abhilash Avatar asked Jul 30 '12 06:07

Abhilash


People also ask

How can I play my own music on iPhone?

Browse and play your musicIn the Music app, tap Library, then tap a category, such as Albums or Songs; tap Downloaded to view only music stored on iPhone.

How do I create a link to a file on my iPhone?

Touch and hold the folder or file. , tap Manage Shared Folder or Manage Shared File, then tap Send Link. Choose a method for sending the link, enter any other requested information, then send or post the invitation.


2 Answers

This should work:

NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];

// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.

self.playerItem = [AVPlayerItem playerItemWithURL:url];

//(optional) [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

self.player = [AVPlayer playerWithPlayerItem:playerItem];

self.player = [AVPlayer playerWithURL:<#Live stream URL#>];

//(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];

Use the following code to play the music:

[self.player play];

like image 79
Tripti Kumar Avatar answered Sep 21 '22 02:09

Tripti Kumar


Try the following:

-(void)viewDidLoad 

{

[super viewDidLoad];
NSString* resourcePath = @"your url"; //your url

NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;


audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 1;

if (audioPlayer == nil)
    NSLog([error description]);             

else 
    [audioPlayer play];
like image 39
Aklesh Rathaur Avatar answered Sep 22 '22 02:09

Aklesh Rathaur