nativescript-videoplayer icon indicating copy to clipboard operation
nativescript-videoplayer copied to clipboard

Video Destroy with Vue

Open packytagliaferro opened this issue 7 years ago • 2 comments

I have been reading the docs and still confused on how to destroy the video. Right now if I go to a detail page and navigate back I can still hear the video playing. Here is what I have that doesnt work (causes crash)

In main.js

Vue.registerElement('VideoPlayer', () => require('nativescript-videoplayer').Video)

In my VideoDetail.vue page:

<template>

    <Page backgroundColor="#242C3B" >
    
        <ActionBar>
            <StackLayout class="actionbar">
                <Label :text="title" class="actionbar-title"/>
                <Label :text="date"  class="actionbar-date"/>
            </StackLayout>
        </ActionBar>

        <StackLayout  v-if="video">

                <VideoPlayer
                    :src="video"
                    autoplay="true"
                    height="300"
                    loop="false"
                    muted="false"
                    ref="lessonVideo"
                    >
                </VideoPlayer>

     </StackLayout>
  
    </Page>

</template>

<script>

    export default {

        data() {
            return {
                video: 'https://myvideolink.mp4'
            }
        }
        
        beforeDestroy(){

            this.$refs.lessonVideo.destroy();

        }
        

    }
</script>



packytagliaferro avatar Dec 12 '18 17:12 packytagliaferro

beforeDestroy(){

            this.$refs.lessonVideo.nativeView.destroy();

        }

javik6 avatar Dec 16 '18 18:12 javik6

I ended up doing something like this to avoid having to remember every time to destroy the native view.

<template>
  <video-player ref="player" v-on="listeners" v-bind="attrs" />
</template>
<script>
// This wraps the nativescript-videoplayer in a component that auto destroys, so you don't have to
// https://github.com/nstudio/nativescript-videoplayer
// using techniques described in https://zendev.com/2018/05/31/transparent-wrapper-components-in-vue.html
export default {
  computed: {
    listeners() {
      const { ...listeners } = this.$listeners;
      return listeners;
    },
    attrs() {
      const { ...attrs } = this.$attrs;
      return attrs;
    }
  },
  beforeDestroy() {
    this.$refs.player.nativeView.destroy();
  }
};
</script>

Then in addition to registering the element, I also add a component on startup to make the usage a bit more seamless.

import Video from "~/components/Video.vue";
Vue.component("Video", Video);

CoreyKaylor avatar May 18 '19 17:05 CoreyKaylor