viro icon indicating copy to clipboard operation
viro copied to clipboard

Video doesn't stop inside a ViroARSceneNavigator

Open Piquart opened this issue 5 years ago • 6 comments

Environment:

  1. Development OS: Mac
  2. Device OS & Version: iOS version 13.3
  3. Version: react-viro 2.17.0 and React Native version 0.59.9
  4. Device(s): iPhone 8

Description

I developed a scene (ImageARScene) in order to recognise some images and start a remote video. This scene is triggered by another screen including a ViroARSceneNavigator; all is working fine but when I try to tap on close button the screen is dismissed but the audio of the video continues to play. How can I solve?

Main screen with ViroARSceneNavigator

    async dismiss() {
        console.log("dismissing...");
        this.props.navigation.dismiss();
    }

    return (
          <View style={styles.outer}>
            <TouchableOpacity
              style={styles.closeButton}
              onPress={() => this.dismiss()}>
              <Text style={styles.closeText}>Close</Text>
            </TouchableOpacity>
            <ViroARSceneNavigator
              ref={ref => (this.sceneNavigator = ref)}
              style={styles.arView}
              onExitViro={() => this._onExitViro()}
              viroAppProps={this.state.viroAppProps}
              initialScene={{
                scene: ImageARScene
              }}
            />
          </View>
        );

ImageARScene

render() {
    return (
      <ViroARScene>
        <ViroARImageMarker target={"targetImage"}>
          <ViroVideo
            source={{ uri: this.state.trailer}}
            ref={VIDEO_REF}
            paused={this.state.isPaused}
            height={0.3}
            width={0.2}
            rotation={[270, 0, 0]}
          />
        </ViroARImageMarker>
      </ViroARScene>
    );
  }

Piquart avatar Feb 07 '20 23:02 Piquart

It's not the prettiest solution, but maybe set the "muted" prop of ViroVideo to true when it's paused?

https://docs.viromedia.com/docs/virovideo-2-compare

HedwigAR avatar Feb 13 '20 09:02 HedwigAR

@HedwigAR thank you for your suggestion; unfortunately, there isn't a pause button so the video needs to pause when the user close the screen, I tried to use componentWillUnmount and it's correctly called (the log is printed) but the video/audio doesn't stop. Maybe I'm wrong something else? Below my componentWillUnmount method, thanks in advance.

componentWillUnmount() {
    console.log("component will dismiss...");
    this.refs[VIDEO_REF].muted = true;
    this.refs[VIDEO_REF].paused = true;
    this.setState({
      isPaused: true
    });
  }

Piquart avatar Feb 15 '20 10:02 Piquart

workaround:


_onUpdateTime(currentTime, totalTime){
    this.setState({ currentTime, totalTime })
}

componentWillUnmount(){
    this.refs[VIDEO_REF].seekToTime(this.state.totalTime);
}

render() {
    return (
      <ViroARScene>
        <ViroARImageMarker target={"targetImage"}>
          <ViroVideo
            source={{ uri: this.state.trailer}}
            ref={VIDEO_REF}
            paused={this.state.isPaused}
            height={0.3}
            width={0.2}
            rotation={[270, 0, 0]}
            onUpdateTime={this._onUpdateTime}
          />
        </ViroARImageMarker>
      </ViroARScene>
    );
}

inversadigital avatar Mar 13 '20 18:03 inversadigital

@inversadigital using this workaround, now it works!

Thank you very much!

Piquart avatar Apr 01 '20 13:04 Piquart

if you're using ViroMaterialVideo, you can call deleteMaterials to release the material:

 useEffect(() => {
      ViroMaterials.createMaterials({
        video_material: {
          lightingModel: 'Lambert',
          diffuseTexture: { uri: YOUR_VIDEO_URL },
        },
      });
    }
    return () => {
      ViroMaterials.deleteMaterials(['video_material']);
    };
  }, []);

JungHsuan avatar Aug 20 '21 07:08 JungHsuan

You can manipulate the props through setNativeProps In your render function :

<ViroARSceneNavigator
    style={styles.videoItem}
    autofocus={true}
    initialScene={{
        scene: () => (
	    <ViroARScene>
		<ViroVideo
		    ref={videoRef}
		    {...other props}
		/>
	    </ViroARScene>
	),
    }}
/>

To toggle play/pause: videoRef?.current?.setNativeProps({ paused: true }) For those who don't use React-Native useRef to create the videoRef it should be videoRef?.setNativeProps({ paused: true }) See https://viro-community.readme.io/docs/virovideo#setnativepropsnativeprops

SamiChab avatar Mar 17 '22 03:03 SamiChab