Audio file not play from server - iphone

I am playing audio file from server but it is not playing if i play local file then it plays here is my code.
//NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSURL *url = [NSURL URLWithString:#"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

Try to use MPMoviePlayerController instead of AVAudioPlayer
NSURL *url = [NSURL URLWithString:#"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
mv = [[MPMoviePlayerController alloc] initWithContentURL:url];
mv.movieSourceType = MPMovieSourceTypeUnknown;
[self.view addSubview:mv.view];
[mv play];

Use this code to solve your problem...
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

Related

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

I am getting an error when using AVAudioPlayer in Iphone. i got this error sometime not every time. error is :
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Code which i am using for playing a file :
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"abc" ofType:#"m4a"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
if(theAudio == nil)
{
NSLog([error description]);
}
else
{
[theAudio play];
}
can anyone tell me how can i solve this issue.

play mp3 from network

I have tried below three solutions but none of them worked. I am sure I am missing some key point but not clear what. (FYI, I am trying out below solutions on iPhone device, not on simulator.)
- (IBAction)actionAVPlayer:(id)sender {
AVPlayer* player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlTextField.text]];
[player play];
}
- (IBAction)actionMemoryBuffer1:(id)sender {
NSError *error;
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextField.text]];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
- (IBAction)actionMemoryBuffer2:(id)sender {
NSURLRequest* request = [ NSURLRequest requestWithURL: [NSURL URLWithString:urlTextField.text] ];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *_objectData = [ NSURLConnection sendSynchronousRequest:request returningResponse: &urlResponse error: &requestError];
NSError *error;
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
Any help is much appreciated.
Try using MPMoviePlayerController. First you need to import the MediaPlayer framework. Then it's as easy as this:
MPMoviePlayerController *myAudioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlTextField.text]];
[myAudioPlayer play];

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];
}
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];
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];
Try my code:
NSURL *filepath = [NSURL fileURLWithPath:audioFilePath];
[yourMediaPlayer setContentURL:filepath];
//set player.
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
^-^,I use MPMoviePlayerController with above code.
Current Answer:
Now consider Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43 link. its solution is avplayer link
Earlier Answer
//You have to pass NSURL not NSData
NSString* resourcePath = #"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSURL *url = [NSURL alloc]initWithString:resourcePath];
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
I guess you are sending
audioPlayer.numberOfLoops = -1;
to receive NSInvalidArgumentException change it to
audioPlayer.numberOfLoops = 1;
and check apple's documentation here

How do I cycle through several sounds using one action?

Right now I have a single sound playing from one action, button press, accelerometer, etc..
I would like to know how I can cycle through several sounds (I'm using 3 sounds for my project) from a single action that the user initiates.
I am currently using the code shown below and it serves it's purpose for playing a single sound for each user action. I have not used NSArray in a project before, so if your including it please include any details.
NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/Jet.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
If you are only using 3 sounds then you can get away with just using a C array of NSString's but if you need a dynamic amount of sounds then you should change to using an NSArray
// .m
NSString *MySounds[3] = {
#"sound1.wav",
#"sound2.wav",
#"sound3.wav",
};
#implementation ...
Then in your method you need to add a little extra logic
- (void)playSound;
{
NSString *path = [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], [self nextSoundName]];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil) {
NSLog(#"%#", [error description]);
} else {
[audioPlayer play];
}
}
- (NSString *)nextSoundName;
{
static NSInteger currentIndex = -1;
if (++currentIndex > 2) {
currentIndex = 0;
}
return MySounds[currentIndex];
}
-(IBAction)playSound:(UIButton *)sender{
NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], [MySounds objectAtIndex:[sender tag]]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
Here you can use single action buttons for all buttons. Just you need to do is set the tags to button. Use array as described by Paul

iPhone SDK - Mute

I have the following to play m background music:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/bgMusic.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
bgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
bgMusic.numberOfLoops = -1;
bgMusic.volume = 0.1;
if (bgMusic == nil)
NSLog([error description]);
else
[bgMusic play];
But how I can I in any view, mute all sounds, not just this, any sound?
Thanks.
[[MPMusicPlayerController applicationMusicPlayer] setVolume:(use a value between 0.0 and 1.0)]
If your MPMoviePlayerController uses the application audio session, this works.