uniapp+videojs在app端播放m3u8视频流

uniapp使用videojs在h5上播放m3u8的案例有很多,但是在app上并不能正常播放,本文将给出一个在app上播放的案例。

实现方式就是通过uniapp的webview内嵌一个h5,以下就是h5的代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<!-- <title>Webview Video Player</title> -->
		<script type="text/javascript">
			/**
			 * 判断手机机型
			 */
			function isAndroidOrIOS() {
				var u = navigator.userAgent;
				var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
				var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				if (isAndroid) {
					return "android"
				}
				if (isiOS) {
					return "ios"
				}
				return false
			};

			function videoInit(videoUrl) {
				let video = document.createElement('video');
				video.id = 'video';
				video.style = `width: 100%; height: 100%;`;
				video.controls = true;
				video.setAttribute('playsinline', true) //IOS微信浏览器支持小窗内播放
				video.setAttribute('webkit-playsinline', true) //这个bai属性是ios 10中设置可以让视频在小du窗内播放,也就是不是全zhi屏播放的video标签的一个属性
				video.setAttribute('x5-video-player-type', 'h5') //安卓 声明启用同层H5播放器 可以在video上面加东西
				let source = document.createElement('source');
				source.src = videoUrl;
				source.type = 'application/x-mpegURL';
				video.appendChild(source);
				var container = document.getElementById('video-container');
				container.appendChild(video);
				var player = videojs('video', {
					// poster: 'https://s.yun-live.com/images/20200715/902844b0f1215ff236ad6b1bd1683555.jpg', // 视频封面图地址
					poster: '/static/images/ipvs-cover.webp', // 视频封面图地址
					title: '12334567788999900',
					playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
					autoDisable: true,
					preload: 'auto', //auto - 当页面加载后载入整个视频 meta - 当页面加载后只载入元数据 none - 当页面加载后不载入视频
					language: 'zh-CN',
					fluid: true, // 自适应宽高
					muted: false, //  是否静音
					aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
					controls: true, //是否拥有控制条 【默认true】,如果设为false ,那么只能通过api进行控制了。也就是说界面上不会出现任何控制按钮
					autoplay: true, //如果true,浏览器准备好时开始回放。 autoplay: "muted", // //自动播放属性,muted:静音播放
					loop: true, // 导致视频一结束就重新开始。 视频播放结束后,是否循环播放
					techOrder: ["html5", "flash"], //播放顺序
					screenshot: false,
					autoFullscreen: true,
					controlBar: {
						volumePanel: { //声音样式
							inline: false // 不使用水平方式
						},
						timeDivider: true, // 时间分割线
						durationDisplay: true, // 总时间
						progressControl: true, // 进度条
						remainingTimeDisplay: true, //当前以播放时间
						fullscreenToggle: true, //全屏按钮
						pictureInPictureToggle: true, //画中画

					}
				}, function() {
					this.on('error', function() { //请求数据时遇到错误
						console.log("请求数据时遇到错误")
					});
					this.on('stalled', function() { //网速失速
						console.log("网速失速")
					});
					this.on('play', function() { //开始播放
						console.log("开始播放")
						// uni.showToast('开始播放')
					});
					this.on('pause', function() { //暂停
						console.log("暂停")
					});
					this.on('timeupdate', function() {
						// console.log(this.currentTime())
					})
				});
				if (isAndroidOrIOS() !== 'ios') {
					player.landscapeFullscreen();
				}
			};
			//获取链接后的视频地址参数
			document.addEventListener('DOMContentLoaded', (event) => {
				const currentUrl = window.location.href;
				const urlParams = new URLSearchParams(new URL(currentUrl).search);
				const videoUrl = urlParams.get('url');
				videoInit(videoUrl)
			});
		</script>
	</head>
	<body>
		<link href="https://cdn.bootcss.com/video.js/6.3.3/video-js.min.css" rel="stylesheet">
		<script src="https://cdn.bootcss.com/video.js/6.3.3/video.min.js"></script>
		<script src="./js/videojs-landscape-fullscreen.js"></script>
		<script src="https://cdn.bootcss.com/videojs-contrib-hls/5.11.0/videojs-contrib-hls.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/videojs-flash@2/dist/videojs-flash.min.js"></script>
		<div class="video-js" id="video-container" style="width: 100%;height: 100%;"></div>
	</body>
</html>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇

了解 WS的个人笔记 的更多信息

立即订阅以继续阅读并访问完整档案。

Continue reading