Skip to content

LivePhoto 实况照片

基于 video 和 image 实现的实况照片组件,支持长按播放视频、自动播放、自定义样式、事件监听等功能。用户可以通过长按图片来播放对应的视频内容,模拟 iOS Live Photo 的效果。

基本用法

基础用法需要提供视频源和静态图片源。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
/>

自定义圆角

通过 radius 属性设置自定义圆角大小。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  :radius="20"
/>

自动播放

通过 autoplay 属性启用自动播放模式,视频会自动开始播放,播放结束后仍可手动交互。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
  :autoplay="true"
/>

隐藏指示器

通过 show-indicator 属性控制指示器的显示。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
  :show-indicator="false"
/>

展示模式

通过 display-only 属性启用展示模式,只显示图片和指示器,不支持视频交互。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
  :display-only="true"
/>

自定义指示器位置

通过 indicator-leftindicator-top 属性自定义指示器的位置。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
  indicator-left="50"
  indicator-top="30"
/>

自定义图片插槽

使用 image 插槽自定义图片内容,可以添加覆盖层、特效等自定义元素。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
>
  <template #image="{ src }">
    <view class="custom-image-container">
      <image :src="src" mode="aspectFill" class="custom-image" />
      <view class="custom-overlay">
        <text>自定义覆盖内容</text>
      </view>
    </view>
  </template>
</up-live-photo>

事件监听

支持监听长按开始/结束、视频播放/暂停等事件。

vue
<up-live-photo
  video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
  src="https://picsum.photos/400/300?random=1"
  width="300"
  height="200"
  radius="12"
  @press-start="onPressStart"
  @press-end="onPressEnd"
  @video-play="onVideoPlay"
  @video-pause="onVideoPause"
  @video-loaded="onVideoLoaded"
  @video-progress="onVideoProgress"
/>

组件方法

可以通过组件实例调用相关方法。

vue
<template>
  <up-live-photo
    ref="livePhotoRef"
    video-src="https://sample-videos.com/zip/10/mp4/SampleVideo_360x240_1mb.mp4"
    src="https://picsum.photos/400/300?random=1"
    width="300"
    height="200"
    radius="12"
  />
  <button @click="stopVideo">停止播放</button>
  <button @click="checkIsPlaying">检查播放状态</button>
  <button @click="resetComponent">重置组件</button>
</template>

<script setup>
import { ref } from 'vue'

const livePhotoRef = ref()

function stopVideo() {
  livePhotoRef.value.stopVideo()
}

function checkIsPlaying() {
  const isPlaying = livePhotoRef.value.isPlaying()
  console.log('播放状态:', isPlaying)
}

function resetComponent() {
  livePhotoRef.value.reset()
}
</script>

Props

参数说明类型默认值可选值
video-src视频源地址string--
src静态图片源地址string--
width组件宽度number | string'100%'-
height组件高度number | string'100%'-
radius圆角大小number | string0-
custom-style自定义样式string''-
custom-class自定义类名string''-
mode图片填充模式string'scaleToFill'-
show-indicator是否显示Live Photo指示器booleantrue-
autoplay是否自动播放booleanfalse-
enable-vibration是否启用振动反馈booleantrue-
muted是否静音播放booleantrue-
display-only是否仅展示模式booleanfalse-
indicator-left指示器距左边距离string | number'20rpx'-
indicator-top指示器距顶部距离string | number'20rpx'-
lazy-load是否懒加载图片booleantrue-
enable-preview是否启用图片预览booleanfalse-
preview-src预览图片地址string''-
placeholder-src占位图片地址string''-
filter图片滤镜number | string''-
delay加载延迟时间number0-
min-height最小高度number | string'200rpx'-
round是否为圆形booleanfalse-

Slots

插槽名说明参数
image自定义图片内容
loading自定义加载状态-
error自定义错误状态-

Events

事件名说明回调参数
video-play视频开始播放-
video-pause视频暂停播放-
press-start开始长按-
press-end结束长按-
video-ended视频播放结束-
video-loaded视频加载完成-
video-progress视频加载进度progress: number
load图片加载成功event
error图片加载失败event
click图片点击事件event

Methods

方法名说明参数返回值
stopVideo停止视频播放--
isPlaying是否正在播放-boolean
reset重置组件状态--

注意事项

  1. 组件需要同时提供 video-srcimage-src 属性
  2. 视频建议使用较短的循环视频,以获得更好的 Live Photo 效果
  3. 在小程序平台,视频播放可能受到平台限制
  4. 建议为视频和图片提供相同的宽高比,以保证显示效果
  5. 组件支持振动反馈,但在某些平台可能不可用

源代码

文档
组件

Released under the MIT License.

Released under the MIT License.