How to video stream of IPTV with m3u8 parser in Swift.

Abdullah Yalcin
Atonomik
Published in
2 min readJul 6, 2022

We all know building an iOS application requires some UI and Swift knowledge. While creating many standard features, UI components are placed, then an interaction and business layer is created in accordance with the design pattern used. But there are some features that the algorithms should be used to play a key role. IPTV feature is one of them.

M3U File

m3u is a computer file format for a multimedia playlist. m3u8 includes album artist, track information, display title, etc…

First of all, we need to grab, then map the m3u file into an array of objects.

Parser Logic

Here is a struct MediaItem, it includes streamUrl, title, and duration.

  • parseM3U function gets an m3u file content, which is presented in String format.
  • #EXTINF:“ word defines a playlist file. It looks up line by line, then maps into MediaItem.

ViewController to stream

  • In this example, we grabbed an m3u file from a remote CDN provider. You can store it in a string or internal storage of your application.
  • Build a listTableView to present media.
  • AVKit can get a stream URL directly, which is built-in.
  • AVPlayerViewControllerDelegate is used for Picture-in-Picture option.

In order to activate Picture-in-Picture in your app

  • Enable “Audio, Airplay, and Picture in Picture” mode in your app target.
  • In your appDelegate -> didFinishLaunchingWithOptions function, set category and activate like below.

do {

try AVAudioSession.sharedInstance().setCategory(.playback)

try AVAudioSession.sharedInstance().setActive(true)

}catch {

print(error)

}

I hope this topic saves your time when building a stream application. For questions, you can reach me on LinkedIn, GitHub, or wherever you find me:)

--

--