美文网首页
解决iOS不能播放音频文件流问题

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

作者: ThemisHoo | 来源:发表于2019-06-17 21:05 被阅读0次

在获取到音频文件流生成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('音频文件有误,请刷新页面重试!')
        })        
      };


                <audio id='audioTag' type='audio/mpeg' preload="auto">
                    <source id='audioTags' type='audio/mpeg'/>
                </audio>

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/qgzzfctx.html