I’ve been trying to play a .ts (Transport Stream) video file in my SwiftUI app using AVPlayer, but I’m running into issues. The .ts file plays fine in VLC and other media players, so I’m confused as to why it won’t work in my iOS app.
I created a SwiftUI app and used AVPlayer and AVPlayerViewController to try to stream the video from the .ts URL.
The URL is valid and works when I test it in VLC, so I know the link isn’t broken.
struct ContentView: View {
@State private var player: AVPlayer?
let url = URL(string: "http://alibaba.gotoplucky.com:23000/live/Damianomaca/EfYpS6xSVy/260.ts")!
let url3 = URL(string: "http://alibaba.gotoplucky.com:23000/live/Damianomaca/EfYpS6xSVy/26060.ts")!
@State var isVPlayerOut = false
var body: some View {
Button {
isVPlayerOut.toggle()
} label: {
Text("Open Video")
}
.sheet(isPresented: $isVPlayerOut) {
VideoPlayerView(url: url3)
}
}
}
#Preview {
ContentView()
}
struct VideoPlayerView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> AVPlayerViewController {
let playerViewController = AVPlayerViewController()
let player = AVPlayer(url: url)
print("(url)")
player.play()
playerViewController.player = player
return playerViewController
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
// Optional updates
}
}
I would appreciate any suggestions how to play this ts file Thanks!
9
“The .ts file plays fine in VLC and other media players,
so I’m confused as to why it won’t work in my iOS app.”
AVPlayer
expects an M3U8 playlist file which lists the .ts
file path inside.
Some steps you can try…
(1) Create the playlist dynamically
By using a String to contain the playlist’s text.
let my_string_m3u8 = """
#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:0
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:0.0,
http://alibaba.gotoplucky.com:23000/live/Damianomaca/EfYpS6xSVy/26060.ts
#EXT-X-ENDLIST
"""
(2) Save text with .M3U8 extension
According to Hacking With Swift
you can save a text file (the M3U8 data) using:
let tmpFileURL = getDocumentsDirectory().appendingPathComponent("my_test_stream.m3u8")
do
{ try my_string_m3u8.write(to: tmpFileURL, atomically: true, encoding: String.Encoding.utf8) }
catch
{
// failed to write file –
// is bad permissions, bad filename, missing permissions,
// or more likely it can't be converted to the encoding
}
Also consider saving M3U8 as binary (instead of text), if still not working to show playback.
alternate version from this reference (Answer):
let tmpFileURL = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("my_test_stream").appendingPathExtension("m3u8")
let wasFileWritten = (try? data.write(to: tmpFileURL, options: [.atomic])) != nil
if !wasFileWritten{ print("File was NOT saved") }
Result: Should be a file in your app’s temp directory called my_test_stream.m3u8
.
(3) Try doing some video playback
Where…
let url4 = URL(string: "path-to-your-temp-dir-m3u8-file")!
Could then be used as:
let player = AVPlayer(url: url4)
print("(url4)")
player.play()
Hopefully you’ll now get some playback…