Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
f0cc4ff8
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6005
Star
91
Fork
164
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
f0cc4ff8
编写于
10月 30, 2024
作者:
M
mahaifeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[audio]添加音频示例
上级
615b79f6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
162 addition
and
148 deletion
+162
-148
pages/API/create-inner-audio-context/create-inner-audio-context.uvue
...reate-inner-audio-context/create-inner-audio-context.uvue
+14
-8
pages/API/create-inner-audio-context/inner-audio-format.uvue
pages/API/create-inner-audio-context/inner-audio-format.uvue
+145
-137
pages/API/create-inner-audio-context/inner-audio-path.uvue
pages/API/create-inner-audio-context/inner-audio-path.uvue
+3
-3
未找到文件。
pages/API/create-inner-audio-context/create-inner-audio-context.uvue
浏览文件 @
f0cc4ff8
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
<button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button>
<button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button>
<button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button>
<button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button>
<button :disabled="!isPlaying && !isPaused" type="primary" @click="stop" class="uni-btn">停止</button>
<button :disabled="!isPlaying && !isPaused" type="primary" @click="stop" class="uni-btn">停止</button>
<button type="primary" @click="onchange(20)" class="uni-btn">跳转到指定位置20</button>
<button type="primary" @click="onchange
Value
(20)" class="uni-btn">跳转到指定位置20</button>
<view class="uni-title">
<view class="uni-title">
<text class="uni-title-text">格式/路径示例</text>
<text class="uni-title-text">格式/路径示例</text>
...
@@ -87,24 +87,30 @@
...
@@ -87,24 +87,30 @@
this.isCanplay = true;
this.isCanplay = true;
// 当音频可以播放时,获取缓冲信息
// 当音频可以播放时,获取缓冲信息
this.buffered = this._audioContext!.buffered;
this.buffered = this._audioContext!.buffered;
this.duration = this._audioContext!.duration
|| 0;
this.duration = this._audioContext!.duration
});
});
},
},
onchanging() {
onchanging() {
this._isChanging = true;
this._isChanging = true;
},
},
onchange(e) {
onchange(e
: UniSliderChangeEvent
) {
console.log(e, 'e');
console.log(e, 'e');
let pos =
typeof e === "number" ? e :
e.detail.value;
let pos = e.detail.value;
this._audioContext!.seek(pos);
this._audioContext!.seek(pos);
this.onSeeking()
this.onSeeking()
this.onSeeked()
this.onSeeked()
this._isChanging = false;
this._isChanging = false;
},
},
onchangeValue(pos:number) {
this._audioContext!.seek(pos);
this.onSeeking()
this.onSeeked()
this._isChanging = false;
},
startTimeInput(e : InputEvent) {
startTimeInput(e : InputEvent) {
let startTimeValue =
Number
(e.detail.value)
let startTimeValue =
parseInt
(e.detail.value)
this._audioContext!.startTime = startTimeValue;
this._audioContext!.startTime = startTimeValue;
this.onchange(startTimeValue)
this.onchange
Value
(startTimeValue)
},
},
setAutoplay() {
setAutoplay() {
this._audioContext!.autoplay = !this._audioContext!.autoplay;
this._audioContext!.autoplay = !this._audioContext!.autoplay;
...
@@ -125,7 +131,7 @@
...
@@ -125,7 +131,7 @@
this._audioContext!.play();
this._audioContext!.play();
this.isPlayEnd = false;
this.isPlayEnd = false;
if (this._audioContext!.startTime > 0) {
if (this._audioContext!.startTime > 0) {
this.onchange(this._audioContext!.startTime)
this.onchange
Value
(this._audioContext!.startTime)
}
}
this._audioContext!.onPlay(() => {
this._audioContext!.onPlay(() => {
this.isPaused = false;
this.isPaused = false;
...
@@ -158,7 +164,7 @@
...
@@ -158,7 +164,7 @@
this._audioContext!.onTimeUpdate(() => {
this._audioContext!.onTimeUpdate(() => {
// console.log('onTimeUpdate:音频播放进度更新事件,currentTime',this._audioContext!.currentTime);
// console.log('onTimeUpdate:音频播放进度更新事件,currentTime',this._audioContext!.currentTime);
if (this._isChanging === true) { return; }
if (this._isChanging === true) { return; }
this.currentTime = this._audioContext!.currentTime
||
0;
this.currentTime = this._audioContext!.currentTime
??
0;
console.log('currentTime', this.currentTime);
console.log('currentTime', this.currentTime);
if (this.currentTime > this.buffered) {
if (this.currentTime > this.buffered) {
console.log('缓冲不足');
console.log('缓冲不足');
...
...
pages/API/create-inner-audio-context/inner-audio-format.uvue
浏览文件 @
f0cc4ff8
<template>
<template>
<page-head :title="title"></page-head>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<!-- #ifdef APP -->
<view class="uni-title">
<scroll-view style="flex: 1;">
<text class="uni-title-text">支持的音频格式示例</text>
<!-- #endif -->
</view>
<view class="uni-padding-wrap uni-common-mt">
<view class="formats" v-for="(item,index) in supportFormats" :key="index">
<view class="uni-title">
<text class="uni-subtitle-text">{{item.format}}</text>
<text class="uni-title-text">支持的音频格式示例</text>
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
</view>
@click="play(item.src,index)"></image>
</view>
<view class="formats" v-for="(item,index) in supportFormats" :key="index">
<text class="uni-subtitle-text">{{item.format}}</text>
<view class="uni-title">
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
<text class="uni-title-text">不支持的音频格式</text>
@click="play(item.src,index)"></image>
</view>
</view>
<view class="formats" v-for="(item,index) in notSupportFormats" :key="index">
<text class="uni-subtitle-text">{{item.format}}</text>
<view class="uni-title">
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
<text class="uni-title-text">不支持的音频格式</text>
@click="play(item.src,index)"></image>
</view>
</view>
<view class="formats" v-for="(item,index) in notSupportFormats" :key="index">
</view>
<text class="uni-subtitle-text">{{item.format}}</text>
</template>
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
@click="play(item.src,index)"></image>
<script>
</view>
type AudioFormat = {
format : string
</view>
src : string
<!-- #ifdef APP -->
}
</scroll-view>
export default {
<!-- #endif -->
data() {
</template>
return {
title: 'audio-format',
<script>
playIndex: 0,
type AudioFormat = {
isPlaying: false,
format : string
_audioContext: null as InnerAudioContext | null,
src : string
supportFormats: [
}
{
export default {
format: 'mp3',
data() {
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
return {
},
title: 'audio-format',
{
playIndex: 0,
format: 'mp4',
isPlaying: false,
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4'
_audioContext: null as InnerAudioContext | null,
},
supportFormats: [
{
{
format: 'm4a',
format: 'mp3',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
},
},
{
{
format: 'aac',
format: 'mp4',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4'
},
},
{
{
format: 'flac',
format: 'm4a',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a'
},
},
{
{
format: 'aac',
format: 'ogg',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg'
},
},
{
{
format: 'flac',
format: 'wav',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav'
},
},
] as Array<AudioFormat>,
{
notSupportFormats: [
format: 'ogg',
{
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg'
format: 'wma',
},
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma'
{
},
format: 'wav',
{
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav'
format: 'aiff',
},
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff'
] as Array<AudioFormat>,
},
notSupportFormats: [
{
{
format: 'caf',
format: 'wma',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma'
},
},
{
{
format: '错误格式',
format: 'aiff',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff'
},
},
] as Array<AudioFormat>
{
}
format: 'caf',
},
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf'
onReady() {
},
this._audioContext = uni.createInnerAudioContext();
{
},
format: '错误格式',
onUnload() {
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa'
if (this._audioContext != null) {
},
this.pause();
] as Array<AudioFormat>
this._audioContext!.destroy()
}
}
},
},
onReady() {
methods: {
this._audioContext = uni.createInnerAudioContext();
pause() {
},
this._audioContext!.pause();
onUnload() {
this.isPlaying = false;
if (this._audioContext != null) {
},
this.pause();
play(audioUrl, index) {
this._audioContext!.destroy()
// console.log(index,audioUrl);
}
if (this.isPlaying && this.playIndex == index) {
},
this.pause();
methods: {
return;
pause() {
}
this._audioContext!.pause();
this.playIndex = index
this.isPlaying = false;
this._audioContext!.src = audioUrl;
},
this._audioContext!.play();
play(audioUrl : string, index : number) {
this.isPlaying = true;
// console.log(index,audioUrl);
this._audioContext!.onPlay(() => {
if (this.isPlaying && this.playIndex == index) {
console.log('开始播放');
this.pause();
});
return;
this._audioContext!.onEnded(() => {
}
console.log('播放结束');
this.playIndex = index
this.isPlaying = false;
this._audioContext!.src = audioUrl;
});
this._audioContext!.play();
this._audioContext!.onError((err) => {
this.isPlaying = true;
this.isPlaying = false;
this._audioContext!.onPlay(() => {
console.log('err', err);
console.log('开始播放');
});
});
},
this._audioContext!.onEnded(() => {
},
console.log('播放结束');
}
this.isPlaying = false;
</script>
});
this._audioContext!.onError((err) => {
<style>
this.isPlaying = false;
.formats {
console.log('err', err);
align-items: center;
});
}
},
},
.icon-play {
}
width: 60px;
</script>
height: 60px;
margin: 10px;
<style>
}
.formats {
</style>
align-items: center;
}
.icon-play {
width: 60px;
height: 60px;
margin: 10px;
}
</style>
\ No newline at end of file
pages/API/create-inner-audio-context/inner-audio-path.uvue
浏览文件 @
f0cc4ff8
...
@@ -29,7 +29,7 @@
...
@@ -29,7 +29,7 @@
description: '本地路径:/static方式',
description: '本地路径:/static方式',
src: '/static/test-audio/ForElise.mp3'
src: '/static/test-audio/ForElise.mp3'
},
},
{
{
description: '本地路径:../static/',
description: '本地路径:../static/',
src: '../../../static/test-audio/ForElise.mp3'
src: '../../../static/test-audio/ForElise.mp3'
},
},
...
@@ -62,7 +62,7 @@
...
@@ -62,7 +62,7 @@
this._audioContext!.pause();
this._audioContext!.pause();
this.isPlaying = false;
this.isPlaying = false;
},
},
play(audioUrl
, index
) {
play(audioUrl
:string, index:number
) {
// console.log(index,audioUrl);
// console.log(index,audioUrl);
if (this.isPlaying && this.playIndex == index) {
if (this.isPlaying && this.playIndex == index) {
this.pause();
this.pause();
...
@@ -81,7 +81,7 @@
...
@@ -81,7 +81,7 @@
});
});
this._audioContext!.onError((err) => {
this._audioContext!.onError((err) => {
this.isPlaying = false;
this.isPlaying = false;
console.log('err', err);
console.log('err', err);
});
});
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录