请用uniapp写一个音乐播放器 使用 slider组件控制audio组件的播放进度(播放)(暂停)(实时显示播放时长)(实时显示进度时长)

<template>
  <view class="container">
    <audio :src="currentMusic.url" :id="audioId" :currentTime="currentTime" @timeupdate="updateCurrentTime"></audio>
   
    <view class="music-list">
      <view
        class="music-item"
        v-for="(music, index) in musicList"
        :key="index"
        :class="{ active: currentMusicIndex === index }"
        @click="playMusic(index)"
      >
        <text :style="{ color: currentMusicIndex === index ? 'blue' : 'black' }">{{ music.name }}</text>
      </view>
    </view>
    <button class="play-btn" @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <text class="current-time">{{ formatTime(currentTime) }}</text>
	<text class="total-time">{{ formatTime(totalTime) }}</text>
	<slider class="slider" :value="currentTime" :max="totalTime" @change="seekTo"></slider>
  </view>
</template>

<script>
export default {
  data() {
    return {
      audioId: 'music',
      musicList: [
        { name: '音乐1', url: 'https://atand.oss-cn-beijing.aliyuncs.com/%E9%9F%B3%E4%B9%90/%E7%A8%8B%E5%93%8D%20-%20%E5%8F%AF%E8%83%BD.mp3'},
        { name: '音乐2', url: 'https://atand.oss-cn-beijing.aliyuncs.com/%E9%9F%B3%E4%B9%90/VIVI%20-%20%E5%BC%80%E5%BF%83%E5%BE%80%E5%89%8D%E9%A3%9E.mp3'},
        { name: '音乐3', url: 'https://atand.oss-cn-beijing.aliyuncs.com/%E9%9F%B3%E4%B9%90/%E4%BC%AF%E7%88%B5Johnny%E3%80%81%E5%94%90%E4%BC%AF%E8%99%8EAnnie%20-%20%E8%A5%BF%E5%8E%A2%E5%AF%BB%E4%BB%96.mp3'},
        { name: '音乐4', url: 'https://atand.oss-cn-beijing.aliyuncs.com/%E9%9F%B3%E4%B9%90/%E5%86%AC%E5%A4%A9%E7%9A%84%E7%A7%98%E5%AF%86.mp3'},
        { name: '音乐5', url: 'https://atand.oss-cn-beijing.aliyuncs.com/%E9%9F%B3%E4%B9%90/%E5%BB%96%E4%BF%8A%E6%B6%9B%20-%20%E8%BF%87%E5%AD%A3%E7%9F%AD%E8%A2%96.mp3'}
      ],
      currentMusicIndex: -1,
      currentMusic: {},
      isPlaying: false,
      currentTime: 0,
	  totalTime:0
    };
  },
  methods: {
    playMusic(index) {
      if (this.currentMusicIndex === index) {
        this.togglePlay();
        return;
      }
      this.currentMusicIndex = index;
      this.currentMusic = this.musicList[index];
      this.audioContext.src = this.currentMusic.url;
      this.audioContext.play();
      this.isPlaying = true;
    },
    togglePlay() {
      if (this.isPlaying) {
        this.audioContext.pause();
      } else {
        this.audioContext.play();
      }
      this.isPlaying = !this.isPlaying;
    },
    updateCurrentTime() {
      this.currentTime = this.audioContext.currentTime;
    },
    seekTo(event) {
      const seekTime = event.mp.detail.value;
      this.audioContext.seek(seekTime);
    },
    formatTime(time) {
      const minutes = Math.floor(time / 60);
      const seconds = Math.floor(time % 60);
      return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
    }
  },
  mounted() {
    this.audioContext = uni.createInnerAudioContext(this.audioId);
    this.audioContext.onTimeUpdate(this.updateCurrentTime);
	this.audioContext.onCanplay(() => {
	  this.totalTime = this.audioContext.duration;
	});
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.slider {
  width: 80%;
  margin-bottom: 20px;
}

.music-list {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: 20px;
}

.music-item {
  margin-bottom: 10px;
  cursor: pointer;
}

.active {
  color: blue;
}

.play-btn {
  padding: 10px 20px;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 4px;
}

.current-time {
  margin-top: 10px;
}
</style>

Uniapp 中,内置组件的样式和行为是可以进行修改的。如果要修改 Slider 组件的样式和行为,可以使用以下方法: 1. 使用 scoped 样式来修改 Slider 组件的样式。在组件所在的页面或组件的样式中,使用 scoped 样式来定义 Slider 组件的样式,例如: ```css <template> <view> <slider class="my-slider" /> </view> </template> <style scoped> .my-slider { background-color: #f00; } </style> ``` 这样,Slider 组件的背景色就会变成红色。需要注意的是,scoped 样式只会对当前组件生效,不会影响其他组件和页面。 2. 使用 props 属性来修改 Slider 组件的属性。Slider 组件多个属性可以进行修改,例如 value、min、max 等。可以在组件使用 props 属性来修改这些属性,例如: ```vue <template> <view> <slider :value="50" :min="0" :max="100" /> </view> </template> ``` 这样,Slider 组件的初始值就会变成 50,最小值为 0,最大值为 100。 3. 使用事件来修改 Slider 组件的行为。Slider 组件多个事件可以进行监听,例如 change、changing 等。可以在组件使用 @change/@changing 等事件来监听 Slider 组件的变化,例如: ```vue <template> <view> <slider :value="50" @changing="handleChange" /> </view> </template> <script> export default { methods: { handleChange(e) { console.log(e.detail.value); }, }, }; </script> ``` 这样,每次 Slider 组件的值发生变化时,就会调用 handleChange 方法并输出当前的值。 通过以上方法,就可以对 Slider 组件进行样式和行为的修改。需要注意的是,如果要对其他内置组件进行修改,也可以使用类似的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王家视频教程图书馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值