为什么我的 expo-av 视频不会自动播放?

huangapple go评论55阅读模式
英文:

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?

      &lt;Video
        style={{width: 500, height: 300, resizeMode: &#39;contain&#39;}}
        source={{
          uri: &#39;https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4&#39;,
        }}
        resizeMode={ResizeMode.CONTAIN}
        shouldPlay={true}
        videoStyle={{width: 500, height:300}}
      /&gt;

答案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 &#39;react&#39;;
import { View, StyleSheet, Button } from &#39;react-native&#39;;
import { Video, ResizeMode } from &#39;expo-av&#39;;
import {useEffect, useState} from &quot;react&quot;;
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:&quot;center&quot;,
justifyContent: &quot;center&quot;,
height: &quot;100%&quot;,
width:&quot;100%&quot;
},
video: {
height: &quot;380px&quot;,
width: &quot;400px&quot;
},
buttons: {
display:&quot;flex&quot;,
width: &quot;500px&quot;,
flexDirection: &quot;row&quot;,
padding:0,
margin:0,
justifyContent: &quot;flex-start&quot;,
marginLeft:&quot;200px&quot;
},
spacing: {
marginRight: &quot;16px&quot;,
}
});
useEffect(()=&gt; {
},[])
return (
&lt;View style={styles.container}&gt;
&lt;Video
ref={video}
style={styles.video}
source={{
uri: &#39;https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4&#39;,
}}
autoPlay
useNativeControls
resizeMode={ResizeMode.CONTAIN}
shouldPlay={true}
isMuted={isMuted}
onPlaybackStatusUpdate={status =&gt; setStatus(() =&gt; status)}
/&gt;
&lt;View style={styles.buttons}&gt;
&lt;Button
style={styles.button}
ref={playerButton}
title={status.isPlaying ? &#39;Pause&#39; : &#39;Play&#39;}
onPress={() =&gt;
status.isPlaying ? video.current.pauseAsync() : video.current.playAsync()
}
/&gt;
&lt;div style={styles.spacing}/&gt;
&lt;Button
style={styles.button}
ref={playerButton}
title={isMuted ? &#39;Unmute&#39; : &#39;Mute&#39;}
onPress={() =&gt; setIsMuted(!isMuted)}
/&gt;
&lt;/View&gt;
&lt;/View&gt;
);
}

huangapple
  • 本文由 发表于 2023年6月19日 11:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定