断断续续花了一周的业余时间,完成了4个iOS端 ffmpeg demo的实现
image
image
也可以直接用ffmpeg命令推流
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
// 直接用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>
image.png iOS对音视频的支持非常好,写个播放的demo,总共不到80行代码
#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