AVPlayer Won’t Play .ts Video Stream, But It Works in VLC

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
}
</code>
<code>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 } } </code>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
"""
</code>
<code>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 """ </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
</code>
<code>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 } </code>
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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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") }
</code>
<code>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") } </code>
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…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let url4 = URL(string: "path-to-your-temp-dir-m3u8-file")!
</code>
<code>let url4 = URL(string: "path-to-your-temp-dir-m3u8-file")! </code>
let url4 = URL(string: "path-to-your-temp-dir-m3u8-file")!

Could then be used as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let player = AVPlayer(url: url4)
print("(url4)")
player.play()
</code>
<code>let player = AVPlayer(url: url4) print("(url4)") player.play() </code>
let player = AVPlayer(url: url4)
print("(url4)")
player.play()

Hopefully you’ll now get some playback…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật