Implementation of HTTP Live Streaming in iOS

前端 未结 3 1734
长情又很酷
长情又很酷 2020-12-22 15:18

I want to write a little iOS video client and have to use HTTP Live Streaming. The videos come from a Wowza Media Server which supports HTTP Live Streaming, so the server-si

相关标签:
3条回答
  • 2020-12-22 15:52

    A short and to the point implementation. The included URL points to a valid stream (as of 12/15/2015), but you can just replace with your own URL to a .m3u8 file.

    Objective-C:

    #import <MediaPlayer/MediaPlayer.h>
    @interface ViewController ()
    @property (strong, nonatomic) MPMoviePlayerController *streamPlayer;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSURL *streamURL = [NSURL URLWithString:@"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"];
    
        _streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
    
        // depending on your implementation your view may not have it's bounds set here
        // in that case consider calling the following 4 msgs later
        [self.streamPlayer.view setFrame: self.view.bounds];
    
        self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
    
        [self.view addSubview: self.streamPlayer.view];
    
        [self.streamPlayer play];
    }
    
    - (void)dealloc
    {
         // if non-ARC
        // [_streamPlayer release];
        // [super dealloc];
    }
    
    @end
    

    Swift:

    import UIKit
    import MediaPlayer
    
    class ViewController: UIViewController {
    
         var streamPlayer : MPMoviePlayerController =  MPMoviePlayerController(contentURL: NSURL(string:"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"))
    
         //Let's play
         override func viewDidLoad() {
             super.viewDidLoad()
             // Do any additional setup after loading the view, typically from a nib.
             streamPlayer.view.frame = self.view.bounds
             self.view.addSubview(streamPlayer.view)
    
             streamPlayer.fullscreen = true
             // Play the movie!
             streamPlayer.play()
        }
    
    }
    

    Updated answer for both the languages. Also MPMoviePlayerController is deprecated in iOS 9, but you can use AVPlayerViewController instead. Happy Coding.!!!

    0 讨论(0)
  • 2020-12-22 16:05

    Below is my Swift 4 solution with AVPlayer

    [since MPMoviePlayerController is deprecated in iOS 9]

    import UIKit
    import AVKit
    ...
    
    class VideoPlayerViewController: UIViewController {
    
        var player: AVPlayer?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            guard let url = URL(string: "http://stream-url.com/file.m3u8") else {
                print("Umm, looks like an invalid URL!")
                return
            }
    
            player = AVPlayer(url: url)
            let controller = AVPlayerViewController()
            controller.delegate = self
            controller.player = player
    
            // present the player controller & play
            present(controller, animated: true) {
                self.player?.play()
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-22 16:07

    If you point a UIWebView at that target m3u8 URL, it will just work.

    0 讨论(0)
提交回复
热议问题