iOS流媒体技术——实时流媒体开发

苹果的HLS技术是最为先进的,有技术规范还有一套流媒体开发和使用的整体解决方案。

采集设备采集数据、传递给server对音频或视频进行编码、通过媒体文件分割工具进行分割、将分隔好的文件和索引放到服务器上、客户端可以进行请求。

下面介绍mediafilesegment如何分割文件。

媒体文件的分割与处理

在苹果开发者网站,下载HTTP Live Streaming Tools.dmg软件,可以在命令行进行分割视频:

mediafilesegment -f hls/ YY.mp4

搭建HLS流媒体服务器

能够提供HTTP服务的服务器就可以。

常用的免费的有Apache和Tomcat等,在Windows平台还可以使用IIS服务器。

使用本地技术开发客户端

还是使用MediaPlayer框架来播放,只需要修改获取URL的地方:

-(NSURL *)p_movieURL
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/stream/prog_index.m3u8"];
    return url;
}

使用Hybird技术开发客户端

Hybird是本地技术 + Web技术的混合。

具体做法是在界面中放置一个webview,加载HTML,从而实现显示作用。

<video>标签实现视频的播放,<audio>实现音频播放。

下面是一个实际的例子:

#import "ViewController.h"
#import <WebKit/WebKit.h>
 
@interface ViewController ()
 
@property (weak, nonatomic) IBOutlet WKWebView *webView;

@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _webView.scrollView.scrollEnabled = NO;
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"html"];
    NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    
    NSError *error = nil;
    NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
    if (error == nil)
    {
        [_webView loadHTMLString:html baseURL:bundleURL];
    }
}
 
@end
<html>
    <head>
        <title>HTTP Live Streaming Example</title>
    </head>
    <body>
        <video src = "http://localhost/stream/hls/prog_index.m3u8"
            width = "300" height = "300" controls = "controls">
        </video>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/run_in_road/article/details/113611386