解决iOS不能播放音频文件流问题

在获取到音频文件流生成URL给audio时,在iOS上不能正常播放

解决代码

        this.request.post('/proxy/speech/tts', formData, {
            responseType: 'blob'
        }).then((res) => {
            this.setState({loadingId: 'loading'})
            console.log(res)
            let type = 'application/octet-stream'
            // const buf = Buffer.from(result, 'binary')
            let blob = new Blob([res.data], {type: type})
            let fileName = res.headers.filename || 'test.mp3'
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            } else {
                let URL = window.URL || window.webkitURL
                let objectUrl = URL.createObjectURL(blob)
                console.log(objectUrl)
                let as = document.getElementById('audioTags')
                as.src = objectUrl
                as.autoplay = "autoplay";
                as.controls = "controls";
                let a = document.getElementById('audioTag')
                a.load()
            }
        }).catch((err) => {
            console.log(err)
        });

      // 播放
      handleCloseModalTip = () => {
        let a = document.getElementById('audioTag')
        a.play().then(() => {
            let time = a.duration * 1000 || 0
            setTimeout(()=>{
                this.setState({
                    labRecord: '',
                    disabled: false
                })
            }, time)
        }).catch((error) => {
            Alert.error('音频文件有误,请刷新页面重试!')
        })        
      };


                

你可能感兴趣的:(解决iOS不能播放音频文件流问题)