get-background-audio-manager.uvue 3.3 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4
<template>
	<view>
		<page-head :title="title"></page-head>
		<view class="uni-padding-wrap">
5
			<view class="uni-hello-text">注意:离开当前页面后背景音乐将保持播放</view>
M
mahaifeng 已提交
6
			<view class="page-body-buttons">
M
mahaifeng 已提交
7
				<template v-if="playing">
M
mahaifeng 已提交
8 9 10 11 12 13
					<view class="page-body-button" @tap="stop">
						<image class="image" src="/static/stop.png"></image>
					</view>
					<view class="page-body-button" @tap="pause">
						<image class="image" src="/static/pause.png"></image>
					</view>
M
mahaifeng 已提交
14 15
				</template>
				<template v-if="!playing">
M
mahaifeng 已提交
16 17 18
					<view class="page-body-button" @tap="play">
						<image class="image" src="/static/play.png"></image>
					</view>
M
mahaifeng 已提交
19
				</template>
M
mahaifeng 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
				<view class="page-body-button"></view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				title: 'backgroundAudio',
				bgAudioMannager: null as BackgroundAudioManager | null,
				dataUrl: 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3',
				playing: false,
				playTime: 0,
				formatedPlayTime: '00:00:00',
35
				count: 100,
M
mahaifeng 已提交
36 37 38 39 40
			}
		},
		onLoad: function () {
			let bgAudioMannager = uni.getBackgroundAudioManager();
			bgAudioMannager.title = '致爱丽丝';
41
			bgAudioMannager.singer = '路德维希·范·贝多芬';
M
mahaifeng 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
			bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';

			bgAudioMannager.onPlay(() => {
				console.log("开始播放");
				this.playing = true;
			})
			bgAudioMannager.onPause(() => {
				console.log("暂停播放");
				this.playing = false;
			})
			bgAudioMannager.onEnded(() => {
				this.playing = false;
			})
			bgAudioMannager.onNext(() => {
				console.log("下一曲");
				this.bgAudioMannager?.stop()
58
				bgAudioMannager.title = '致爱丽丝' + this.count++;
59
				bgAudioMannager.singer = '路德维希·范·贝多芬';
M
mahaifeng 已提交
60
				bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
M
mahaifeng 已提交
61
				this.bgAudioMannager!.src = this.dataUrl;
M
mahaifeng 已提交
62 63 64 65 66
				this.bgAudioMannager?.play()
			})
			bgAudioMannager.onPrev(() => {
				console.log("上一曲");
				this.bgAudioMannager?.stop()
67
				bgAudioMannager.title = '致爱丽丝' + this.count--;
68
				bgAudioMannager.singer = '路德维希·范·贝多芬';
M
mahaifeng 已提交
69
				this.bgAudioMannager!.src = this.dataUrl;
M
mahaifeng 已提交
70 71 72
				this.bgAudioMannager?.play()
			})
			this.bgAudioMannager = bgAudioMannager;
73
			this.playing = !bgAudioMannager.paused
M
mahaifeng 已提交
74 75 76 77
		},
		methods: {
			play: function () {
				console.log('play')
M
mahaifeng 已提交
78
				this.bgAudioMannager!.src = this.dataUrl;
79
				this.bgAudioMannager!.play()
M
mahaifeng 已提交
80 81 82 83 84 85
			},
			pause: function () {
				this.bgAudioMannager?.pause();
			},
			stop: function () {
				this.bgAudioMannager?.stop();
M
mahaifeng 已提交
86
				this.playing = false
M
mahaifeng 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			}
		}
	}
</script>

<style>
	.image {
		width: 150rpx;
		height: 150rpx;
	}

	.page-body-text {
		padding: 0 30rpx;
	}

	.page-body-wrapper {
		margin-top: 0;
	}

	.page-body-info {
		padding-bottom: 50rpx;
	}

	.time-big {
		font-size: 60rpx;
		margin: 20rpx;
	}

	.slider {
		width: 630rpx;
	}

	.play-time {
		width: 100%;
		padding: 20rpx 0;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.page-body-buttons {
		display: flex;
		justify-content: center;
		margin-top: 100rpx;
	}

	.page-body-button {
		flex-direction: row;
		justify-content: center;
M
mahaifeng 已提交
136
    margin-bottom: 10px;
M
mahaifeng 已提交
137
	}
138
</style>