vue不同页面切换,背景音乐连续播放不间断

最近遇到了这样一个需求,就是h5不同页面之间跳转,要求bgm连续播放不间断。 不知道大家是怎么处理的呢?
我之前想的是,在每个页面都写一个audio标签,然后切换页面的时候存一下当前音乐的播放进度,到下一个页面再给音乐写入开始时间。

<template>
	    <audio loop="loop" preload="preload" id="bgmusic" controls="controls" ref="MusicPlay" style="display: none;">
      		<source src="xxx.mp3" type="audio/mpeg">
    	</audio>
    	<span @click="goIndex"></span>
</template>

<script>
mounted(){
     this.soundBgm = document.getElementById("bgmusic");
      //判断本地是否有存储过音频播放时间
      if(sessionStorage.bgmstart == 'true'){
          this.current = 'musicplay';
      if (sessionStorage.bgmTime == null) {
        //若没有时,从头自动播放
        this.soundBgm.currentTime = 0;
        this.soundBgm.play();
      } else {
        //若有存储的则,取出本地存储的音频播放的暂停时间
        var curTime = window.sessionStorage.getItem("bgmTime");
        sessionStorage.setItem('bgmPlay', this.soundBgm.bgmPlay);
        //从暂停时间开始接着播放
        this.soundBgm.currentTime += curTime;
        this.soundBgm.play();
      }
      }
  },
  methods:{
        //页面跳转时将本页面音频最后截至时间存储下来
      fun() {
        this.soundBgm.pause();
        sessionStorage.setItem('bgmPlay', this.soundBgm.bgmPlay);
        sessionStorage.setItem('bgmTime', this.soundBgm.bgmPlay ? this.soundBgm.currentTime + this.soundBgm.context.currentTime - this.soundBgm.startTime : this.soundBgm.currentTime);
      },
      // 跳转
         goIndex() {
            this.fun();
	        this.$router.push({
	            path: `/xxx`,
	          });
            }
  }
  </script>

但是这样第一是不同页面切换的时候,音乐有很明显的卡顿。第二就是iOS设置currentTime没反应,不知道为什么一直是0。有木有大佬可以讲一下这样为什么iOS不能适配啊(其实大家也能看出来 我这里写的非常混乱,原生夹杂vue…真的是很晕了)

后来请教了一个大佬指点了一下 ,正确的操作应该是 在app.vue里设置全局audio

<template>
  <div id="app">
    <router-view />
    <audio
      loop="loop"
      preload="preload"
      id="bgmusic"
      controls="controls"
      ref="MusicPlay"
      style="display: none;"
    >
      <source
        src="xxx.mp3"
        type="audio/mpeg"
      />
    </audio>
  </div>
</template>

其他页面直接用this.$parent去调用然后播放

this.$parent.$refs.MusicPlay.play();
this.$parent.$refs.MusicPlay.pause();

按照逻辑功能的不同,在合适的位置写入即可

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Vue项目中,可以通过使用Vue Router库来实现主页面切换不同页面的功能。Vue Router是Vue.js官方的路由管理器,可以将不同页面组件映射到不同的URL地址,从而实现页面间的切换。 首先需要在项目中安装Vue Router: ``` npm install vue-router --save ``` 然后在main.js中引入Vue Router并配置路由: ``` import Vue from 'vue' import VueRouter from 'vue-router' import Home from './components/Home.vue' import About from './components/About.vue' import Contact from './components/Contact.vue' Vue.use(VueRouter) const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] const router = new VueRouter({ routes }) new Vue({ router, template: ` <div id="app"> <h1>My App</h1> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-link to="/contact">Contact</router-link> <router-view></router-view> </div> ` }).$mount('#app') ``` 在上面的代码中,我们定义了三个路由:`/`、`/about`和`/contact`,分别映射到三个组件:Home、About和Contact。在Vue实例的模板中,我们使用`<router-link>`标签来实现页面之间的切换,使用`<router-view>`标签来显示当前路由的组件。 接下来,我们需要在组件中使用路由: ``` <template> <div> <h2>About</h2> <p>This is the about page.</p> </div> </template> <script> export default { name: 'About' } </script> ``` 在组件中,可以通过`this.$router.push()`方法来实现编程式导航: ``` this.$router.push('/contact') ``` 这样就可以实现Vue页面切换不同页面的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值