论坛风格切换切换到宽版
  • 12阅读
  • 3回复

uniapp [复制链接]

上一主题 下一主题
离线北斗星
 

  1. <template>
  2.     <view class="container">
  3.         <view class="timer-display">{{ formatTime(remainingTime) }}</view>
  4.         <view class="settings">
  5.             <view class="item">
  6.                 <text>倒计时总时长 (分钟):</text>
  7.                 <input type="number" v-model="duration" :disabled="isRunning" />
  8.             </view>
  9.             <view class="item">
  10.                 <text>提醒间隔 (1-5分钟):</text>
  11.                 <picker @change="onIntervalChange" :value="intervalIndex" :range="intervalOptions" :disabled="isRunning">
  12.                     <view class="picker-text">{{ intervalOptions[intervalIndex] }} 分钟</view>
  13.                 </picker>
  14.             </view>
  15.             <view class="item">
  16.                 <text>背景音乐:</text>
  17.                 <switch :checked="useBgMusic" @change="useBgMusic = $event.detail.value" :disabled="isRunning" />
  18.             </view>
  19.         </view>
  20.         <view class="controls">
  21.             <button v-if="!isRunning" type="primary" @click="startTimer">开始闹钟</button>
  22.             <button v-else type="warn" @click="stopTimer">停止/重置</button>
  23.         </view>
  24.     </view>
  25. </template>
  26. <script>
  27. export default {
  28.     data() {
  29.         return {
  30.             duration: 10,           // 总时长(分)
  31.             remainingTime: 600,     // 剩余时间(秒)
  32.             intervalOptions: [1, 2, 3, 4, 5],
  33.             intervalIndex: 0,       // 默认1分钟提醒一次
  34.             isRunning: false,
  35.             useBgMusic: true,
  36.             
  37.             timer: null,
  38.             bgAudio: null,
  39.             alertAudio: null,
  40.             lastRemindTime: 0       // 上次提醒时的剩余时间
  41.         };
  42.     },
  43.     onLoad() {
  44.         // 初始化音频实例
  45.         this.bgAudio = uni.createInnerAudioContext();
  46.         this.bgAudio.src = '/static/bg_music.mp3';
  47.         this.bgAudio.loop = true;
  48.         this.alertAudio = uni.createInnerAudioContext();
  49.         this.alertAudio.src = '/static/alert_tone.mp3';
  50.     },
  51.     methods: {
  52.         onIntervalChange(e) {
  53.             this.intervalIndex = e.detail.value;
  54.         },
  55.         formatTime(seconds) {
  56.             const m = Math.floor(seconds / 60);
  57.             const s = seconds % 60;
  58.             return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
  59.         },
  60.         startTimer() {
  61.             this.remainingTime = this.duration * 60;
  62.             this.lastRemindTime = this.remainingTime;
  63.             this.isRunning = true;
  64.             // 播放背景音乐
  65.             if (this.useBgMusic) {
  66.                 this.bgAudio.play();
  67.             }
  68.             this.timer = setInterval(() => {
  69.                 if (this.remainingTime > 0) {
  70.                     this.remainingTime--;
  71.                     this.checkIntervalRemind();
  72.                 } else {
  73.                     this.handleFinish();
  74.                 }
  75.             }, 1000);
  76.         },
  77.         checkIntervalRemind() {
  78.             const intervalSeconds = this.intervalOptions[this.intervalIndex] * 60;
  79.             // 如果距离上次提醒过去了设定的间隔
  80.             if (this.lastRemindTime - this.remainingTime >= intervalSeconds) {
  81.                 this.alertAudio.play();
  82.                 this.lastRemindTime = this.remainingTime;
  83.                 uni.showToast({ title: '间隔提醒', icon: 'none' });
  84.             }
  85.         },
  86.         handleFinish() {
  87.             this.stopTimer();
  88.             this.alertAudio.play(); // 播放结束铃声
  89.             uni.showModal({
  90.                 title: '时间到',
  91.                 content: '闹钟已结束',
  92.                 showCancel: false
  93.             });
  94.         },
  95.         stopTimer() {
  96.             clearInterval(this.timer);
  97.             this.timer = null;
  98.             this.isRunning = false;
  99.             this.bgAudio.stop(); // 停止背景音乐
  100.         }
  101.     },
  102.     onUnload() {
  103.         // 页面销毁释放资源
  104.         if (this.bgAudio) this.bgAudio.destroy();
  105.         if (this.alertAudio) this.alertAudio.destroy();
  106.     }
  107. };
  108. </script>
  109. <style>
  110. .container { padding: 30px; }
  111. .timer-display { font-size: 60px; text-align: center; margin: 40px 0; font-family: monospace; color: #333; }
  112. .settings { background: #f8f8f8; padding: 15px; border-radius: 10px; }
  113. .item { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
  114. input { width: 60px; border-bottom: 1px solid #ccc; text-align: center; }
  115. .picker-text { color: #007AFF; font-weight: bold; }
  116. .controls { margin-top: 30px; }
  117. </style>


离线北斗星

只看该作者 沙发   发表于: 01-20
请用uniapp写一个倒计时闹钟,可以选择背景音乐,间隔1-5分钟提醒一次,可选择提醒铃声,结果时背景音乐停止播放,并可选择提醒音乐。
这是一个使用 uni-app 开发的倒计时闹钟核心代码实现。它包含了倒计时逻辑、背景音乐管理、间隔提醒(每1-5分钟)以及结束提醒功能。
1. 准备工作
请在项目的 static 目录下放置音频文件:
static/bg_music.mp3 (背景音乐)
static/alert_tone.mp3 (提醒/结束铃声)
离线北斗星

只看该作者 板凳   发表于: 01-20
3. 功能要点说明:
背景音乐逻辑:使用 uni.createInnerAudioContext()。点击“开始”后背景音乐循环播放,倒计时归零或手动停止时,调用 bgAudio.stop() 强制停止。
间隔提醒:在 setInterval 每秒执行时,计算当前剩余时间与上次提醒时间的差值。如果达到用户选中的间隔(1-5分钟),则触发提醒铃声。
铃声选择:代码中演示了固定路径音频。若需让用户选择,可增加一个列表,点击时修改 alertAudio.src 即可。
2026年适配:该代码遵循 uni-app 标准 API,在 iOS、Android 和微信小程序等 2026 年主流平台上均能稳定运行。
4. 注意事项
静音开关:在手机端运行时,请确保手机未开启静音模式,否则 InnerAudioContext 可能没有声音。
后台运行:如果是移动端 App,长时间倒计时建议配合“保活”插件或系统通知权限,否则 App 进入后台后定时器可能会被系统挂起。
离线北斗星

只看该作者 地板   发表于: 01-21
3. 功能要点说明:
背景音乐逻辑:使用 uni.createInnerAudioContext()。点击“开始”后背景音乐循环播放,倒计时归零或手动停止时,调用 bgAudio.stop() 强制停止。
间隔提醒:在 setInterval 每秒执行时,计算当前剩余时间与上次提醒时间的差值。如果达到用户选中的间隔(1-5分钟),则触发提醒铃声。
铃声选择:代码中演示了固定路径音频。若需让用户选择,可增加一个列表,点击时修改 alertAudio.src 即可。
2026年适配:该代码遵循 uni-app 标准 API,在 iOS、Android 和微信小程序等 2026 年主流平台上均能稳定运行。
4. 注意事项
静音开关:在手机端运行时,请确保手机未开启静音模式,否则 InnerAudioContext 可能没有声音。
后台运行:如果是移动端 App,长时间倒计时建议配合“保活”插件或系统通知权限,否则 App 进入后台后定时器可能会被系统挂起。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个