1

I am using AVPlayer to play a video from an https url with a setup this:

player = AVPlayer(url: URL(string: urlString))
player?.automaticallyWaitsToMinimizeStalling = false

But since the video is a little long, there is a short blank screen delay before the video actually starts to play. I think this is because it is being loaded from https.

Is there anyway to remove that delay by making AVPlayer play the video right away without loading the whole thing?

I added .automaticallyWaitsToMinimizeStalling but that does not seem to make a difference.

If anyone has any other suggestions please let me know.

Arnab
  • 4,216
  • 2
  • 28
  • 50
Micro
  • 10,303
  • 14
  • 82
  • 120
  • Take a look at the answer to this question here: https://stackoverflow.com/a/47055601/4844273 -You should use AVPlayerItem and AVPlayerLayer. – elarcoiris Jun 30 '19 at 04:16
  • 1
    @elarcoiris that did not answer it. – Micro Jun 30 '19 at 21:43
  • 2
    The delay is inevitable so why not just cover it? – matt Jul 02 '19 at 03:31
  • @matt how do other apps like snapchat and instagram not have it then? – Micro Jul 02 '19 at 05:15
  • Normally first screen of the video is used as a thumbnail. when the video loaded and starts playing it doesn't feel like flickering so users think it is the video there. Ask your video provider has a progressive download feature if so check @teodora-georgieva's answer if not ask your video provider to serve video as HTTP Live Streams (HLS). In any case you will need the first frame while getting the first buffer. – ugur Jul 08 '19 at 22:47

4 Answers4

2

I don't think there is nothing to do with loading from https. what is your video file format? I think you are thinking of adaptive bitrate streaming behavior.

https://en.wikipedia.org/wiki/Adaptive_bitrate_streaming#Apple_HTTP_Live_Streaming

HTTP Live Streaming (HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc. as part of QuickTime X and iOS. HLS supports both live and Video on demand content. It works by breaking down streams or video assets into several small MPEG2-TS files (video chunks) of varying bit rates and set duration using a stream or file segmenter. One such segmenter implementation is provided by Apple.[29] The segmenter is also responsible for producing a set of index files in the M3U8 format which acts as a playlist file for the video chunks. Each playlist pertains to a given bitrate level, and contains the relative or absolute URLs to the chunks with the relevant bitrate. The client is then responsible for requesting the appropriate playlist depending on the available bandwidth.

For more information about HTTP Live Streaming

https://developer.apple.com/documentation/http_live_streaming

This tutorial includes some experiments on HTTP Live Streaming version and Non-HTTP Live Streaming version.

https://www.raywenderlich.com/5191-video-streaming-tutorial-for-ios-getting-started

HMHero
  • 2,333
  • 19
  • 11
0

Have you tried using AVPlayerItem's preferredForwardBufferDuration? You can manage how long AVPlayer continues to buffer using this property.

player.currentItem?.preferredForwardBufferDuration = 1

From Apple's own documentation:

The duration the player should buffer media from the network ahead of the playhead to guard against playback disruption.

This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.

Community
  • 1
  • 1
0

Suggestion:

As a video is streaming, we are also relying on a network connection. So for poor network connection, It always has a chance to display a blank screen.

We can do like, We can fetch thumbnail of streaming video on the previous screen from the server or can generate thumbnail from application side from streaming URL. When streaming screen open, display thumbnail to a user with streaming indicator and when video starts streaming hide thumbnail.

Sagar Thummar
  • 2,034
  • 15
  • 21
0

In that particular case you can place an UIImageView above to the AVPLayerlayer view.

  1. That image work as a landscape/cover image of your video which matched the first frame of your video along an UIActivityIndicator on subview.

  2. Now hide that image when video got about to play.

This helps to hide the black frame of your video as it inevitable to handle the initial buffer state of the video.

Paul.V
  • 386
  • 1
  • 3
  • 13