[英]Implementation of HTTP Live Streaming in iOS
我想寫一個小的iOS視頻客戶端,並且必須使用HTTP Live Streaming。 視頻來自支持HTTP直播流的Wowza媒體服務器,因此服務器端實現不是我的問題。 我已經觀看了WWDC視頻,並閱讀了有關HTTP直播的Apple文檔。
但是沒有人解釋如何在iOS設備上播放視頻。 在WWDC-talk中提到有三種可能性來顯示視頻:
哪一個是最好的?
我如何從像這樣的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>
(來源: Apple HTTP直播流媒體概述 )
我真的不知道從哪里開始編碼...也許有人比煩人的“Stitched Stream Player”知道更好的示例代碼,或者可以寫一點教程。
簡短的實施。 包含的URL指向有效的流(截至2015年12月15日),但您可以使用自己的URL替換為.m3u8文件。
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
迅速:
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
。 快樂的編碼。!!!
如果您將UIWebView指向該目標m3u8 URL,它將正常工作。
下面是我使用AVPlayer
Swift 4解決方案
[因為iOS 9中不推薦使用MPMoviePlayerController
]
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()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.