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
Related
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];
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 am developing an iPhone application.In this app i am using AVAudioPlayer for playing audio.
This is my code for playing the sound.
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:#"/sound.mp3"];
NSError* err;
player_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err )
{
}
else
{
[player_ prepareToPlay];
[player_ play];
int playingLoops = 0;
player_.numberOfLoops = playingLoops;
}
It is woking fine on iphone devices except iPhone5.While i run the app on iPhone 5 i get following error then app crashes.
2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error '!obj' trying to fetch default input device's sample rate
2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error getting audio input device sample rate: '!obj'
2012-11-26 09:02:28.494 test[728:1dd03] <0xb0081000> AQMEIOManager::FindIOUnit: error '!dev'
I was also facing same problem giving the same error. when try to solve this problem i notice that there is no problem in code. we need to update media player or install flash player and it's work fine.
You have to add AVAudioPlayerDelegate to your .h file and following code to your .m file
NSString* resourcePath =[[NSBundle mainBundle]pathForResource:#"sound" ofType:#"mp3"];
NSError* err;
AVAudioPlayer *audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath: resourcePath] error:&err];
audioplayer.delegate=self;
if( err )
{
NSLog(#"%#",err);
}
else
{
[audioplayer prepareToPlay];
[audioplayer play];
}
This code is working for me and accept the answer if it is working.
Anbu, try below code.At my end it's working fine.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSLog(#"File path is:->%#",[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]);
NSFileManager* manager;
manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]])
{
printf("file Exists...\n\n");
}
else
printf("File not exist...\n\n");
NSError *er;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([er description]);
else
[audioPlayer play];
In any concern, let me know. And, also please check your console for the full path and for file exist or not.
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.
So I'm trying to test out the audio player on the iPhone, and I went off Troy Brant's iOS book. I have the Core Audio, Core Foundation, AudioToolbox, and AVFoundation frameworks added to my project. The error message I get is in the subject field. I read like 20 pages of Google search results before resorting to asking here! /sigh. Thanks if you can help. Here's my code, pretty much verbatim out of his book:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Yonah" ofType:#"caf"];
NSLog(#"%#", soundFilePath);
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (!error)
{
audioPlayer.delegate = self;
//audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
else
{
NSLog(#"Error loading audio clip: %#", [error localizedDescription]);
}
EDIT: Holy Shinto. I figured out what it was. I changed
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
to
NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];
to the latter and I was getting a weird error, weirder than the one in the subject BUT I googled that and I changed my OS input device from my webcam to my internal microphone and guess what, it worked under the fileURLWithPath method. I'll be. Damned.
try this one- convert your url into NSdata
NSString* resourcePath = self.audioStr; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error = [[NSError alloc] init];
NSLog(#"url is %#",resourcePath);
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}`
Try to read the audio file with something like this:
audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:[NSString stringWithString:soundFilePath_]]
error:&error];