HLS Native Player

Previous Post

Last post, I explained Http Live Streaming.
Set up Apache, and how to write HTML.

iOS

For iOS, the simple way is to use AVPlayerViewController.
AVPlayerViewController is supported over iOS8.
UI Component is provided. You can drag and drop AVPlayerViewController.
Screen Shot 2017-09-24 at 12.32.21 PM

ViewController.swift

import UIKit
import AVFoundation
import AVKit

class ViewController: AVPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let url : URL? = URL(string: "http://localhost:80/videos/sample.m3u8")
        self.player = AVPlayer.init(playerItem: AVPlayerItem(url: url!))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

And to use localhost data, we need to change ATS setting.

info.plist

    NSAppTransportSecurity
    
        
        NSAllowsLocalNetworking
        
        
        NSAllowsArbitraryLoads
        
    

Set this as Rootcontroller, you can see video.
Support swift4

Android

For Android, VideoView is easy way to play video.

activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.atmarkplant.hlssampleandroid.MainActivity">

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

Unfortunately, emulator does not seem to support video play.
Instead of test local, I use remote sample.(But high resolution provided by apple does not work well.)

Screen Shot 2017-09-24 at 11.06.02 PM

class MainActivity : AppCompatActivity() {

    lateinit var videoView : VideoView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

       // window.setFormat(PixelFormat.TRANSPARENT)

        videoView = findViewById(R.id.videoview)

        // URL Play
        //videoView.setVideoURI(Uri.parse("http://10.0.2.2/videos/sample.m3u8"))
        videoView.setVideoURI(Uri.parse("https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8")) // Fine
        // videoView.setVideoPath("http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8")  // Work fine
        // videoView.setVideoURI((Uri.parse("http://techslides.com/demos/sample-videos/small.mp4")))  // Fine
        //videoView.setVideoURI(Uri.parse("https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8")); // Not working
        videoView.setMediaController(MediaController(this))
        //videoView.start()
        videoview.requestFocus()
        videoview.setOnPreparedListener(MediaPlayer.OnPreparedListener {

        })
    }
}