英文:
Why will my expo-av Video not start playing automatically?
问题
我有一个正在使用 expo-av 渲染的视频。视频可以正常渲染,但在首次渲染时不会自动播放。我已将 shouldPlay
属性设置为 true。我提供了一个 示例链接,以及以下一些代码。如果您使用示例链接,请将模拟器设置为 web。
为什么视频不会自动开始播放?
<Video
style={{ width: 500, height: 300, resizeMode: 'contain' }}
source={{
uri: 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
}}
resizeMode={ResizeMode.CONTAIN}
shouldPlay={true}
videoStyle={{ width: 500, height: 300 }}
/>
英文:
I have a Video that I am rendering with expo-av. The video renders fine, however it will not start playing upon first render. I have the shouldPlay
prop set to true. I have provided a snack example here as well as some code below. If you use the snack example, please set the simulator to web.
Why does the video not start playing automatically?
<Video
style={{width: 500, height: 300, resizeMode: 'contain'}}
source={{
uri: 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
}}
resizeMode={ResizeMode.CONTAIN}
shouldPlay={true}
videoStyle={{width: 500, height:300}}
/>
答案1
得分: 2
英文:
The snack is working just fine.
I do see only the first frame when the autoplay permission of the browser (chrome or Firefox) blocks audio.
But if both audio and video are allowed, then the video starts playing immediately.
答案2
得分: 2
补充正确原因的VonC所给的答案之外,我们还有Chrome中的自动播放策略和Chromium自动播放。
AutoPlay策略可能因浏览器而异,但在大多数情况下至少需要用户交互。此外,如果音频静音,则自动播放的可能性很高。该文章解释了我们今天看到的大多数视频流网站为什么是静音的原因。视频被静音,如果浏览器支持它,将自动播放,如果不支持,则如果用户取消静音,它将自动播放。
简而言之,我们必须保持音频静音,并在屏幕上放置一个静音/取消静音按钮。这个演示的样式很差,但它至少给了一个进一步操作的想法。
代码如下,并且还有snack示例。
// App.js
import * as React from 'react';
import { View, StyleSheet, Button } from 'react-native';
import { Video, ResizeMode } from 'expo-av';
import {useEffect, useState} from "react";
export default function App() {
const video = React.useRef(null);
const playerButton = React.useRef(null);
const [isMuted, setIsMuted] = useState(true);
const [status, setStatus] = React.useState({});
const styles = StyleSheet.create({
container: {
alignItems:"center",
justifyContent: "center",
height: "100%",
width:"100%"
},
video: {
height: "380px",
width: "400px"
},
buttons: {
display:"flex",
width: "500px",
flexDirection: "row",
padding:0,
margin:0,
justifyContent: "flex-start",
marginLeft:"200px"
},
spacing: {
marginRight: "16px",
}
});
useEffect(() => {
},[])
return (
<View style={styles.container}>
<Video
ref={video}
style={styles.video}
source={{
uri: 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
}}
autoPlay
useNativeControls
resizeMode={ResizeMode.CONTAIN}
shouldPlay={true}
isMuted={isMuted}
onPlaybackStatusUpdate={status => setStatus(() => status)}
/>
<View style={styles.buttons}>
<Button
style={styles.button}
ref={playerButton}
title={status.isPlaying ? 'Pause' : 'Play'}
onPress={() =>
status.isPlaying ? video.current.pauseAsync() : video.current.playAsync()
}
/>
<div style={styles.spacing}/>
<Button
style={styles.button}
ref={playerButton}
title={isMuted ? 'Unmute' : 'Mute'}
onPress={() => setIsMuted(!isMuted)}
/>
</View>
</View>
);
}
英文:
Adding to the correct reason given by VonC, we have Autoplay policy in Chrome and Chromium Autoplay.
AutoPlay Policy may vary from browser to browser, but in most cases it atleast requires a user interaction. Also if audio is muted, then there's a high probability of auto play. The article explains the reason why most of the video streaming websites we see today are muted. The video is muted,if browser supports it will be auto played, if it doesn't, then if the user unmutes it, then it automatically plays.<br>
In short, we have to keep the audio muted and place a mute/unmute button on the screen.<br>
The styling is poor in this demo, but it atleast give an idea on how to proceed further.<br>
The code is provided below and there's also snack example.
// App.js
import * as React from 'react';
import { View, StyleSheet, Button } from 'react-native';
import { Video, ResizeMode } from 'expo-av';
import {useEffect, useState} from "react";
export default function App() {
const video = React.useRef(null);
const playerButton = React.useRef(null);
const [isMuted, setIsMuted] = useState(true);
const [status, setStatus] = React.useState({});
const styles = StyleSheet.create({
container: {
alignItems:"center",
justifyContent: "center",
height: "100%",
width:"100%"
},
video: {
height: "380px",
width: "400px"
},
buttons: {
display:"flex",
width: "500px",
flexDirection: "row",
padding:0,
margin:0,
justifyContent: "flex-start",
marginLeft:"200px"
},
spacing: {
marginRight: "16px",
}
});
useEffect(()=> {
},[])
return (
<View style={styles.container}>
<Video
ref={video}
style={styles.video}
source={{
uri: 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
}}
autoPlay
useNativeControls
resizeMode={ResizeMode.CONTAIN}
shouldPlay={true}
isMuted={isMuted}
onPlaybackStatusUpdate={status => setStatus(() => status)}
/>
<View style={styles.buttons}>
<Button
style={styles.button}
ref={playerButton}
title={status.isPlaying ? 'Pause' : 'Play'}
onPress={() =>
status.isPlaying ? video.current.pauseAsync() : video.current.playAsync()
}
/>
<div style={styles.spacing}/>
<Button
style={styles.button}
ref={playerButton}
title={isMuted ? 'Unmute' : 'Mute'}
onPress={() => setIsMuted(!isMuted)}
/>
</View>
</View>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论