html52.com
我爱小程序-开发者交流社区

API音频接口在真机上无法播放下载接口所创建的本地临时资源文件?

问题模块
API和组件



手机上利用downloadFile下载服务器端一个音频文件,代码如下:

wx.downloadFile({
           url: 'http://autotest.car580.com/blackmusic.mp3',
           success: function (res) {
               console.log('背景音乐下载完成', res)
               that.setData({
                   backauto: res.tempFilePath,
                   backautoText: "背景音乐下载完成"
               })
           }
       })

然后使用getBackgroundAudioManager接口播放downloadFile接口所创建的本地临时资源文件(路径),代码如下:

const backgroundAudioManager = wx.getBackgroundAudioManager()
var that =this
       backgroundAudioManager.src = that.data.backauto
       console.log('播放音乐', that.data.backauto)

在PC端开发工具自带模拟器上能正常播放音乐,调试器能看到console.log('播放音乐', that.data.backauto)记录的日志。到到了真机上,播放音乐时提示通过如下代码:

backgroundAudioManager.onError((res)=>{
   console.log(res)
})

反馈的结果:

errCode:10001
errMsg:"errCode:500,err:error URL format"
src:"wxfile://tmp_a9ilkasiuoqkwerqi9087934795234985234jqjrkjkdfa.mp3"


之前使用playVoice接口和playBackgroundAudio接口真机上甚至没有反应,不执行接口代码。

并且我录音完成后播放录音的内容,以上提到的接口在真机上都没有反应,无法播放录音文件。


测试真机:vivo X7 Plus,微信目前最新版本6.5.16,基础库版本1.6.0。


能不能文档详细点,不要那么多坑啊?具体有什么解决方案吗?


网友回复

灿:

求官方回答。不要沉默了

周家齐:

我和你一样的需求,一样的代码但是也是无法实现,我的手机型号是小米6,。

A周伟杰:

希望官方技术能给个方案。

以上代码在开发工具完美执行,没有任何错误。到了真机上就不行行了。同时我也发现开发工具上执行和真机上执行的不同点。

开发工具上,downloadFile接口下载的mp3文件到本地,创建临时文件路径,后缀是mpeg,如下:

http://tmp/wx21b0454e406c6b1b.o6zAJs9LOTfVyToZ8qOdQql2dV-E.351c9423d15078401d16f71e468003c1.mpeg

getBackgroundAudioManager接口能播放,但到了真机上后缀变成了mp3,就不能播放。

提示"errCode:500,err:error URL format"错误。

然后录音接口不论开发工具还是真机,录音完成后回调的临时文件路径后缀都是mp3,开发工具能正常播放录音文件,手机上就不行,提示"errCode:500,err:error URL format"错误。

A周伟杰:

完成代码如下:

js

//index.js
//获取应用实例
const app = getApp()
const recorderManager = wx.getRecorderManager()
const backgroundAudioManager = wx.getBackgroundAudioManager()
const innerAudioContext = wx.createInnerAudioContext()
const options = {
    duration: 600000,
    sampleRate: 44100,
    numberOfChannels: 1,
    encodeBitRate: 192000,
    format: 'mp3'
}
recorderManager.onStart(() => {
    console.log('recorder start')
})
recorderManager.onStop((res) => {
    console.log('recorder stop', res)
    backgroundAudioManager.stop()
    innerAudioContext.src = res.tempFilePath
    innerAudioContext.play()
})
backgroundAudioManager.onPlay(()=>{
    console.log('播放音乐')
})
backgroundAudioManager.onError((res)=>{
    console.log(res)
})
innerAudioContext.onError((res) => {
    console.log(res)
})
innerAudioContext.onPlay(() => {
    console.log('播放路in个音乐')
})
Page({
    data: {
        autoname:"测试录音合成背景音乐",
        backauto:'',
        backautoText:''
    },
    //事件处理函数
    default1:function(){
        var that = this
        backgroundAudioManager.src = that.data.backauto
        //开始录音 
        recorderManager.start(options);
    },
    default2:function(){
        //结束录音 
        recorderManager.stop()
        backgroundAudioManager.stop()
    },
    default3:function(){
        var that =this
        backgroundAudioManager.src = that.data.backauto
        console.log('播放音乐', that.data.backauto)
        // wx.playBackgroundAudio({
        //     dataUrl: that.data.backauto,
        //     success: function () {
        //         console.log('播放音乐', that.data.backauto)
        //     }
        // })
    },
    default4: function () {
        backgroundAudioManager.stop()
    },
    onLoad: function () {
        var that = this
        if (wx.createInnerAudioContext && wx.getRecorderManager() && wx.getBackgroundAudioManager()) {
            wx.showModal({
                title: '提示',
                content: '支持目前最新播放录音组件'
            })
        }else{
            //如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
            })
        }
        wx.downloadFile({
            url: 'http://autotest.car580.com/blackmusic.mp3',
            success: function (res) {
                console.log('背景音乐下载完成', res)
                that.setData({
                    backauto: res.tempFilePath,
                    backautoText: "背景音乐下载完成"
                })
            }
        })
    }
})

html

<!--index.wxml-->
<view class="container">
<view class="section">
<view class="section__title">{{backautoText}}</view>
<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="default1" hover-class="other-button-hover" style="margin-bottom:15px;"> 开始录音 </button>
 
<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="default2" hover-class="other-button-hover"> 暂停录音 </button>
 
<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="default3" hover-class="other-button-hover"> 播放音乐 </button>
<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
disabled="{{disabled}}" bindtap="default4" hover-class="other-button-hover"> 停止播放 </button>
</view>
</view>


评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付