2011-11-28 40 views
51

我想寫一個小小的iOS視頻客戶端,並且必須使用HTTP Live Streaming。這些視頻來自支持HTTP Live Streaming的Wowza媒體服務器,所以服務器端的實現不是我的問題。 我已經觀看了WWDC視頻並閱讀了關於HTTP Live Streaming的Apple文檔。在iOS中實現HTTP Live Streaming

但是沒有任何地方解釋瞭如何在iOS設備上播放視頻。在WWDC通話中提到,有3個possebilties顯示視頻:

  • UIWebView的
  • 的MPMoviePlayerController
  • AVPlayerItem

哪一個是最好的?

我怎樣才能從這樣的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 Live Streaming Overview

我真的不知道從哪裏開始編碼......也許有人知道的比惱人的「縫合碼流播放器」更好的例子代碼或者可以寫一個小教程...

回答

54

一個簡短的點到點實現。包含的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() 
    } 

} 

兩種語言更新了答案。另外MPMoviePlayerController已在iOS 9中棄用,但您可以改用AVPlayerViewController。快樂編碼。

+0

H克里斯。感謝這個簡單的例子。它效果很好。我想知道你如何從視頻創建.m3u8文件? – Isuru

+3

我的網站已停機一段時間,但您仍然可以訪問我在此處撰寫的帖子:http://www.christopherlavender.com/tag/http-live-streaming/。此外,還有一些較舊的教程仍然不錯。關鍵是,如果您要「扮演您自己的」HLS視頻,您需要Apple Developer網站上的命令行分段工具。他們工作得很好,但文檔是最好的。 – GnarlyDog

+0

感謝您的教程。我會馬上看看它。 :) – Isuru

5

如果您將UIWebView指向目標m3u8 URL,它將會正常工作。

+7

大聲笑這是我最喜歡的詞組。我愛當事情「只是工作」 – uofc

相關問題