提交 5e179b7c 编写于 作者: M mahaifeng

[audio]修改音频示例

上级 c83c48d1
...@@ -1097,16 +1097,16 @@ ...@@ -1097,16 +1097,16 @@
} }
}, },
// #endif // #endif
// #ifdef APP // // #ifdef APP
{ // {
"path": "pages/API/background-audio/background-audio", // "path": "pages/API/background-audio/background-audio",
"group": "1,7,9", // "group": "1,7,10",
"style": { // "style": {
"navigationBarTitleText": "backgroundAudio | 背景音乐" // "navigationBarTitleText": "backgroundAudio | 背景音乐"
} // }
}, // },
// #endif // // #endif
// #ifdef WEB || MP // #ifdef WEB || MP || APP
{ {
"path": "pages/API/create-inner-audio-context/create-inner-audio-context", "path": "pages/API/create-inner-audio-context/create-inner-audio-context",
"group": "1,7,9", "group": "1,7,9",
...@@ -1116,7 +1116,7 @@ ...@@ -1116,7 +1116,7 @@
}, },
// #endif // #endif
// #ifdef WEB || MP // #ifdef WEB || MP || APP
{ {
"path": "pages/API/create-inner-audio-context/inner-audio-format", "path": "pages/API/create-inner-audio-context/inner-audio-format",
"style": { "style": {
...@@ -1124,7 +1124,7 @@ ...@@ -1124,7 +1124,7 @@
} }
}, },
// #endif // #endif
// #ifdef WEB || MP // #ifdef WEB || MP ||APP
{ {
"path": "pages/API/create-inner-audio-context/inner-audio-path", "path": "pages/API/create-inner-audio-context/inner-audio-path",
"style": { "style": {
......
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
bgAudioMannager.title = '致爱丽丝'+this.count++; bgAudioMannager.title = '致爱丽丝'+this.count++;
bgAudioMannager.singer = '暂无2'+this.count++; bgAudioMannager.singer = '暂无2'+this.count++;
bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg'; bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
this.bgAudioMannager!!.src = this.dataUrl; this.bgAudioMannager!.src = this.dataUrl;
this.bgAudioMannager?.play() this.bgAudioMannager?.play()
}) })
bgAudioMannager.onPrev(() => { bgAudioMannager.onPrev(() => {
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
this.bgAudioMannager?.stop() this.bgAudioMannager?.stop()
bgAudioMannager.title = '致爱丽丝'+this.count--; bgAudioMannager.title = '致爱丽丝'+this.count--;
bgAudioMannager.singer = '暂无'+this.count--; bgAudioMannager.singer = '暂无'+this.count--;
this.bgAudioMannager!!.src = this.dataUrl; this.bgAudioMannager!.src = this.dataUrl;
this.bgAudioMannager?.play() this.bgAudioMannager?.play()
}) })
// bgAudioMannager.onTimeUpdate((e) => { // bgAudioMannager.onTimeUpdate((e) => {
...@@ -91,17 +91,17 @@ ...@@ -91,17 +91,17 @@
methods: { methods: {
play: function () { play: function () {
console.log('play') console.log('play')
this.bgAudioMannager!!.src = this.dataUrl; this.bgAudioMannager!.src = this.dataUrl;
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
if (UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, ["android.permission.POST_NOTIFICATIONS"])) { if (UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, ["android.permission.POST_NOTIFICATIONS"])) {
this.bgAudioMannager!!.play() this.bgAudioMannager!.play()
} else { } else {
// 检查当前 Android 版本和 targetSdkVersion // 检查当前 Android 版本和 targetSdkVersion
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Android 13 及以上版本 // Android 13 及以上版本
if ((UTSAndroid.getAppContext() as Application).applicationInfo.targetSdkVersion >= 33) { if ((UTSAndroid.getAppContext() as Application).applicationInfo.targetSdkVersion >= 33) {
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, ["android.permission.POST_NOTIFICATIONS"], (_ : boolean, p : string[]) => { UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, ["android.permission.POST_NOTIFICATIONS"], (_ : boolean, p : string[]) => {
this.bgAudioMannager!!.play() this.bgAudioMannager!.play()
}, (_ : boolean, p : string[]) => { }, (_ : boolean, p : string[]) => {
uni.showToast({ uni.showToast({
title: "权限被拒绝了", title: "权限被拒绝了",
......
<template> <template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;">
<!-- #endif -->
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<page-head title="audio"></page-head> <page-head title="audio"></page-head>
<view class="uni-common-mt"> <view class="uni-common-mt">
...@@ -39,6 +42,9 @@ ...@@ -39,6 +42,9 @@
<button type="primary" @click="pause">音频路径示例</button> <button type="primary" @click="pause">音频路径示例</button>
</navigator> </navigator>
</view> </view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template> </template>
<script lang="uts"> <script lang="uts">
const audioUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3' const audioUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
...@@ -163,8 +169,8 @@ ...@@ -163,8 +169,8 @@
onTimeUpdate() { onTimeUpdate() {
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) { return; }
this.currentTime = this._audioContext!.currentTime ?? 0; this.currentTime = this._audioContext!.currentTime
console.log('currentTime', this.currentTime); console.log('currentTime', this.currentTime);
if (this.currentTime > this.buffered) { if (this.currentTime > this.buffered) {
console.log('缓冲不足'); console.log('缓冲不足');
......
...@@ -94,6 +94,20 @@ ...@@ -94,6 +94,20 @@
}, },
onReady() { onReady() {
this._audioContext = uni.createInnerAudioContext(); this._audioContext = uni.createInnerAudioContext();
this._audioContext!.onPlay(() => {
console.log('开始播放');
});
this._audioContext!.onPause(()=>{
console.log('播放暂停');
})
this._audioContext!.onEnded(() => {
console.log('播放结束');
this.isPlaying = false;
});
this._audioContext!.onError((err) => {
this.isPlaying = false;
console.log('err', err);
});
}, },
onUnload() { onUnload() {
if (this._audioContext != null) { if (this._audioContext != null) {
...@@ -116,17 +130,6 @@ ...@@ -116,17 +130,6 @@
this._audioContext!.src = audioUrl; this._audioContext!.src = audioUrl;
this._audioContext!.play(); this._audioContext!.play();
this.isPlaying = true; this.isPlaying = true;
this._audioContext!.onPlay(() => {
console.log('开始播放');
});
this._audioContext!.onEnded(() => {
console.log('播放结束');
this.isPlaying = false;
});
this._audioContext!.onError((err) => {
this.isPlaying = false;
console.log('err', err);
});
}, },
}, },
} }
......
...@@ -43,13 +43,24 @@ ...@@ -43,13 +43,24 @@
}, },
{ {
description: '错误路径', description: '错误路径',
src: '../static/test-audio/ForElise.mp3' src: '../static/test-audio/ForElise22.mp3'
}, },
] as Array<AudioPath> ] as Array<AudioPath>
} }
}, },
onReady() { onReady() {
this._audioContext = uni.createInnerAudioContext(); this._audioContext = uni.createInnerAudioContext();
this._audioContext!.onPlay(() => {
console.log('开始播放');
});
this._audioContext!.onEnded(() => {
console.log('播放结束');
this.isPlaying = false;
});
this._audioContext!.onError((err) => {
this.isPlaying = false;
console.log('err', err);
});
}, },
onUnload() { onUnload() {
if (this._audioContext != null) { if (this._audioContext != null) {
...@@ -63,7 +74,7 @@ ...@@ -63,7 +74,7 @@
this.isPlaying = false; this.isPlaying = false;
}, },
play(audioUrl:string, index:number) { 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();
return; return;
...@@ -72,17 +83,6 @@ ...@@ -72,17 +83,6 @@
this._audioContext!.src = audioUrl; this._audioContext!.src = audioUrl;
this._audioContext!.play(); this._audioContext!.play();
this.isPlaying = true; this.isPlaying = true;
this._audioContext!.onPlay(() => {
console.log('开始播放');
});
this._audioContext!.onEnded(() => {
console.log('播放结束');
this.isPlaying = false;
});
this._audioContext!.onError((err) => {
this.isPlaying = false;
console.log('err', err);
});
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册