实时获取经纬度接口和音频接口相结合,如果在某点附近时就播放那个点的音频,但是如果两个点过近(小于20m)的话,由于经纬度是实时监听的,
就会导致最近的距离点每次都不一样,这样就会导致多次获取音频路径,造成了多个音频同时播放的问题,但是页面中只定义了一个全局音频管理器,
音频路径每次也只会获取到一个,为什么会出现多个音频同时播放的问题呢?
_locationChangeFn = function(res) {
let {latitude, longitude} = res;
self.curlatitude = latitude.toFixed(6);
self.curlongitude = longitude.toFixed(6);
console.log("实时经纬度", self.latlng);
let markersArray = allSpotArr;
markersArray.forEach((item, index)=>{
let DISTANCE = Location.distance(self.curlatitude, self.curlongitude, Number(markersArray[index].lat), Number(markersArray[index].lng));
self.distanceKm[index] = DISTANCE;
})
let distanceKmArray = self.distanceKm;
let CURRENTDISTANCEINDEX = -1;
let distanceSpotArr = [];
let distanceSpotArrs = [];
distanceKmArray.forEach((item, index) => {
if( Number(item) <= 0.03){
let obj = {
index: index,
distance: item
}
distanceSpotArrs.push(obj)
distanceSpotArr.push(Number(item))
}
});
console.log("最小距离数组", distanceSpotArrs);
if(distanceSpotArr.length > 1){
var min = Math.min(...distanceSpotArr);
console.log("最小距离", min);
let minIndex = distanceSpotArr.findIndex((item, index) => {
return item == min;
});
CURRENTDISTANCEINDEX = distanceSpotArrs[minIndex].index;
}else if(distanceSpotArr.length == 1){
CURRENTDISTANCEINDEX = distanceSpotArrs[0].index
console.log("距离的数组", distanceSpotArrs);
}else{
CURRENTDISTANCEINDEX = -1;
}
if(CURRENTDISTANCEINDEX != -1){
let narrateSrc;
if(self.lang == 'cn'){
narrateSrc = self.is_man ? (self.$imgsrc + markersArray[CURRENTDISTANCEINDEX].mp3_cn_boy) : (self.$imgsrc + markersArray[CURRENTDISTANCEINDEX].mp3);
}else{
narrateSrc = self.$imgsrc + markersArray[CURRENTDISTANCEINDEX].mp3;
}
if(!audiosrc){
audiosrc = narrateSrc;
innerAudioContext.src = audiosrc;
innerAudioContext.autoplay = true;
self.isLatlngAudioPlay = true;
}else{
if(audiosrc != narrateSrc){
audiosrc = narrateSrc;
console.log("音频不相等时播放");
innerAudioContext.src = audiosrc;
innerAudioContext.play();
self.isLatlngAudioPlay = true;
}else{
if(self.isLatlngAudioPlay){
console.log("正在播放不做操作");
}else{
console.log("暂停时播放");
audiosrc = narrateSrc;
innerAudioContext.src = audiosrc;
innerAudioContext.play();
self.isLatlngAudioPlay = true;
}
}
}
console.log("音频地址", narrateSrc, markersArray[CURRENTDISTANCEINDEX]);
}