swiftvideo-streamingavfoundationavplayeravplayeritem

Is it possible to configure custom headers for AVPlayerItem URLRequest?


I have HTTP Live Stream URL and I use AVPlayerItem to play the stream. I can init AVPlayerItem only with URL but i need to send some custom HTTP Header Fields in request so that the server responds.

1) Is it possible to configure custom headers for AVPlayerItem URLRequests?

2) If not, what other options can you recommend?


Solution

  • when you are creating AVURLAsset you can add an option AVURLAssetHTTPHeaderFieldsKey and provide your HTTP headers. The code should look similar to:

    let asset = AVURLAsset(url: url, options:["AVURLAssetHTTPHeaderFieldsKey": yourHttpHeaders])
    let item = AVPlayerItem(asset: asset)
    let player = AVPlayer(playerItem: item)