Html iOS中HTTP直播流的实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8291691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementation of HTTP Live Streaming in iOS
提问by Lindemann
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 视频客户端,并且必须使用 HTTP Live Streaming。视频来自支持 HTTP Live Streaming 的 Wowza Media Server,因此服务器端实现不是我的问题。我已经观看了 WWDC 视频并阅读了有关 HTTP Live Streaming 的 Apple 文档。
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 possibilities to display the videos:
但是没有任何地方解释如何在 iOS 设备上播放视频。在 WWDC 演讲中提到有 3 种可能性来显示视频:
- UIWebView
- MPMoviePlayerController
- AVPlayerItem
- 网页视图
- MP电影播放器控制器
- AVPlayerItem
Which one is the best?
哪一个是最好的?
And how can I read out the video-URL's from a HTML-Page like these, which become provided by the server?
以及如何从服务器提供的 HTML 页面中读出视频 URL?
<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>
(Source: Apple HTTP Live Streaming Overview)
(来源:Apple HTTP Live Streaming 概览)
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.
我真的不知道从哪里开始编码......也许有人知道比烦人的“缝合流播放器”更好的示例代码,或者可以编写一个小教程。
回答by GnarlyDog
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.
一个简短而中肯的实现。包含的 URL 指向一个有效的流(截至 2015 年 12 月 15 日),但您可以将自己的 URL 替换为 .m3u8 文件。
Objective-C:
目标-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.!!!
更新了两种语言的答案。MPMoviePlayerController
在 iOS 9 中也已弃用,但您可以AVPlayerViewController
改用。快乐编码.!!!
回答by jab
If you point a UIWebView at that target m3u8 URL, it will just work.
如果您将 UIWebView 指向该目标 m3u8 URL,它将正常工作。
回答by Rao
Below is my Swift 4solution with AVPlayer
下面是我的Swift 4解决方案AVPlayer
[since MPMoviePlayerController
is deprecated in iOS 9]
[因为MPMoviePlayerController
在 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()
}
}
}