import { requestMinimaxi } from './minimaxi_stream.js';
import { getMinimaxiConfig } from './config.js';

export async function playVideoWithAudio(videoPath, text) {
  // 1. 初始化视频播放
  const video = document.createElement('video');
  video.src = videoPath;
  document.body.appendChild(video);
  
  // 2. 启动音频合成流
  const minimaxiConfig = getMinimaxiConfig();
  const audioStream = await requestMinimaxi({
    apiKey: minimaxiConfig.apiKey,
    groupId: minimaxiConfig.groupId,
    body: {
      model: 'speech-01-turbo',
      text,
      output_format: 'hex', // 流式场景必须使用hex
      voice_setting: {
        voice_id: 'tianbing_xinggan_03',
        speed: 1
      }
    },
    stream: true
  });

  // 3. 将音频hex转换为可播放格式
  const audioCtx = new AudioContext();
  const audioBuffer = await audioCtx.decodeAudioData(
    hexToArrayBuffer(audioStream.data.audio)
  );
  
  // 4. 同步播放
  const source = audioCtx.createBufferSource();
  source.buffer = audioBuffer;
  source.connect(audioCtx.destination);
  
  video.play();
  source.start(0);
}

function hexToArrayBuffer(hex) {
  // ... hex转ArrayBuffer实现
}