AVAusioSession pty error in Simulator - ios

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

Related

AVAudioPlayer is not restarting the bg music?

I'm on latest Xcode version. I'm using following code to restart my background music. But these codes are not restarting the background music.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"game" ofType:#"wav"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player stop];
player.currentTime = 0;
[player play];
To use AVAudioPlayer, you need a strong reference to it. Try to set the player as a property of your controller, then use [self.player play]
Declare AVAudioPlayer into .h file.

iOS: playing sound while other apps are playing

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.

Issue with playing music file during a phone calling iOS sdk

I have two pieces of code:
Code 1 - This is playing a music file via the in-ear speaker:
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"8Ocean.mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory];
NSError *error=nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if(!error)
{
[self.audioPlayer prepareToPlay];
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
[self.audioPlayer play];
}
else
{
NSLog(#"%#",error.localizedDescription);
}
Code 2- This is playing a music file during a phone call (is routed to in-ear or speaker phone depending on user setting during the phone call)
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"8Ocean.mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory];
NSError *error=nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if(!error)
{
[self.audioPlayer prepareToPlay];
self.audioPlayer.volume=10.0;
UInt32 sessionCategory = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[self.audioPlayer play];
}
else
{
NSLog(#"%#",error.localizedDescription);
}
My issue is:
Code 1 and 2 work well independently. However, after using code 1, code 2 will not work at all (unless the app is killed and restarted)
I have tried everything I can think of but cannot work out where the problem is from.
Your app needs to handle audio session interruptions from phone calls in order for audio processing not to be killed for your app.

Having trouble with audio timing

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

AVAudioPlayer hitch before playing

I've an AVAudioPlayer with a MP3-file. But before playing, you'll hear a hitch.
This is my code:
NSString *geluid = [NSString stringWithFormat:#"RG_%#", nummer];
NSString *path = [[NSBundle mainBundle] pathForResource:geluid ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio prepareToPlay];
[theAudio play];
NSURL *url = [NSURL fileURLWithPath:path];
avplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[avplayer prepareToPlay];
[avplayer play];
For example: You hear "Ssstop here", in stead off "Stop here".
Please help me to fix it.
Is this code running two AVAudioPlayer instances at the same time ?
Why are you using both avplayer and theAudio ?
From looking at it it looks like you should get rid of one of them...

Resources