How to pause or stop the current playing audio? - ios

In my application, I have one audio file. I want pause or stop the audio by clicking the button. I have tried with some code, but it still plays the audio. Please tell how to pause the audio or stop it by clicking the button.
Stop code.
- (IBAction)back:(id)sender {
[self.audioPlayer isPlaying];
[self.audioPlayer stop];
}
I have used the above to stop the playing audio but its not working.
where am I doing wrong? How to stop the audio?

Stopping Audio is easy just you need to take 2 Action Method for 2 separate buttons 1->Play and 2->Stop then code is as fallows
-(IBAction)Play:(id)sender
{
[self.audioPlayer play];
}
-(IBAction)Stop:(id)sender
{
[self.audioPlayer stop];
}
Another way is to use BOOL and use the 1 Action method to play and stop like this
First declare BOOl object
BOOL AudioBool;
-(IBAction)Audio:(id)sender
{
if(AudioBool == YES)
{
[self.audioPlayer play];
AudioBool = NO;
}
else
{
[self.audioPlayer stop];
AudioBool = YES;
}
}

Related

streaming audio file is playing after getting pop to the View controller

I'm using AVPlayer to stream an audio file for my project. The problem I'm facing is the audio file plays after getting pop from the AudioPlayerVC.
Everything is okay when the audio plays on the AudioPlayerVC. It stops, plays and pauses too on AudioPlayerVC. But on the buffering time when I return back, it plays after some sec. I can listen to sound but cannot stop or do anything else.
- (IBAction)backAction:(id)sender {
if (_audioToPlay.playing == YES) {
[_audioToPlay stop];
} else if (_audioToPlay.playing == NO || _audioToPlay.volume == 1.0 ) {
_audioToPlay.volume = 0.0;
[_audioToPlay stop];
}
[self.navigationController popViewControllerAnimated:YES];
}
If below is not working then Refer avplayer-continues-to-play-after-viewcontroller-is-removed-from-navigationcontroller
You can do is firstly pause player :
[yourAVPlayer pause];
Then pop to Parent View Controller
[self.navigationController popViewControllerAnimated:YES];

AVAudioPlayer resume audio clips from the same state after interruption

I'm using AVAudioPlayer instances in my project to play both background music clips and voice audio clips in sequence. I have to sync the audio with the animations displayed. It works great except in a scenario (i.e) after an interruption such as a call. If the interruption begins the animation will get paused , and if ends it will be resumed, audio should be in the same way so that it could be synced perfectly.
I have used delegate methods of AVAudioPlayer which get called during an interruption,
- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
[self pauseAllPlayers];
}
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player withOptions:(NSUInteger) flags{
[self playAllPlayers];
}
-(void)pauseAllPlayers
{
if(musicPlayer1)
[musicPlayer1 pause];
if(musicPlayer2)
[musicPlayer2 pause];
// ... paused all players the similar way
}
-(void)playAllPlayers
{
if(musicPlayer1)
[musicPlayer1 play];
// .. played all players the similar way
}
I have also set delegate to self. It works fine , but the audio clip not getting resumed from the same state after interruption, it plays the clip from the beginning. I have also tried the following:
1)
- (void)endInterruptionWithFlags:(NSUInteger)flags {
if (flags) {
if (AVAudioSessionInterruptionFlags_ShouldResume) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{
[self playAllPlayers];
});
}
}
}
2)
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player withOptions:(NSUInteger) flags{
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(resumeBG) userInfo:nil repeats:NO];
[musicPlayer1 play];
}
-(void) resumeBG
{
[musicPlayer2 play]; // This is the player that plays background music.
}
3)
- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
[self stopAllAudioPlayers];
}
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player withOptions:(NSUInteger) flags{
[self playAllPlayers];
}
-(void) stopAllAudioPlayers
{
if(musicPlayer1)
[musicPlayer1 stop];
if(musicPlayer2)
[musicPlayer2 stop];
//...
}
But these are not working. I need to resume both BG and voice audio clips from the same state when the interruption begins. Is there a way to do that with AVAudioPlayer itself?
EDIT:
Based on Duncan C's answer I have tried the following:
- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
[self storeTime];
}
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player withOptions:(NSUInteger) flags{
[self resumeAtTime];
}
- (void) storeTime
{
player1Time = [player1 currentTime];
player2Time = [player2 currentTime];
}
-(void) resumeAtTime
{
[player1 playAtTime:player1Time];
[player2 playAtTime:player2Time];
}
I have also tried using (player1.deviceCurrentTime + player1Time) in resumeAtTime function. Is that the correct way to do so? Also I have tried using player.currentTime/player1.rate
The docs don't make it clear if the play method plays a sound from the beginning or continues after a pause. It just says that it plays the sound, which suggests playing from the beginning. Your findings suggest that that is what it does.
Here's what I would do. Use the currentTime method to get the playback point for your sound(s) before pausing them.
Then use playAtTime to resume each sound at the time it left off.

AVAudioPlayer only plays a custom audio file for one second

In my application I am using an AVAudioRecorder object to allow the user to record a sound file, and when the user is done recording they can play the sound via the AVAudioPlayer. The issue I'm having is when the user tries to play the audio file it only plays for one second. What is even more odd is that this code worked perfect on iOS 5, but since upgrading to iOS 6 this issue has started to happen.
I have checked the file that is being created via iTunes file sharing, and I'm able to play the entire sound file on my desktop.
Below I have pasted the code from my manipulation file that loads the audio player. From what I can tell the
- (void)playRecordingBtnClk:(id)sender
{
NSLog(#"Play btn clk");
if (!isRecording) {
// disable all buttons
[_recordButton disableButton];
[_stopButton disableButton];
[_playButton disableButton];
[_saveButton disableButton];
// create the player and play the file from the beginning
if (audioPlayer) {
[audioPlayer release];
audioPlayer = nil;
}
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];
audioPlayer.delegate = self;
audioPlayer.currentTime = 0.0;
[audioPlayer prepareToPlay];
if (error) {
NSLog(#"Error Playing Audio File: %#", [error debugDescription]);
return;
}
[audioPlayer play];
[self startTicker];
}
}
It appears that the
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
method is being called after one second which is why the audio player stops. Anyone have any ideas what is going on here?
For iOS 6.0, add two more lines:
soundPlayer.currentTime = soundPlayer.duration + soundPlayer.duration;
soundPlayer.numberOfLoops = 1;
Not a perfect solution. We may need to wait for iOS 6.0.1/6.1 update.
For more, please visit this, someone commented there, saying it might be a problem of CDAudioManager.

iOS AVPlayer Play/Pause button issue

I am using AVPlayer to play a live stream from the Internet using the following code :
NSString *u = #"http://192.192.192.192:8036";
NSURL *url = [NSURL URLWithString:u];
radiosound = [[AVPlayer alloc] initWithURL:url];
[radiosound play];
And I have one button play :
[radiosound play];
and Pause :
[radiosound pause];
My issue is that I want to use only one button Play/Pause, but when I am using this code
if (radiosound.isPlaying) {
[radiosound pause];
} else {
[radiosound play];
}
My app crashes, because AVPlayer doesn´t recognize "isPlaying".
Any tips?
AVPlayer doesn't have an isPlaying property. Use the rate property (0.0 means stopped, 1.0 playing).
if (radiosound.rate == 1.0) {
[radiosound pause];
} else {
[radiosound play];
}
You can look at the AVPlayer class reference here.
After some research I found out that when there is no network connection, AVPlayer still sets the rate to 1.0 after receiving a -play message.
Thus, I also check for the currentItem and modified my method like that:
-(BOOL)isPlaying
{
if (self.player.currentItem && self.player.rate != 0)
{
return YES;
}
return NO;
}
Please share your opinion if you think something is wrong with this approach.

iphone stop AVAudioPlayer

I want to stop the AVAudioPlayer when click on new tab. Stop an Pause button are working properly but if i am click on new tab bar the player is to be play in background so how can i stop the player.
I used:
-(void)stop_play
{
[player stop];
play.hidden=NO;
stop.hidden=YES;
}
when you click on new tab,in your action method write code like this
if(audioPlayer.playing){
[audioPlayer stop];
}
when your AVAudioPlayer playing, set one bool var as YES.. than call stop maethod with passing this bool var.. like Code here
if(isPlay)
{
isPlay = NO;
[audioPlayer stop];
}
after than you play audio set bool var again YES...

Resources