AudioStreamDemo.vue 5.79 KB
<template>
  <!-- #ifdef APP-PLUS -->
  <view class="audio-demo-root">
    <view
      class="audio-demo-fab"
      :class="{ running: isRunning }"
      @tap.stop="onFabClick"
    >
      <text class="fab-label">{{ fabText }}</text>
      <text class="fab-sub">PCM</text>
    </view>

    <view v-if="panelVisible" class="audio-demo-mask" @tap="panelVisible = false">
      <view class="audio-demo-panel" @tap.stop>
        <view class="panel-head">
          <text class="panel-title">录音 / WS Demo</text>
          <text class="panel-status">{{ statusText }}</text>
          <view class="panel-actions">
            <text class="act" @tap.stop="clearLogs">清空</text>
            <text class="act danger" @tap.stop="panelVisible = false">关闭</text>
          </view>
        </view>
        <scroll-view class="panel-scroll" scroll-y :scroll-into-view="scrollIntoView">
          <view
            v-for="(line, idx) in logs"
            :key="line.time + '_' + idx"
            :id="'demo-log-' + idx"
            :class="['log-line', 'lv-' + line.level]"
          >
            <text class="log-text">{{ line.text }}</text>
          </view>
          <view v-if="!logs.length" class="log-empty">点击悬浮按钮开始:录音 5s → WS 上传</view>
        </scroll-view>
        <view class="panel-foot">
          <text class="hint">日志同步至 uni.$logger 与悬浮「日志」球(若已开启)</text>
        </view>
      </view>
    </view>
  </view>
  <!-- #endif -->
</template>

<script>
// #ifdef APP-PLUS
import AudioStreamDemoRunner from '@/utils/audioStream/AudioStreamDemoRunner.js'
// #endif

const STATUS_MAP = {
  idle: '待命',
  connecting: '连接 WS…',
  recording: '录音中(5s)',
  uploading: '上传中…',
  done: '完成'
}

export default {
  name: 'AudioStreamDemo',
  data() {
    return {
      panelVisible: false,
      status: 'idle',
      logs: [],
      scrollIntoView: '',
      // #ifdef APP-PLUS
      runner: null
      // #endif
    }
  },
  computed: {
    isRunning() {
      return this.status === 'connecting' || this.status === 'recording' || this.status === 'uploading'
    },
    fabText() {
      if (this.status === 'recording') return '录'
      if (this.status === 'uploading') return '传'
      return '测'
    },
    statusText() {
      return STATUS_MAP[this.status] || this.status
    }
  },
  created() {
    // #ifdef APP-PLUS
    this.runner = new AudioStreamDemoRunner({
      onLog: (entry) => this.appendLog(entry),
      onStatus: (s) => {
        this.status = s
      }
    })
    // #endif
  },
  beforeDestroy() {
    // #ifdef APP-PLUS
    this.runner?.cancel()
    // #endif
  },
  methods: {
    onFabClick() {
      // #ifndef APP-PLUS
      uni.showToast({ title: '仅 App 端', icon: 'none' })
      return
      // #endif
      this.panelVisible = true
      if (this.isRunning) return
      this.startDemo()
    },
    async startDemo() {
      // #ifdef APP-PLUS
      this.appendLog({ level: 'info', text: '[AudioDemo] 点击开始', time: Date.now() })
      const ok = await this.runner.start()
      if (!ok && this.status === 'idle') {
        uni.showToast({ title: '启动失败,看日志', icon: 'none' })
      }
      // #endif
    },
    appendLog(entry) {
      this.logs.push(entry)
      if (this.logs.length > 300) {
        this.logs.splice(0, this.logs.length - 300)
      }
      this.$nextTick(() => {
        this.scrollIntoView = 'demo-log-' + (this.logs.length - 1)
      })
    },
    clearLogs() {
      this.logs = []
    }
  }
}
</script>

<style lang="scss" scoped>
/* 全屏占位但不挡点击,仅悬浮钮可点(与 FloatLogPanel 同模式) */
.audio-demo-root {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100010;
  pointer-events: none;
}

.audio-demo-fab {
  position: fixed;
  left: 24rpx;
  top: 120rpx;
  pointer-events: auto;
  width: 112rpx;
  height: 112rpx;
  border-radius: 56rpx;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  box-shadow: 0 8rpx 24rpx rgba(37, 99, 235, 0.55);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  &.running {
    background: linear-gradient(135deg, #ef4444, #dc2626);
    animation: pulse 1s ease-in-out infinite;
  }
}
.fab-label {
  color: #fff;
  font-size: 34rpx;
  font-weight: 600;
  line-height: 1.1;
}
.fab-sub {
  color: rgba(255, 255, 255, 0.85);
  font-size: 18rpx;
  margin-top: 4rpx;
}
@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.06);
  }
}
.audio-demo-mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  pointer-events: auto;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
}
.audio-demo-panel {
  width: 100%;
  max-height: 70vh;
  background: #1e1e1e;
  border-radius: 24rpx 24rpx 0 0;
  display: flex;
  flex-direction: column;
}
.panel-head {
  padding: 24rpx 28rpx 16rpx;
  border-bottom: 1px solid #333;
}
.panel-title {
  color: #f5f5f5;
  font-size: 30rpx;
  font-weight: 600;
}
.panel-status {
  display: block;
  margin-top: 8rpx;
  color: #93c5fd;
  font-size: 24rpx;
}
.panel-actions {
  margin-top: 16rpx;
  display: flex;
  gap: 24rpx;
}
.act {
  color: #60a5fa;
  font-size: 26rpx;
  &.danger {
    color: #f87171;
  }
}
.panel-scroll {
  flex: 1;
  max-height: 50vh;
  padding: 16rpx 24rpx;
  box-sizing: border-box;
}
.log-line {
  margin-bottom: 12rpx;
  font-size: 22rpx;
  line-height: 1.45;
  word-break: break-all;
}
.log-text {
  color: #d4d4d4;
}
.lv-warn .log-text {
  color: #fbbf24;
}
.lv-error .log-text {
  color: #f87171;
}
.lv-debug .log-text {
  color: #9ca3af;
}
.log-empty {
  color: #6b7280;
  font-size: 24rpx;
  padding: 40rpx 0;
  text-align: center;
}
.panel-foot {
  padding: 16rpx 24rpx 32rpx;
  border-top: 1px solid #333;
}
.hint {
  color: #6b7280;
  font-size: 22rpx;
}
</style>