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

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];

Related

iOS AVAudioPlayer can't REplay when asked to play again at the end (works before and after end of file but not a THIS specific moment)

Sorry for the weird title but AVAudioPlayer acts a bit weird when it comes to playing a sound again, right before the sound that it's playing ends.
I can play an mp3 file when the player is not playing, when the player finished playing and when the player is playing.
I cannot play an mp3 when the song that is currently playing is about to end. This is my code, I documented the important bits.
-(void) play:(int)p{
if([players[p] isPlaying]){
NSLog(#"is playing");
[players[p] setCurrentTime:0.025f]; //when it's playing, set it to the start of the
//file so it starts playing again. Usually works
if(![players[p] isPlaying]){
NSLog(#"is not playing"); //although I just told the player to play again
//it sometimes returns that it in fact does NOT play
//so it's playing in the first if condition but
//not playing in the second if condition
[players[p] prepareToPlay];
[players[p] play]; //from here I try to resurrect the player numerous times
//but everything fails, I have to call the function again to make it work
[players[p] setCurrentTime:0.025f];
if([players[p] isPlaying]){
NSLog(#"playing"); //never gets called; although I tell it to play so many times
//it just won't do
}else {
NSLog(#"NOTplaying");
[players[p] play];
}
} else {
NSLog(#"is still playing");
}
} else {
NSLog(#"not playing at all");
[players[p] play]; } //if not playing, play. Always works
}
I think this might have something to do with how the AVAudioPlayer releases the file or that it's playing but is not playing anyumore in the millisecond after that. (playing during the first if condition but stopped right after that).
The window in which this occurs can easily be reproduced with with a 5sec audio file. Just wait till it's about to end and then click "play" and the method gets executed but nothing's playing. The method works in every other case (not playing and playing but not going to finish playing).

How to pause or stop the current playing audio?

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;
}
}

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