1

I'm trying to play an m3u8 stream in VideoPlayer, but it only plays one frame of the stream before freezing. The built-in play button is unresponsive. I get these messages which might be related:

[api] -[CIImage initWithCVPixelBuffer:options:] failed because the buffer is nil.
Metal API Validation Enabled
[MADService] Client XPC connection invalidated

I thought the problem could be the video player view re-rendering, but I tried isolating the player and no change.

import SwiftUI
import AVFoundation
import AVKit

struct VideoPlayerView: View {
    @State private var player: AVPlayer = AVPlayer()
    @State var url: URL?
    var body: some View {
        return
            VideoPlayer(player: player)
                .onAppear {
                    playVideo()
                }
    }
    
    func playVideo() {
        if let newUrl = url {
            let newPlayerItem = AVPlayerItem(url: newUrl)
            player.replaceCurrentItem(with: newPlayerItem)
        } else {
            player.replaceCurrentItem(with: nil)
        }
    }
}

The function playVideo is correctly called once. Can you walk me through how I can diagnose this problem?

edit I tried playing the URL in VLC on MacOS and it works correctly there.

zakdances
  • 22,285
  • 32
  • 102
  • 173
  • 1
    Having all these reference types and a State could pose issues. I would start there. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Mar 31 '23 at 22:16
  • @loremipsum Yes, you're right, thank you. I'll see if I can create a simpler reproducible example. – zakdances Apr 01 '23 at 12:56
  • @loremipsum I simplified the example. I also checked to make sure the media item wasn't being set repeatedly. – zakdances Apr 04 '23 at 22:28
  • You haven't provided links to a video but if I remember correctly the m2u8 file has to have a very specific format. But somewhere in Apple's samples there is a sample HLS player, it is pretty simple from what I remember and it handles downloading the video and buffering – lorem ipsum Apr 04 '23 at 23:21

0 Answers0