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];
Related
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()
I am trying to play audio in iOS using AVPlayer but i am not able to see any Deafault UI fro playing the audio. Here is my code
NSURL *url = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
player = [AVPlayer playerWithURL:url];
[player play];
Audio os played but not any audio player UI is diplayed as in case of MPMoviePlayerController UI is shown while playing the video.Please tell me the solution here?
iOS is not providing any native kind of the UI for Playing Audio for that.you create own UI and that manage by the developer thru AVAudioPlayer class.
Here is tutorial for the custom Playing Audio with AVAudioPlayer
I have a simple app that records video at 120fps, saves it to the documents directory, and then uses an instance of AVPlayer to play it back.
If I set the playback rate to 1.0 (for the AVPlayer instance), the playback is a bit choppy (probably more along the lines of 60fps). However, if I interrupt the playback by pressing the "play" button (which then sets the playback rate to 1.0 again!), it plays back very smoothly.
For instance, this is what occurs when the user presses the play button (and yes, I observe the 'status' property of AVPlayer before allowing the user to play the video):
- (IBAction)playButtonPressed:(id)sender
{
[self.videoPlayer seekToTime:kCMTimeZero];
[self.videoPlayer setRate:1];
self.videoPlayer.volume = 1;
}
And this is my simple player-setup code:
- (void)setUpAVPlayer
{
self.videoURL = [[NSURL alloc] initFileURLWithPath:self.videoURL.path];
self.videoPlayer = [AVPlayer playerWithURL:self.videoURL];
[self.previewLayer setPlayer:self.videoPlayer];
// observe player status
[self.videoPlayer addObserver:self
forKeyPath:#"status"
options:0
context:nil];
}
There really is nothing unusual about what I'm doing. It plays back video fine. It's only the case where the video was recorded at 120fps that there is a playback issue.
(Also, this is running on my iPhone5s since it is the only device that supports 120fps.)
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];
}
I'm using AVAudioPlayer to play a mp3 file. instead of playing whole mp3, I want to specify play time range, like from 2 seconds to 5 seconds. how to specify the play time range?
You can either add an observer to playback and stop it when currentTime will become the time you want or you can cut your mp3 file to specified time and play it. There are various ways for doing this thing.
You might be able to do it using AVPlayer, AVPlayerItem and AVAsset. The AVPlayerItem is able to seek in an audio file.
I haven't tested it, but it might look something like this.
AVAsset *asset = [AVAsset assetWithURL:URL_TO_YOUR_MP3];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
/* start playback at 2 seconds */
[playerItem seekToTime:CMTimeMake(2.0, 1.0) completionHandler:^(BOOL finished) {
/* Instantiate player */
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
/* Start playback */
[player play];
/* Pause player in 3 seconds */
[player performSelector:#selector(pause) withObject:nil afterDelay:3.0];
}];