在地图之后调用音频API,真机无法播放声音,模拟器正常
发布于 5 年前 作者 xieping 18225 次浏览 来自 问答

微信小程序,在启动地图之前,播放声音手机和模拟器均正常,启动模拟器之后,模拟器正常,手机无声音

代码如下:

//index.js

//获取应用实例

const app = getApp()

var EARTH_RADIUS = 6378137.0; //单位M

var PI = Math.PI;

Page({

data: {

latitude: ‘’,

longtitude: ‘’,

curLatitude: ‘’,

curLongitude: ‘’,

precision: ‘30’,

exenum: ‘0’,

distance: ‘0’

},

onLoad: function () {

var that = this;

/////////////////////// 在地图被调用前,手机播放正常,模拟器正常

const innerAudioContext = wx.createInnerAudioContext()

innerAudioContext.autoplay = true

innerAudioContext.src = ‘res/Young.mp3’

innerAudioContext.onPlay(() => {

console.log(‘开始播放’)

})

innerAudioContext.onError((res) => {

console.log(res.errMsg)

console.log(res.errCode)

});

setTimeout(function () {

console.log(“before等待============”)

////////////////////////

if (that.data.precision > 0) {

wx.chooseLocation({  //调用地图

success: function (res) {

console.log(res);

wx.openLocation({

latitude: res.latitude,

longitude: res.longitude,

name: res.name,

address: res.address

});

that.setData(

{

//latitude: res.latitude,

//longitude: res.longitude

latitude: 39.928712,

longitude: 116.393345

}

);

console.log("destination latitude is " + " " + that.data.latitude);

console.log("destination longitude is " + " " + that.data.longitude);

wx.getLocation({

success: function (res) {

console.log("current latitude is " + " " + res.latitude);

console.log("current longitude is " + " " + res.longitude);

that.setData(

{

curLatitude: res.latitude,

curLongitude: res.longitude

});

var mydis = getFlatternDistance(that.data.curLatitude, that.data.curLongitude, that.data.latitude, that.data.longitude);

console.log(“distance” + " " + mydis);

// if (that.data.distance < 5000000) {

//start play, 这里再次播放音频,手机无声音,模拟器正常。

const innerAudioContext = wx.createInnerAudioContext()

innerAudioContext.autoplay = true

innerAudioContext.src = ‘res/Young.mp3’

innerAudioContext.onPlay(() => {

console.log(‘开始播放’)

})

innerAudioContext.onError((res) => {

console.log(res.errMsg)

console.log(res.errCode)

})

//end play

// };

//////////////////////////////////////////////////////////////////

function getRad(d) {

return d * PI / 180.0;

};

function getFlatternDistance(lat1, lng1, lat2, lng2) {

console.log(lat1, lng1, lat2, lng2);

var f = getRad((lat1 + lat2) / 2);

var g = getRad((lat1 - lat2) / 2);

var l = getRad((lng1 - lng2) / 2);

var sg = Math.sin(g);

var sl = Math.sin(l);

var sf = Math.sin(f);

var s, c, w, r, d, h1, h2;

var a = EARTH_RADIUS;

var fl = 1 / 298.257;

sg = sg * sg;

sl = sl * sl;

sf = sf * sf;

s = sg * (1 - sl) + (1 - sf) * sl;

c = (1 - sg) * (1 - sl) + sf * sl;

w = Math.atan(Math.sqrt(s / c));

r = Math.sqrt(s * c) / w;

d = 2 * w * a;

h1 = (3 * r - 1) / 2 / c;

h2 = (3 * r + 1) / 2 / s;

return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));

};

//////////////////////////////////////////////////////////////////

},

})

}

});

};

}, 5000);

},

})

回到顶部