Page({
    data: {
        fullScreenBtn: false,
        playBtn: false,
        gesture: true,
        muted: true,
        gesture: false,
        centerBtn: false,
        recordFlag: 0,
        recordSource: '',
        videoCtr: 'recordingVideoEnd',

    },
    onLoad: function (option) {
        if (option.title) {
            wx.setNavigationBarTitle({
                title: option.title //页面标题为路由参数
            })
        }
        this.recorderManager = wx.getRecorderManager();
        this.videoCtx = wx.createVideoContext('myVideo', this);

        // 监听录音部分
        // 录音开始
        this.recorderManager.onStart(() => {
            this.videoCtx.play();
            this.setData({
                recordFlag: 1
            })
            console.log('recorder start')
        })
        // 录音结束
        this.recorderManager.onStop((res) => {
            this.videoCtx.stop();
            console.log('recorder stop', res)
            console.log(res.tempFilePath)
            const recordFile = res.tempFilePath;
            this.setData({
                recordFlag: 0,
                recordSource: recordFile
            })
        })
    },
    // onShow:function(){

    // },
    
    // 录音中视频播放结束 (控制录音同时结束)
    recordingVideoEnd:function(){
        // 
        if (this.data.recordFlag === 0) {
            this.recordStop();
            this.setData({
                videoCtr : 'playingVideoEnd'
            })
        }
        // 录音结束
        if (this.data.recordFlag === 1) {
            // this.recordStop();
        }
    },
    // 播放中视频播放结束 (控制录音同时结束)
    playingVideoEnd:function(){
        this.innerAudioContext.stop();
    },
    /***
     *  recordFlag: 
     *      0 初始状态
     *      1 录音中
     ***/
    audioRecord: function () {
        console.log(this.data.recordFlag)
        if (this.data.recordFlag === 0) {
            this.recordStart();
        }
        // 录音结束后
        if (this.data.recordFlag === 1) {
            this.recordStop();
        }
    },
    // 录音开始
    /**
     * duration: 时长 最长10分钟
            sampleRate: 44100, 采样率
            numberOfChannels: 1, 录音通道
            encodeBitRate: 192000, 码率
            format: 'mp3', 格式
            frameSize: 50 制定帧大小
     */
    recordStart: function () {
        const options = {
            duration: 600000,
            sampleRate: 44100,
            numberOfChannels: 1,
            encodeBitRate: 192000,
            format: 'mp3',
            frameSize: 50
        }
        this.recorderManager.start(options);
        
    },
    // 录音结束
    recordStop: function () {
        this.recorderManager.stop();
    },
    // 播放录音
    audioPlay: function () {
        console.log('音频播放');
        this.innerAudioContext = wx.createInnerAudioContext();
        this.innerAudioContext.onError((res) => {
            // 播放音频失败的回调
        })
        this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
        this.videoCtx.play();
        this.innerAudioContext.play();
    },
})