AVAudioPlayer is not restarting the bg music? - ios

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.

Related

AVAusioSession pty error in Simulator

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

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.

How perform streaming of .m4a song file with AVAudioPlayer?

I'm trying to play this song from itunes store while I'm downloading it
Why the code below doesn't play?
NSLog(#"start");
NSURL *songUrl = [NSURL URLWithString:#"http://a1804.phobos.apple.com/us/r1000/064/Music/v4/9b/b3/c7/9bb3c7dc-a06f-f18c-3e41-2ce1e36f73b4/mzaf_7432104896053262141.aac.m4a"];
NSError* errorVar = nil;
AVAudioPlayer* song = [[AVAudioPlayer alloc] initWithContentsOfURL:songUrl error:&errorVar];
[song prepareToPlay];
[song play];
I think you have to use AVPlayer instead of AVAudioPlayer.
Apple documentation suggests AVAudioPlayer is used for playback of audio data from a file or memory. AVPlayer works equally well with local and remote media files.
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
[player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
Refer Apple Docs for more info.
You can do
// soundUrl can be a remote url, don't need to be a file url
NSData *data = [NSData dataWithContentsOfURL:soundUrl];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil];

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...

IOS: AVAudioPlayer management memory

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.

Resources