How to pause/resume avplayer preload - ios

I'm using AVPlayer to play video from Internet, is there a way to pause/resume preloading video when pause/resume playing?

One way to do that would be to save your AVURLAsset to a variable (tempAsset = player.playerItem.asset), and then replace the playerItem in your player with nil. When you are ready to play it again, you can create a new playerItem from the asset that you saved previously.

I have same problem and i used buffer limit to stop preloading in my code.
This works for me.
You can also try setting buffer limit to stop preloading video when you click on Pause or Resume.
I hope this will help you.

The duration the player should buffer media from the network ahead of the playhead to guard against playback disruption.
player.currentItem?.preferredForwardBufferDuration
When you work with AVPlayerItem you can set time interval for the buffer. Example:
AVPlayerItem(). preferredForwardBufferDuration = TimeInterval(exactly: 100)!

Related

In which cases avplayer should play? Like play() function, set player rate

I set player to pause and i am setting player rate after the pause. After setting player rate avplayer starting the playing without play() method, so i want to know in which other cases that avplayer should play without play() method.
I find that when we set rate or seek to specific time than it is play but i want to know is there other cases when avplayer should play.
Please check https://github.com/teamSolutionAnalysts/sa-plug-avplayer which is very easy plug and play module, lets you play video easily and you tube embedded url supported.

Play Song at specific index of MPMediaItemCollection in Swift

I try to make my own Musicplayer with Swift. I need to jump to a specific song/index of my MPMediaItemCollection and start to play it but i can only find methods like skipToNextItem() and skipToPreviousItem(). Is there any other way to do that than with a loop?
let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()
According to the documentation, we use the nowPlayingItem property.
To specify that playback should begin at a particular media item in the playback queue, set this property to that item while the music player is stopped or paused.
So, it sounds like you should stop or pause the player, set the nowPlayingItem, and then call play again.
player.nowPlayingItem = mediaCollection.items[selectedIndex]
player.play()

iOS managing AVPlayer buffer prompt

I use AVPlayer to play a video that is on the internet. So my question is how can I give proper prompt to the user when he needs to wait? (e.g. an animating UIActivityIndicatorView whenever he needs to wait for the player to load some more data or seek to a specific time)
I spinning the UIActivityIndicatorView whenever I set a new playerItem to my AVPlayer, then I observe status of the playerItem, when it becomes AVPlayerItemStatusReadyToPlay, I stop the spinning and start play. So whenever a new playerItem is set on my AVPlayer, a spinning will be there until it is ready to play.
I also want to give my user the spinning when he needs to wait for the playerItem to load more buffer when network connection is bad. I tried observing playbackBufferEmpty and playbackLikelyToKeepUp but did not work as I expected. I also messed with the rate property of my AVPlayer, still not work quite well.

AVPlayer removes background music

I've been using giffycat to decode, store, and play gifs in my app. I am making it so that it can easily load a gif in a UICollectionView's cell, so I have decided for each gif model to have its own AVPlayer. I have noticed that simply by creating an AVPlayer, shown bellow, audio from other apps is killed! Annoying for both the user and the creater!
// Create an AVURLAsset with an NSURL containing the path to the video
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:_mp4] options:nil];
// Create an AVPlayerItem using the asset
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:item]; //if this line is commented out, I hear audio, else audio from Spotify is quickly killed...
Since these videos are just gifs, I am wondering if there is some way to unassign the audio session. I do not know much about this. ples help!
Turns out the answer is pretty easy, after a little googling and documentation reading...
The solution is
// audio session
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient,
withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
oops, just realized I am posting my question in objC and answer in Swift. Well tough, because that's life sometimes.
AudioSession is a singleton for your entire app to rule how your application mingles with the other sounds of the system and other apps! The default audio session is
playback is enabled, recording is disabled
when user moves silent switch to "silent" your audio is silenced
when user presses sleep/wake button to lock screen or auto-lock period expires, your audio is silenced
when your audio starts, other audio on device (music) is silenced.
CategoryAmbient tells it not to do 4
Nice documentation!
https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1
Set the audioMix property of the AVPlayerItem to nil before creating an AVPlayer from it to remove the audio track from the asset.

Streaming multiple music with AVPlayer

I have an app that I want to make which requires streaming audio files from web server. I use AVPlayer as the player. The problem is, some responses that I am receiving from the server has two audio files on it. And this makes the streaming hard. My audio player UI by the way is like this:
I have a slider for the streamed time ranges (the black one) and another slider for the AVPlayer.currentTime. I have two audio music streamed and their music durations are added together which is now 8:46. My first music has 6 minutes duration and my second music has 1:46. As you can see in the above photo, my streamed time ranges slider indicates that AVAsset has completely streamed the first music. My problem is, I can't continue streaming and playing the next music when the first one has reached it's end. It just stop and the slider value gets back to 0.
What I want to accomplish is that when the first item has reached its end, AVPlayer would load another player item and that would be the second music. Will continue to play and slider will continue to move.
Is this possible? What are your suggestions? Thanks experts.
To load audio files one after the other, you can use AVQueuePlayer.
NSURL *song1 = [NSURL URLWithString:#"audio url1"];
NSURL *song2 = [NSURL URLWithString:#"audio url2"];
AVPlayerItem *playerItem1 = [[AVPlayerItem alloc] initWithURL:song1];
AVPlayerItem *playerItem2 = [[AVPlayerItem alloc] initWithURL:song2];
NSArray *songs = #[playerItem1, playerItem2];
self.queuePlayer = [[AVQueuePlayer alloc] initWithItems:songs];
[self.queuePlayer play];
Hope this might help you.

Resources