vue-cli init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
class AudioManager {
|
||||
constructor() {
|
||||
this.audioContext = null;
|
||||
this.isPlaying = false;
|
||||
}
|
||||
|
||||
init() {
|
||||
if (!this.audioContext) {
|
||||
// #ifdef APP-PLUS
|
||||
// 使用 plus.audio 而不是 createInnerAudioContext
|
||||
this.audioContext = plus.audio.createPlayer('/static/audio/ninyouxindepeibudan.mp3');
|
||||
|
||||
// 监听播放完成
|
||||
this.audioContext.addEventListener('ended', () => {
|
||||
console.log('Audio playback completed');
|
||||
this.isPlaying = false;
|
||||
});
|
||||
|
||||
// 监听错误
|
||||
this.audioContext.addEventListener('error', (e) => {
|
||||
console.error('Audio error:', e.message);
|
||||
this.isPlaying = false;
|
||||
});
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
|
||||
play() {
|
||||
console.log('audioContext',this.audioContext)
|
||||
console.log('isplay', this.isPlaying)
|
||||
if (!this.audioContext) {
|
||||
this.init();
|
||||
}
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
if (!this.isPlaying) {
|
||||
try {
|
||||
// 开始播放
|
||||
this.audioContext.play(() => {
|
||||
// 当音频文件播放完成时回调。
|
||||
this.isPlaying = false;
|
||||
console.log('开始播放音频');
|
||||
}, (e) => {
|
||||
console.error('播放音频失败:', e.message);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('播放异常:', e);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
stop() {
|
||||
// #ifdef APP-PLUS
|
||||
if (this.audioContext && this.isPlaying) {
|
||||
try {
|
||||
this.audioContext.stop();
|
||||
this.isPlaying = false;
|
||||
} catch (e) {
|
||||
console.error('停止播放失败:', e);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
destroy() {
|
||||
// #ifdef APP-PLUS
|
||||
if (this.audioContext) {
|
||||
try {
|
||||
this.stop();
|
||||
this.audioContext = null;
|
||||
} catch (e) {
|
||||
console.error('销毁音频播放器失败:', e);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
|
||||
export const audioManager = new AudioManager();
|
||||
@@ -0,0 +1,54 @@
|
||||
const SYSTEM_INFO = uni.getSystemInfoSync();
|
||||
|
||||
/**
|
||||
* 获取状态栏高度
|
||||
*/
|
||||
export const getStatusBarHeight = () => {
|
||||
// APP和小程序都可以直接获取状态栏高度
|
||||
return SYSTEM_INFO.statusBarHeight || 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取导航栏高度
|
||||
*/
|
||||
export const getNavigationBarHeight = () => {
|
||||
// #ifdef MP-WEIXIN
|
||||
// 微信小程序,通过胶囊按钮计算
|
||||
const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
|
||||
if (menuButtonInfo && menuButtonInfo.top && menuButtonInfo.height) {
|
||||
const statusBarHeight = getStatusBarHeight();
|
||||
return (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height;
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// APP端可以自定义导航栏高度,一般是44px
|
||||
return 44;
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
|
||||
// 其他小程序平台使用固定高度
|
||||
return 45;
|
||||
// #endif
|
||||
|
||||
return 44; // 默认值
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取完整的顶部安全区高度
|
||||
*/
|
||||
export const getTopSafeAreaHeight = () => {
|
||||
const statusBarHeight = getStatusBarHeight();
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// APP一般只需要状态栏高度,因为导航栏是自定义的
|
||||
return statusBarHeight;
|
||||
// #endif
|
||||
|
||||
// #ifdef MP
|
||||
// 小程序需要状态栏+导航栏的完整高度
|
||||
return statusBarHeight + getNavigationBarHeight();
|
||||
// #endif
|
||||
|
||||
return statusBarHeight; // 默认值
|
||||
};
|
||||
Reference in New Issue
Block a user