I am trying to play a sound on the click of a button. But when I run the app it crashes and I get Thread 1 : signal SIGABRT.
This is my code:
.h file
#import <AVFoundation/AVFoundation.h>
#interface HljodViewController : UIViewController <AVAudioPlayerDelegate>{
}
-(IBAction)playsound;
.m file
-(IBAction)playsound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Geese_4" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.numberOfLoops = 1;
[theAudio play];
}
No one can tell you how to solve your issue if you don't capture the NSError. I suggest you do the following:
NSError *outError = nil;
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&outError];
if (outError)
{
NSLog(#"%#", [outError localizedDescription]);
}
... // continue on to happy time
try this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourfile" ofType:#"mp3"];
NSError *error;
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[sound play];
Related
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.
I have an Ipad application in which i am playing a bit of sound after my operation success .it was working fine earlier.i am doing like this `
NSError *error;
NSString *bell = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"aif"];
AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:bell] error:&error];
if(error) {
NSLog(#"%#",&error);
}
[av play];
but now the sound is not playing in ipad device.but in simulator it was working fine?can anybody help me?
Check if file exists:
NSError *error;
NSString *bell = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"aif"];
if([[NSFileManager defaultManager] fileExistsAtPath:bell]){
AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:bell] error:&error];
if(error) {
NSLog(#"%#",&error);
}
[av play];
}
else
{
NSLog(#"File doesnot exist");
}
Note :Check phone is not set to silent mode
NSString *musicPath = [[NSBundle mainBundle] pathForResource:#"ding" ofType:#"mp3"];
if (musicPath)
{
if(TapSoud)
{
[TapSoud release];
TapSoud = nil;
}
TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
}
[TapSoud setDelegate:self];
[TapSoud prepareToPlay];
[TapSoud play];
NSString *musicPath1 = [[NSBundle mainBundle] pathForResource:#"roadrunner" ofType:#"mp3"];
if (musicPath1)
{
if(MatchSoud)
{
[MatchSoud release];
MatchSoud = nil;
}
MatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath1] error:nil];
}
[MatchSoud setDelegate:self];
[MatchSoud prepareToPlay];
NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:#"Wrong Answer" ofType:#"wav"];
if (musicPath2)
{
if(WrongMatchSoud)
{
[WrongMatchSoud release];
WrongMatchSoud = nil;
}
WrongMatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath2] error:nil];
}
[WrongMatchSoud setDelegate:self];
[WrongMatchSoud prepareToPlay];
This code works properly in the simulator, but generates no sound on the device. What is the problem here?
The most likely problem is that the audio files are missing either in the project or at resource copying stage. The simulator stores previously copied resources even if removed from the project until the application is removed completely from the simulator.
Make sure musicPath, musicPath1 and musicPath2 are not empty and valid.
I am trying to make an audio controller, but the stop button doesn't work. But I don't know why. How is this caused and how can I solve it?
.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface myprojectViewController : UIViewController {
AVAudioPlayer* theAudio;
}
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
#end
.m
- (IBAction)start:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:#"LE" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)stop:(id)sender{
[theAudio stop];
}
'theAudio' is a local variable in your start() method. You've declared it as a local variable, so it is out of scope in your stop() method, and you have an invalid reference. You need to use the class variable 'theAudio. Change :
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
to
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
I want to play mp3 file placed in resources as code is given below,
NSString *st=#"alarm_1.mp3";
NSLog(st);
NSURL* musicFile = [NSURL fileURLWithPath:st];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
//AVAudioPlayer *click1=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:#"alarm_1.mp3"]];
[click prepareToPlay];
[click play];
[click release];
but unfortunately i cant hear the sound
please help
I don't think your file name is right. Try this syntax:
NSURL *clickURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:clickURL error:nil];
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
This is answer