在iOS中实现HTTP Live Streaming [英] Implementation of HTTP Live Streaming in iOS

查看:130
本文介绍了在iOS中实现HTTP Live Streaming的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个小小的iOS视频客户端,并且必须使用HTTP Live Streaming。视频来自支持HTTP直播流的Wowza媒体服务器,因此服务器端实现不是我的问题。
我已经观看了WWDC视频并阅读了关于HTTP Live Streaming的Apple文档。

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-side implementation is not my problem. I have already watch the WWDC videos and read the Apple documentation about HTTP Live Streaming.

但无处可解释如何在iOS设备上播放视频。在WWDC-talk中提到有3个拥有来显示视频:

But there is nowhere explained how to play back the videos on an iOS device. In a WWDC-talk was mentioned that there are 3 possebilties to display the videos:


  • UIWebView

  • MPMoviePlayerController

  • AVPlayerItem

哪一个最好?

我怎样才能从HTML-Page这样的HTML页面中读出视频URL,由服务器提供?

And how can I read out the video-URL's from a HTML-Page like these, which become provided by the server?

<html>
<head>
    <title>HTTP Live Streaming Example</title>
</head>
<body>
    <video
        src="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
        height="300" width="400"
    >
    </video>
</body>
</html>

(来源: Apple HTTP直播流概述

我真的不知道从哪里开始编码...也许有人知道比恼人的缝合流播放器更好的示例代码,或者可以编写一个小教程... p>

I really don't know where to start with coding...Maybe somebody know better example code than the annoying "Stitched Stream Player" or can write a little tutorial...

推荐答案

一个简短的点对点实现。包含的网址指向一个有效的流(截至2015年12月15日),但您可以将自己的网址替换为.m3u8文件。

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:

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()
    }

}

更新了两种语言的答案。另外,在iOS 9中不推荐使用 MPMoviePlayerController ,但可以使用 AVPlayerViewController 。快乐编码。!!!

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

这篇关于在iOS中实现HTTP Live Streaming的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆