AVAudioPlayer could not stop playing music in iOS 7.1.2 - ios

AVAudioPlayer starts playing music when a local notification is shown to user. After clicking the notification and app runs in foreground I could not stop the music only in iOS 7.1.2. But I can stop it in iOS 8+.
Has anyone heard any iOS 7.1.2 bug about this issue reported by Apple?
I use the same instance that I start playing the music.
// start playing the music
[self.player prepareToPlay];
// Stop playing the music
[self.player stop];
[self.player setCurrentTime:0.0f];

For playing use:
[self.player prepareToPlay];
For pausing/stopping use:
if (self.theAudio.playing) {
[self.theAudio pause];
}
[self.theAudio stop];
self.theAudio.currentTime = 0;

Related

Play audio from video when phone is on silent mode

I am building an application that plays certain video files. However, when the phone is on vibrate the audio still plays. Apps like youtube still play the audio form their videos when the phone is on vibrate. How can I do this? I am using avplayer to play the video. Below is my code for playing the video
_player = [[AVPlayer alloc]initWithPlayerItem:pi];
AVPlayerViewController *playerController = [[AVPlayerViewController alloc]init];
playerController.player = _player;
[self presentViewController:playerController animated:YES completion:nil];
playerController.view.frame = self.view.frame;
[_player setMuted:false];
[self addChildViewController:playerController];
Found the solution. Adding this code allows whatever video is playing to produce audio even on silent mode.
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];

iOS AVPlayer Does Not Play the Next Song in a Playlist After App is Backgrounded Then Screen is Locked

I am working on this iOS app that plays music from URLs. Currently it has no issues playing through a playlist while the app is foregrounded, backgrounded, or even when the screen is locked ONLY IF the app was in the foreground prior to locking.
If the app is backgrounded THEN has the screen locked, the next song will load, but it will not play.
My question is: How can I get the AVPlayer to play the next song in a list while the app is in the background and the screen is locked?
Below is the code I am using to load and play the songs...
- (void)loadNextSong {
if (self.playlistIndex < self.playlist.count-1) { //check to see if the next track is in bounds of the playlist array
self.playlistIndex++;
[self.player pause];
[self serviceDidFetchSong:self.playlist[self.playlistIndex]];
[self.player play];
} else { //play the first song if we reach the end of the playlist
self.playlistIndex = 0;
[self.player pause];
[self serviceDidFetchSong:self.playlist[self.playlistIndex]];
[self.player play];
}
}
- (void)serviceDidFetchSong:(Song *)song {
if (song.audioFileURL != nil) {
if (![self.song.audioFileURL isEqual:song.audioFileURL]) {
if (self.timeIntervalObserver) {
[self.player removeTimeObserver:self.timeIntervalObserver];
}
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:song.audioFileURL options:0];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
//this is just for a visual of the song progress
__block __weak SongPlayerViewController *blockSelf = self;
self.timeIntervalObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds((1.0f/5.0f), NSEC_PER_SEC)
queue:NULL
usingBlock:^(CMTime time){
[blockSelf updateProgressBar];
}];
}
self.song = song;
}
}
After trying to debug with some console logs and break points, I have verified that [self.player play] is definitely being called and that the correct URL is loaded each time.
My info.plist file has audio and fetch enabled for the required background modes.
The next song just doesn't seem to play at all when the app has been backgrounded and the screen is locked.
Any insight or suggestions would be much appreciated. Thank you.
EDIT
This seems to be an intermittent issue which appears much less frequently since the release of iOS 9.3 which leads me to believe this may have been related to the OS...?
It works when you play it twice.
player?.play()
player?.play()

Play next video on button click in AVPlayer

I have 2 videos. I am using AVPlayer to play videos. The first one is loaded when App starts, and when I click on the next video, it should start playing that video from the start. How can it be done?
I am using the following code in attempt to switch to the next video
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithURL:url];
[self pause];
[self.moviePlayer replaceCurrentItemWithPlayerItem:playerItem];
[self.moviePlayer seekToTime:kCMTimeZero];
[self play];

pause player when going to background and resume when app returns to foreground ios

When playing a video in an iOs app I need it to pause when the app is sent to background and resume when the app returns from background.
- (void)onApplicationDidBecomeActive:(NSNotification *)notif
{
NSLog(#"player: app did become active...");
[self.videoPlayer play];
}
- (void)onApplicationDidEnterBackground:(NSNotification *)notif
{
NSLog(#"player: app did enter background...");
[self.videoPlayer pause];
[self.videoPlayer setFullscreen:NO];
}
On iOS7 this works ok but io iOs8 it seems that another player is created underneath the current one. (self.videoPlayer is a MPMoviePlayerController)
Why is this?

Play audio when already playing

My audio won't play unless its finished playing even if I stop it then play it.So if you play the sound and play it again before the sound clip reaches the end, won't play again. How can I make it play and start from the beginning even if its still playing.
-(void)playSound{
[avplayer stop];
[avPlayer play];
}
As per the documents -
The stop method does not reset the value of the currentTime property to 0. In other words, if you call stop during playback and then call play, playback resumes at the point where it left off.
Set the currentTime property to 0.0 before playing again.
-(void)playSound{
[avplayer stop];
avplayer.currentTime = 0.0;
[avPlayer play];
}

Resources