InnerAudioContext를 기반으로 기본 오디오 구성 요소를 캡슐화하는 uni-app(vue)에 대한 자세한 설명
관련 학습 권장 사항: WeChat 애플릿 개발
이유
같은 이유는 애플릿 공식이 audio
구성 요소audio
组件了
音频组件的要求与限制
1、点击播放或者暂停
2、显示播放进度及总时长
3、通过图标变化显示当前音频所处状态(暂停/播放/加载中)
4、页面音频更新时刷新组件状态
5、全局有且只有一个音频处于播放状态
6、离开页面之后要自动停止播放并销毁音频实例
材料/属性/方法
让我们开始吧
uni-app Vue
- 同样的先构造
DOM
结构
<view class="custom-audio"> <image v-if="audioSrc !== undefined && audioSrc !== null && audioSrc !== ''" @click="playOrStopAudio" :src="audioImg" class="audio-btn" /> <text v-else @click="tips" class="audio-btn">无音源</text> <text>{{ fmtSecond(currentTime) }}/{{ fmtSecond(duration) }}</text></view>复制代码
- 定义接受的组件
props: { audioSrc: { type: String, default: '' }, },复制代码
- 定义
CustomAudio
组件的初始化相关的操作,并给innerAudioContext
的回调添加一些行为(之前Taro那篇我们踩过的坑这里就直接上代码了)
import { formatSecondToHHmmss, afterAudioPlay, beforeAudioRecordOrPlay } from '../../lib/Utils'const iconPaused = '../../static/images/icon_paused.png'const iconPlaying = '../../static/images/icon_playing.png'const iconStop = '../../static/images/icon_stop.png'const iconLoading = '../../static/images/icon_loading.gif'// ...data() { return { audioCtx: null, // 音频上下文 duration: 0, // 音频总时长 currentTime: 0, // 音频当前播放的时长 audioImg: iconLoading, // 默认状态为加载中 } },watch: { audioSrc: { handler(newSrc, oldSrc) { console.log('watch', newSrc, oldSrc) this.audioImg = iconLoading this.currentTime = 0 this.duration = 0 if (this.audioCtx === undefined) { this.audioCtx = uni.createInnerAudioContext() this.onTimeUpdate = this.audioCtx.onTimeUpdate this.bindAuidoCallback(this.audioCtx) } else { this.audioCtx.src = newSrc } if (this.audioCtx.play) { this.audioCtx.stop() getApp().globalData.audioPlaying = false } } } }, mounted() { this.audioCtx = uni.createInnerAudioContext() this.audioCtx.src = this.audioSrc this.audioCtx.startTime = 0 this.bindAuidoCallback(this.audioCtx) },methods: { bindAuidoCallback(ctx) { ctx.onTimeUpdate((e) => { this.onTimeUpdate(e) }) ctx.onCanplay((e) => { this.onCanplay(e) }) ctx.onWaiting((e) => { this.onWaiting(e) }) ctx.onPlay((e) => { this.onPlay(e) }) ctx.onPause((e) => { this.onPause(e) }) ctx.onEnded((e) => { this.onEnded(e) }) ctx.onError((e) => { this.onError(e) }) }, tips(){ uni.showToast({ title: '无效音源,请先录音', icon: 'none' }) }, playOrStopAudio() { if (this.audioCtx === null) { this.audioCtx = uni.createInnerAudioContext() this.audioCtx.src = this.audioSrc this.bindAuidoCallback(this.audioCtx) } if (this.audioCtx.paused) { if (beforeAudioRecordOrPlay('play')) { this.audioCtx.play() this.audioImg = iconPlaying } } else { this.audioCtx.pause() afterAudioPlay() this.audioImg = iconPaused } }, onTimeUpdate(e) { console.log('onTimeUpdate', this.audioCtx.duration, this.audioCtx.currentTime) if (this.audioCtx.currentTime > 0 && this.audioCtx.currentTime <= 1) { this.currentTime = 1 } else if (this.currentTime !== Math.floor(this.audioCtx.currentTime)) { this.currentTime = Math.floor(this.audioCtx.currentTime) } const duration = Math.floor(this.audioCtx.duration) if (this.duration !== duration) { this.duration = duration } }, onCanplay(e) { if (this.audioImg === iconLoading) { this.audioImg = iconPaused } console.log('onCanplay', e) }, onWaiting(e) { if (this.audioImg !== iconLoading) { this.audioImg = iconLoading } }, onPlay(e) { console.log('onPlay', e, this.audioCtx.duration) this.audioImg = iconPlaying if (this.audioCtx.duration > 0 && this.audioCtx.duration <= 1) { this.duration = 1 } else { this.duration = Math.floor(this.audioCtx.duration) } }, onPause(e) { console.log('onPause', e) this.audioImg = iconPaused }, onEnded(e) { console.log('onEnded', e) if (this.audioImg !== iconPaused) { this.audioImg = iconPaused } afterAudioPlay() }, onError(e) { uni.showToast({ title: '音频加载失败', icon: 'none' }) throw new Error(e.errMsg, e.errCode) }, fmtSecond(sec) { const { min, second } = formatSecondToHHmmss(sec) return `${min}:${second}` } },复制代码
同样的scss
오디오 구성 요소 요구 사항 및 제한 사항
1. 재생 또는 일시 정지
2. 재생 진행률 및 전체 지속 시간 표시
3. 아이콘 변경/재생/로딩)
4. 페이지 오디오가 업데이트되면 구성요소 상태를 새로 고칩니다.
5. 전체적으로 재생 상태인 오디오는 하나만 있습니다.
6. 페이지를 떠난 후 오디오를 삭제합니다. 예시
재료/속성/방법
시작합시다uni-app Vue
- 동일한 우선
DOM
구조 구성
<style lang="scss" scoped>.custom-audio { border-radius: 8vw; border: #CCC 1px solid; background: #F3F6FC; color: #333; display: flex; flex-flow: row nowrap; align-items: center; justify-content: space-between; padding: 2vw; font-size: 14px;
.audio-btn { width: 10vw; height: 10vw; white-space: nowrap; display: flex; align-items: center; justify-content: center;
}
}
</style>复制代码
로그인 후 복사- 허용되는 구성 요소 정의
rrreee - CustomAudio 구성 요소의 초기화와 관련된
작업을 정의하고 <code>innerAudioContext
의 콜백에 일부 동작을 추가합니다(이전 Taro 기사에서 밟았던 함정은 직접적으로 여기에 코딩됨)
ul>rrreee동일한 scss
파일rrreee
Finally
문제가 발생하거나 제안 사항이 있으면 팔로우하세요. 토론해 보겠습니다~🎜🎜🎜다른 훌륭한 기사를 알고 싶다면 🎜uni-app🎜 칼럼을 방문하세요~🎜🎜
위 내용은 InnerAudioContext를 기반으로 기본 오디오 구성 요소를 캡슐화하는 uni-app(vue)에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
2. 재생 진행률 및 전체 지속 시간 표시
3. 아이콘 변경/재생/로딩)
4. 페이지 오디오가 업데이트되면 구성요소 상태를 새로 고칩니다.
5. 전체적으로 재생 상태인 오디오는 하나만 있습니다.
6. 페이지를 떠난 후 오디오를 삭제합니다. 예시

uni-app Vue
- 동일한 우선
DOM
구조 구성
<style lang="scss" scoped>.custom-audio { border-radius: 8vw; border: #CCC 1px solid; background: #F3F6FC; color: #333; display: flex; flex-flow: row nowrap; align-items: center; justify-content: space-between; padding: 2vw; font-size: 14px; .audio-btn { width: 10vw; height: 10vw; white-space: nowrap; display: flex; align-items: center; justify-content: center; } } </style>复制代码
- 허용되는 구성 요소 정의
- CustomAudio 구성 요소의 초기화와 관련된
작업을 정의하고 <code>innerAudioContext
의 콜백에 일부 동작을 추가합니다(이전 Taro 기사에서 밟았던 함정은 직접적으로 여기에 코딩됨) ul>rrreee
동일한 scss
파일rrreee
Finally
문제가 발생하거나 제안 사항이 있으면 팔로우하세요. 토론해 보겠습니다~🎜🎜🎜다른 훌륭한 기사를 알고 싶다면 🎜uni-app🎜 칼럼을 방문하세요~🎜🎜
위 내용은 InnerAudioContext를 기반으로 기본 오디오 구성 요소를 캡슐화하는 uni-app(vue)에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











VSCode에서 uni-app을 개발하는 방법은 무엇입니까? 다음 기사에서는 VSCode에서 uni-app을 개발하는 방법에 대한 튜토리얼을 공유할 것입니다. 이것은 아마도 가장 훌륭하고 자세한 튜토리얼일 것입니다. 와서 살펴보세요!

uniapp을 사용하여 간단한 지도 탐색을 개발하는 방법은 무엇입니까? 이 기사는 간단한 지도를 만드는 데 도움이 되기를 바랍니다.

uniapp을 사용하여 스네이크 게임을 개발하는 방법은 무엇입니까? 다음 기사는 uniapp에서 Snake 게임을 구현하는 방법을 단계별로 설명합니다. 도움이 되기를 바랍니다.

uni-app 인터페이스, 전역 메서드 캡슐화 1. 루트 디렉터리에 api 파일을 생성하고 api 폴더에 api.js, baseUrl.js 및 http.js 파일을 생성합니다. 2.baseUrl.js 파일 코드 importdefault"https://XXXX .test03.qcw800.com/api/"3.http.js 파일 코드 내보내기 기능https(opts,data){lethttpDefaultOpts={url:opts.url,data:data,method:opts.method

이번 글에서는 다중 선택박스의 전체 선택 기능 구현과 관련된 이슈를 주로 정리한 uniapp 관련 지식을 소개합니다. 전체 선택 기능을 구현할 수 없는 이유는 체크박스의 체크된 필드가 동적으로 수정하면 인터페이스의 상태가 실시간으로 변경될 수 있지만 checkbox-group의 변경 이벤트는 트리거될 수 없습니다. 모두에게 도움이 되기를 바랍니다.

이 글은 유니앱 캘린더 플러그인 개발 과정을 단계별로 안내하고, 다음 캘린더 플러그인 개발부터 출시까지 어떻게 진행되는지 소개하는 글이 여러분께 도움이 되길 바랍니다!

uniapp은 스크롤 보기 드롭다운 로딩을 어떻게 구현합니까? 다음 기사에서는 uniapp WeChat 애플릿 스크롤 보기의 드롭다운 로딩에 대해 설명합니다. 모든 사람에게 도움이 되기를 바랍니다.

이 기사는 uniapp에 대한 관련 지식을 제공합니다. 주로 uniapp을 사용하여 전화를 걸고 녹음을 동기화하는 방법을 소개합니다. 관심 있는 친구들이 꼭 읽어보시기 바랍니다.
