I have a sound file on a server and I try to play it on device.
Using this code:
NSURL audioURL = [NSURL URLWithString:#"http://..."];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]
crashes with the error:
Description of exception being thrown: '-[NSTaggedPointerString getCharacters:range:]: Range {0, 12} out of bounds; string length 5
The url has the length 73.
Firstly, why is it crashing instead of populating my error object?
If I use this code:
NSURL audioURL = [NSURL URLWithString:#"http://..."];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error]
it works.
Looking at the name of the methods, it looks like they do the same thing, so why isn't the first method working?
Reading the AVAudioPlayer init methods' documentation reveals nothing related to this problem.
Are you streaming your audio file?
AVAudioPlayer cannot stream from URL. You can use it with files stored on device.
If you want to stream your file from URL you need to use AVPlayer
The following code:
AVPlayerItem *item = [AVPlayerItem playerItemWithURL: audioURL];
AVPlayer *myAVPlayer = [AVPlayer playerWithPlayerItem: item];
EDIT
When you are going to use your AVAudioPlayer with URL you need to present your URL like this:
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"myMusic" ofType:#"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
Taken from here
Hope it will help
Related
I have a simple code to a play sound through AVAudioPlayer
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"beep"
ofType:#"mp3"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.volume = 1;
[self.player prepareToPlay];
My exception breakpoint will stop at prepareToPlay and console log wrote:
ERROR: AVAudioSessionUtilities.h:111: GetProperty:
AudioSessionGetProperty ('prp?') failed with error: 'pty?
After 2x tapping to pass through this breakpoint, application will not crash. This happened only in the simulator.
Any suggestions?
Following code might be helpful.
NSURL *url;
url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/beep.mp3", [[NSBundle mainBundle] resourcePath]]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
self.audioPlayer .delegate = self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
I think its a Simulator specific error. This error doesnt come up on the device. There is a discussion here if you want to know more.
AVSpeechSynthesizer error AudioSession
in my app i need to play alert sounds.
if another app (like spotify) is running, when i run my code it stops the music from spotify whenever the alert sound plays.
is there anyway to avoid this?
this is my play sound code:
- (void)playSendSound
{
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player play];
}
You are using AVAudioPlayer to play your alert sound and sure it will stop whatever music that is playing.
Modify your code to the following:
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[player play];
This should prevent the sound that is playing to stop when you play your alert sound.
Hope this helps.
In my application, I have an audio url. I want to play the audio using that url on my device. It does not seem to be working.
Here is the code I am using.
- (IBAction)play:(id)sender {
NSURL *url = [NSURL URLWithString:#"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer play];
}
I have used the above code. It's not working. Please tell me where I'm going wrong in the above code.
You need to add percentage escape in your query string, also you need to declare the AVAudioPlayer as a global variable.
So write the property in your class extension:
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
And modify your method like:
- (IBAction)play:(id)sender
{
NSString *urlstr = #"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[_audioPlayer play];
}
You need to add percentage encoding %20 of space in URL
NSString *strURl=#"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
NSURL *url = [NSURL URLWithString:[strURl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if still not working use delegate methods
audioPlayer = self;
USe delegate methods
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player
error:(NSError *)error
{
NSLog(#"%#",error);
}
I am having trouble with my audio.
here is the code:
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"Cheering" ofType:#"mp3"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
It will not play like this. The code steps through just fine but no audio. If I put a break on the last line and then step through it plays properly. I have tried everything I can to get this to work but have no clue where to go from here.
It is acting like it needs a delay before the play command.
Try this Sure It will Works
NSURL* url = [[NSBundle mainBundle] URLForResource:#"Cheering" withExtension:#"mp3"];
NSAssert(url, #"URL is valid.");
NSError* error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if(!audioPlayer)
{
NSLog(#"Error creating player: %#", error);
} else
{
[audioPlayer play];
}
Did you try
[audioPlayer prepareToPlay]
before you actually try to play it?
https://developer.apple.com/library/ios/DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/occ/instm/AVAudioPlayer/prepareToPlay
I am trying to use NSData with MPMoviePlayerViewController.
NSData *data = [NSData dataWithBytesNoCopy:mData3->mappedAddress+100398125 length:2313453 freeWhenDone:NO];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
NSURL *movieURL = [NSURL fileURLWithPath:dataString];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
This leads to the player opening for a second and then being dismissed.
When I access the movie file locally using the URL to the file in the main bundle it plays perfectly.
How does one use NSData to play a video on iOS?
Thanks
There is no way to play a movie directly from an in-memory NSData blob.