AVPlayer can only play up to 16videos - swift

I have implemented a pager in which each page shows the user a video. Each page has his own AVPlayer instanciated.
Everything work as expected until the 17th video.
I read a lot regarding this issue. It appears that this is an Apple limitation, where an app is limited to display the player layers.
The solution I tried (several sources says it fixes the issue) is to remove the player layer from its parent and set the player to Nil. So each time a page disappears (didDisappear) I call:
// player and playerLayer are init at viewWillAppear().
player?.pause()
player = nil
playerLayer?.removeFromSuperlayer()
That does not change anything... I am still limited to 16 video plays.
Thus, my question is:
1) Why this behavior? Is it really expected?
2) How do Musical.ly or Snapchat achieve to switch infinitly between videos?
Thanks a lot for your help.

Try to change your code with
player?.pause()
player?.replaceCurrentItem(with: nil)
playerLayer?.removeFromSuperlayer()
player = nil
setting nil item into player will stop playing.
1) Yes, it's expected behavior for AVPlayer. It was designed for video players creation or for displaying ads videos in games, but not for videos walls like has Instagram or similar services.
2) It's simple. They are not using AVPlayer. For your purpose you can use combination of AVAssetReader and AVSampleBufferDisplayLayer

Related

Unity VideoPlayer and WebGLMovieTexture cant play two videos in a row

I'm trying to play videos in Unity WebGL in the browser, but having lots of problems.
I tried two different video players and none of them work fully.
The WebGLMovieTexture player works like this
public WebGLStreamingVideoPlugin _videoPlugin = new WebGLStreamingVideoPlugin("http://www.example.net/video.mp4");
_videoPlugin.Play();
Basically when you want to play a video you create a new instance and give it the URL like above, and it plays great!!
The problem is when you want to stop that video, and play a different video, it seems impossible because there is no way dispose of the first video because there is only a Stop() in the API, which stops the playback but it continues to stream the video data from the internet in the background.
There is no way to delete the instance because Destroy() cant be called since that WebGLMovieTexture is not derived from monodevelop, and C# does not seem to give a way to delete an object (how silly). Even setting it to null doesnt do it, it continues to stream the video in the background.
So if you create a new instance in order to play a different video, you end up with TWO video streams, and if you do it again to play a third, you end up with THREE, and so on, so quickly you can see how bad that will end up.
My question is how to dispose of or destroy the first WebGLMovieTexture player, or maybe tell it to stop streaming the first video and start playing a different one?
The second player I tried is the VideoPlayer for WebGL in Unity Version 5.6.0b4, with this one I can only get it to play a video if I hardcode the URL in the inspector, if I put the URL in code it doesn't play it in the browser.
vPlayer = gameObject.GetComponent<UnityEngine.Video.VideoPlayer>();
if (vPlayer.isPlaying) {
Debug.Log("STOPPING PLAY");
vPlayer.Stop();
}
vPlayer.url = url;
vPlayer.isLooping = true;
vPlayer.frame = 0;
vPlayer.targetCameraAlpha = 1F;
vPlayer.Prepare();
vPlayer.Play();
And to get it to play a second video I suspect I will have the same problems as the other one.
Anybody have any ideas that can help me?

AirPlay flickering when switching between movies

I have an app that shows list of movies in a table view. When I play them one after another on device it works just great. But when I switch to Apple TV over AirPlay it doesn't work anymore. It play's the first video on ATV ok but after a switch to the next video the screen on ATV start blinking/flickering and after a few seconds it falls back playing on a device. I am using MPMoviePlayerController for playing stream videos.
I also found out that if previous video finished playing over AirPlay it tries to start the next one over AirPlay also. Is this intended behaviour?
Is this kind a related with property allowsAirPlay?
I think I've found a solution. Before you switch playing another video you should stop the previous one:
[self.moviePlayer stop];
This is not needed if you are not playing via AirPlay cos the next video will automatically stop previous one by nature - you can not play two videos at once.
But If you do play video over AirPlay you need to stop previous one first and than play the next one.
This solution works for me.

When my embed youtube video finished I can't play it again?

I have a working webview in Titanium with an embeded youtube video. The video plays nice, I can stop it, resume it and so on. But when the video plays to the end and I press "done". The webview goes black with the text: YouTube. Nothing else. I would like there to be the same thumb and playbutton as before I played the video.
My embeded code looks like this:
html:'<html><head></head><body style="margin:0"> <embed id="yt" src="'+tubeURL+'" type="application/x-shockwave-flash" width="150" height="113"></embed></body></html>',
I've googled this problem for days now. I hope someone can help me here.
Cheers
//Martin
EDIT: This solved it. Put &controls=1&rel=0 in the end of the URL.
YouTube players can be embedded in a web page using either an < iframe> tag or an < object> tag.
By appending parameters to the SWF or IFrame URL, you can customize the playback experience in your application. For example, you can automatically play videos using the autoplay parameter or cause a video to play repeatedly using the loop parameter or in your case hide or show controls.
controls
Values: 0, 1, or 2. Default is 1. This parameter indicates whether the video player controls will display. For AS3 players, it also defines when the Flash player will load:
controls=0 – Player controls do not display in the player. For AS3 players, the Flash player loads immediately.
controls=1 – Player controls display in the player. For AS3 players, the Flash player loads immediately.
controls=2 – Player controls display in the player. For AS3 players, the Flash player loads afer the user initiates the video playback.
The correct answer for your question would be controls=2.
SOURCE INFO: https://developers.google.com/youtube/player_parameters#controls

iphone, my AVPlayer player is playing video really fast

My application is using AVPlayer to play video. There is a serious problem that my app currently experience. Sometimes, it is playing the video very fast although I checked to make sure that I either set the self.player.rate = 0.3 or self.player.rate = 1 or not set the rate at all. This only happens sometimes and it happens to all the videos I have.
Can anyone give me any hint on the current problem?
I finally know the answer. I called [player setRate:0.3] and [player play] at the same time and the second call override the first call, which sets the rate to be 0 (normal). The method setRate: already set the rate for the player and play the video already

MPMoviePlayer or AVPlayer Frame advance

Is there a way to playback a video file frame by frame using either MPMoviePlayer or AVPlayer?
Or even another movie player that I do not know about?
Here is what I want to do. I want to load a video into a fullscreen player and move the content one frame at a time based on user interaction. This will need to be pretty solid as I would need to accurately control what frame the movie player was displaying at any one time.
Ideally I would love to know if it were possible to load a video and control the frame displayed using code.
I know that I could do this using a UIImageView animation but tests show that this uses FAR to much memory.
When using AVPlayer, you can use the - (void)stepByCount:(NSInteger)stepCount method of its current AVPlayerItem to step forward or backward.
AVPlayer *mPlayer = [AVPlayer playerWithURL:url];
[mPlayer.currentItem stepByCount:1];