VLC player with live streams for iOS developers
A lot of iOS developers use Apple’s AVPlayer for their applications. Sometimes AVPlayer doesn’t give you information what you want and you need to use something else. VLC player is a good choice, and in this article I’ll try to describe how it can be used.
First you must install their library, for example I will use cocoapods, how to use cocoapods you can find in this article.
For SPM I have create package, you can install it by link https://gitlab.com/Kyrylenko/vlc_spm
How to install SPM you can read here
ADDING VLC PLAYER TO YOUR APPLICATION
Open terminal and paste these commands
$ pod init
$ open Podfile
In opened file and after “use_frameworks!” paste “pod ‘MobileVLCKit’ ” your file must look like this
After these steps paste command $ pod install in terminal
Open YourProjectName.xcworkspace and disable bitcode:
You can disable bitcode by going to your target’s Build Settings -> Set Enable Bitcode to “No”
After that select Product->Scheme->Edit Scheme and disable GPU Frame Capture
HOW TO USE VLC PLAYER INSIDE APPLICATION
Import and start VLC player
For using VLC player you must import lib to your controller import MobileVLCKit
After importing MobileVLCKit you must initialise and configure VLC Player
Create instance of VLC player like it shown at 14'th line on the picture above. After this you should set drawable property for VLC player (line 25, you can use any view). It says to VLC where the video must be rendered. At last, you must create VLCMedia object with url or path to local video and start video playback (lines 26, 27).
Configuring options VLC player
For configuration player’s media you can use those methods:
- addOption(_ option: String)
- addOptions(_ options: [AnyHashable:Any])
For example, you can enable verbose logs from VLC and configure size of the video buffer in miliseconds:
- videoPlayer.media.addOption( “-vv”)
- videoPlayer.media.addOption( “ — network-caching=10000”) (max 60000 milliseconds)
If you would like more information about configure options you can use this link.
Add controls to your player
Add UISlider to your UIViewContrller with range (0..200) and add handler for this slider’s value changed event:
Add delegate to your player
To add delegate you must implement VLCMediaPlayerDelegate protocol
In this example above I have implemented only one method from the protocol.
The most part of the methods from VLCMediaPlayerDelegate are using Notification object inside them. You can get a player object with construction like this:
guard let videoPlayer = aNotification.object as? VLCMediaPlayer else {return}
You can get sample by this link