not able to play audio from internet using AVAudioPlayer - objective-c

i tried to play an audio file from internet using AVAudioplayer but there is no response from UIView but if i m to play an mp3 file from bundle it works ..cud u guys help me out below is the code...
-(IBAction)playPause:(id)sender
{
NSURL *url=[NSURL URLWithString:#"http://www.1040communications.net/sheeba/stepheni/iphone/images/today.mp3"];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!player) NSLog(#"Error: %#", error);
[player prepareToPlay];
[player play];
}

The URL has to be a file URL, see Apple’s Technical QA1634. You can download the file to the device and then play it locally.

your url is not returning any data.That's why it is not playing.

Related

audioPlayer sound played on IPhone different than IPAD (Iphone is low volume and has to be held to ear)

I am playing a sound file in an APP and on the iPhone plays with volume like it is a phone call (you have to hold the phone to your ear).
When played on the iPad it plays like you would expect, sounds like a music file being played.
Does anyone know how to make the iPhone play a sound like it is music playing?
Here is what I use to play the sound:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/30perMinute2.caf", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(#"selection didn't work");
else {
// [audioPlayer stop];
[audioPlayer play];
}
I have set the volume in audioPlayer.
Here is whatr ended up fixing the problem:
In AppDelegate.h
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
In AppDelegate.m
In application didFinishLaunchingWithOptions add
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

No sound/volume in Iphone Simulator 4.3 . But plays ok in Iphone simulator 5.1

I am developing a IPhone app to play sound files. My deployment target is 4.0. While trying to play in IPhone Simulator 4.3 it plays with no sound/volume. But it playing ok in Simulator 5.1.
My code:
NSString *fileName=[NSString stringWithFormat:#"book%i_chapter_%i",book_number,chapter_number];
NSString *path=[[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3" inDirectory:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if(error)
{
NSLog(#"Error in audio Player: %#",[error localizedDescription]);
}
else
{
[audioPlayer prepareToPlay];
//audioPlayer.delegate=self;
[audioPlayer setVolume:20.0];
[audioSlider setValue:0.0];
[audioSlider setMaximumValue:[audioPlayer duration]];
[audioPlayer play];
}
Please help me to fix this. Thanks

How to Stop UIWebView from playing mp3?

I am loading a MP3 file into UIWebView control on an iPad Application, I have a UIButton called done the user expected to dismiss the UIWebView and stop the music, here is a code snippet:
// points to the mp3 file path
NSURL *url = [NSURL fileURLWithPath:self.sFilePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[web loadRequest:request];
When the user hit done I do the following:
[self.webview removeFromSuperview];
[self.webview release];
But that does not stop the music from playing, I noticed that loading mp3 files on UIWebView opens the QuickTime player is that correct way?
I am greatly appreciative of any guidance or help.
Looks like a dupe of Video/audio streaming does not stop even if UIWebView is closed - iPad
The solution there:
[self.webContent loadRequest:NSURLRequestFromString(#"about:blank")];
I suggest that you play the sound yourself using for example AVAudioPlayer (probably the easiest way to play and control sounds in iOS).
Something like this:
NSURL *url = [NSURL fileURLWithPath:self.sFilePath];
NSData *mySound = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:mySound error:NULL];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
You can then stop the sound as simple as:
[audioPlayer stop];
or
[audioPlayer pause];
Don't forget to include the AVFoundation.framework in your project.

MP3 file not playing in Iphone/Ipad

I am palying MP3 files in my application using code
NSString *filePath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:#"congratulation.MP3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;
[audioPlayer play];
I am playing 15 MP3 files
All playing well, but one file is not playing it is playing very well i itunes but not in my app
Any solution for this strange problem.
Amit Battan
I have to replace the MP3 files

Audio playback does not start

NSError *err;
// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.
Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer
The operation couldn’t be completed. (OSStatus error -43.)
Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.
Did you initialize a AVAudioSession?
The following works fine for me. Might give you a hint of whats not working for you.
Initializing:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
[session setActive:TRUE error:nil];
Loading:
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.volume = 1.0;
Playing:
[audioPlayer play];
Update
To find the right NSRUL for fileUrl, I used this code:
NSURL* fileUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"MySound" ofType:#"wav"] isDirectory:NO];
And then I added MySound.wav to the project (bundle).