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);
}
Related
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
I'm following in a tutorial on how to play mp3 sounds when pressing a button.
I created a button (playSound).
I added it to the view controller interface:
- (IBACTION)playSound:(id)sender;
in the implementation I declared the needed header files and I wrote the following:
#import "AudioToolbox/AudioToolbox.h"
#import "AVFoundation/AVfoundation.h"
- (IBAction)playSound:(id)sender {
//NSLog(#"this button works");
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"];
//NSLog(#"%#", audioPath);
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
//NSLog(#"%#", audioURL);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
I'm not getting any errors. the NSlogs are logging the URL fine, and now I have no clue where to look further. I also checked if my MP3 sound was maybe damaged, but that was also not the case. all i hear is a little crackling noise for 1 second. then it stops.
You declared a local variable audioPlayer to hold a pointer to the player. As soon as your button handler returns the player is being released before it has a chance to play your sound file. Declare a property and use it instead of the local variable.
In YourViewController.m file
#interface YourViewController ()
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
or in YourViewController.h file
#interface YourViewController : UIViewController
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
Then replace audioPlayer with self.audioPlayer in your code.
try this its working for me
in .h
AVAudioPlayer * _backgroundMusicPlayer;
in .m
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"Theme" ofType:#"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
[_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions
[_backgroundMusicPlayer setNumberOfLoops:-1];
[_backgroundMusicPlayer play];
Edited
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Name of your audio file"
ofType:#"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate=self;
[audioPlayer play];
Try this and let me know. Make sure you set the delegate for audioPlayer.
Firstly re add your music file in your project , then try this code
With the log you can see error.
NSError *error;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"bg_sound" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error){
//Print Error
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops: -1];
[audioPlayer play];
audioPlayer.volume=1.0;
}
Make sure your music files are properly added in project
I want to play a sound when a button is pressed. I tried this:
-(void)PlayClick
{
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];
}
…and imported #import <AudioToolbox/AudioToolbox.h> and #import <AVFoundation/AVFoundation.h>
but for some reason the sound is not playing. The name and type are right because the file I added to the project is "BubblePopv4.mp3". The volume on the iPad is on maximum, and the sound plays fine on iTunes! Any ideas?
Leave it, Don't do it in a new Class, simple stuff for you:
- (void)playSoundWithOfThisFile:(NSString*)fileNameWithExtension {
// Eg - abc.mp3
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
[audioPlayerObj play];
}
Do this:
In .h file:
Use this delegate - <AVAudioPlayerDelegate>
#import "AVFoundation/AVAudioPlayer.h"
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
In .m file , In some method:
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
And add these two methods:
- (id)initWithFileName:(NSString*)fileNameWithExtension {
if ((self = [super init])) {
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
}
return self;
}
- (void)playSound {
[audioPlayerObj play];
}
You can pass the filename to this Method - initWithFileName
and then call playSound method to play sound for you.
Hope this helps.
Replace url initialization with:
NSURL* musicFile = [NSURL initFileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];
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.
I have this method to play an mp3:
- (void) playSound:(NSString*)sound{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
sound];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[player prepareToPlay];
[player play];
}
everytime I call this method I get a new string "sound" and then I must alloc everytime this AVAudioplayer "player"; I want release it when I stop it or when it finish...is it possible?
Try the below instruction
yourViewController.h
#interface yourViewController : UIViewController <AVAudioPlayerDelegate>
yourViewController.m file for add function
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//play finished
}
Declare the AVAudioPlayer Object below code
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"audio" ofType:#"mp3"]] error:NULL];
audioPlayer.delegate = self;
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
[audioPlayer play];
Thanks..!
Take a look at AVAudioPlayerDelegate.
It has the methods audioPlayerDidFinishPlaying:successfully: and audioPlayerDecodeErrorDidOccur:error: that should let you do what you want.