断断续续花了一周的业余时间,完成了4个iOS端 ffmpeg demo的实现
- 做个简单的入口
image
- 接入ffmpeg编译好的库和头文件,打印configuration
image
- 推流的实现 推流的实现,需要先搭建一个nginx rtmp服务器,也不复杂,网上有很多详细的教程 推流后,本地可以用ffplay来查看推流的视频,也可以写个简单的h5页面查看 推流最末尾会报错,错误如下,google/baidu 找遍了所有的帖子,没找到原因 // av_write_trailer(ofmt_ctx); 只能确定是这句代码有问题,应该是写流结尾出bug了, 可能是采用了老接口的问题 Error muxing packet [flv @ 0x12102c000] Failed to update header with correct duration. [flv @ 0x12102c000] Failed to update header with correct filesize. Error occurred.
也可以直接用ffmpeg命令推流
代码语言:javascript复制ffmpeg -re -i "/home/users/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://xxxxx:1935/hls/test2
代码语言:javascript复制// 直接用ffplay接受流媒体数据
// ffplay -i rtmp://180.76.164.113:1935/hls/test2
// h5实现代码, 引用了hls库:
<!DOCTYPE html>
<html>
<head>
<title>rtmp</title>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
</head>
<body>
非的发达
<video
autoplay="true"
muted="muted"
id="video"></video>
<script>
if (Hls.isSupported()) {
var video = document.getElementById('video');
var hls = new Hls();
hls.loadSource('http://xxxxx/hls/test2.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function(){
video.play();
})
}
</script>
</body>
</html>
- iOS原生播放器的实现
image.png iOS对音视频的支持非常好,写个播放的demo,总共不到80行代码
代码语言:javascript复制#import "PlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface PlayViewController ()
@property MPMoviePlayerController *moviePlayer;
@end
@implementation PlayViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.moviePlayer play];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(MPMoviePlayerController *)moviePlayer{
if(!_moviePlayer){
NSString *urlStr = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"resource.bundle/war3end.mp4"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
-(void)addNotification{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackfinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
}
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放...");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停播放");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放");
break;
default:
break;
}
}
-(void)mediaPlayerPlaybackfinished:(NSNotification *)notification{
NSLog(@"播放完成.%li", self.moviePlayer.playbackState);
}
@end