AVAudioPlayer Play/Pause/Stop local audio file on three different buttons - objective-c

I want to play/pause/stop local audio file. Buttons for all functionalities are different. Check attached screenshot for reference.
Below is my click events,
-(IBAction)click_Play:(id)sender {
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Here, I am facing issue in pause functionality. After pausing the audio when I click on play button again. It start playing from starting and not at the point where I pause it. Any solution?
Thanks in advance!

Change your code to this:
-(IBAction)click_Play:(id)sender {
if (!player)
{
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
else {
[player play];
}
}

Don't create instance of AVAudioSession and AVAudioPlayer again and again in play button action. So, keep it simple, create instance of AVAudioSession and AVAudioPlayer in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
}
-(IBAction)click_Play:(id)sender {
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Edit:
Call this method when you what to change track. I'll never recommend to create instance of object again and again
-(void)changeTrackWithPath:(NSString *)path{
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
[player replaceCurrentItemWithPlayerItem:playerItem];
[player play];
}

Related

how to play mp3 in asset url using AVPlayer

i have url in ipod-library://item/item.mp3?id=8788117795090152241
how to play mp3 in this url
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url_selectedSong];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
but not play song
Use this:
NSError * error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if(player) {
[player play];
}

How to play audio, even when mute button is on?

I'm trying to play an audio file when a button is tapped.
If I step through my code it works, but if I just tap on the button no sound is played.
Here is my code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
AVAudioPlayer * player;
NSString * path = [[NSBundle mainBundle] pathForResource:#"soundOne" ofType:#"mp3"];
NSURL * url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player play];
Is this the appropriate way to play audio? Nothing is being flagged as being deprecated.
I also see this message in the debugger:
AudioQueue: request to trim 0 + 2690 = 2690 frames from buffer containing 1152 frames
EDIT:
This code works.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"soundOne"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
How can I play audio with the mute button on?
Here is the code I have tried for this:
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
//NSLog(#"audio route: %#", state);
return (CFStringGetLength(state) <+ 0);
}
return NO;
Stick this before you call play:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
That should do it.

AVAudioPlayer play audio properly in Ipod Touch 4g but not in Ipad, Ipad2, why?

Here is my code:
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:[settings alarmAudioName] ofType:#"mp3"];
NSLog(#"%#",path);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
NSLog(#"%#",[NSURL fileURLWithPath:path]);
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
{
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(#"Should have been played");
}
NSLog:
/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA Development.app/Song 1.mp3
file://localhost/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA%20Development.app/Song%201.mp3
Its works correct in Ipod Touch 4g, but not working in Ipad, Ipad2, why?
Thanks the answers,

Prompting AVAudioPlayer to play based on a timer

I am trying to setup a stopwatch alarm that is enabled via UISwitch. I want the alarm to be triggered after an x amount of secs after the timer has started. I have an ivar int variable (timerCount) that holds the amount of secs that has passed and I want to play a sound based on that ivar. When the timer begins and I enable the switch I would hope that the player would fire at 10 secs, but that is not the case. I know the "if statement" is the culprit because when I remove it the AVPlayer will play the wav file when the switch is enabled. I just need another set of eyeballs to look at this and figure out what I missed.
Here is the code snippet:
- (IBAction)timerSound{
if (voiceSwitch.on){
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"horn"
ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
if (timerCount == 10)
[self.player play];
}
else {
[self.player stop];
}
}
You are looking for playAt:.
Example based on yours –
- (IBAction)valueChanged:(id)sender {
UISwitch *aSwitch = (UISwitch*)sender;
if ( aSwitch.on ) {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"horn"
ofType:#"wav"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];
[audioPlayer playAtTime:(audioPlayer.deviceCurrentTime + 3)];
} else {
[audioPlayer stop];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self.theSwitch setOn:NO animated:YES];
[player release];
}

Cocoa Touch - Loading AVAudioPlayer

I have this code to play a sound but the first time you play it it takes a good 5 seconds to load...
How can I speed this up?
-(IBAction)playSound{ //play the cricket noise
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
You can try by putting the following section of your code in viewDidLoad so that the audioPlayer instance is ready to play before it is asked to actually play the audio:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;