在我的智能电视应用程序中,我正在使用SoundCloud API,通过跨域请求 - $.getJSON()
等。
该应用程序还在100%HTML5模式(无Flash)上使用SoundManager2 API从从SoundCloud检索到的.stream_url
加载曲目。尝试播放加载的曲目时出现问题。假设应用程序加载了 10 首曲目,其中一些将无法播放。
我注意到日志指出:
0040 sound0: play(): Loading - attempting to play...
0041 sound0: abort
0042 sound0: waiting
0043 sound0: loadstart
0044 sound0: stalled
它永远停留的地方。问题总是发生在相同的曲目上,而其他曲目是可流式传输和可播放的,显示日志:
0078 sound2: durationchange (191190)
0079 sound2: loadedmetadata
0080 sound2: stalled
0081 sound2: suspend
0082 sound2: loadeddata
0083 sound2: canplay
0084 sound2: onload()
0085 sound2: playing ♫
用于创建声音对象并播放它的代码:
SoundObject = soundManager.createSound({ // load the current track
url: "https://api.soundcloud.com/tracks/" + TrackIDs[elements][index-1] + "/stream?client_id=f430822ab78177a5cfb94d572d9036a2",
volume: 100,
autoLoad: true,
//autoPlay: true,
//stream: true,
whileplaying:function(){currentPosition=SoundObject.position;barWhilePlaying(elements, index, currentPosition);},
onfinish: function(){barOnFinish(elements, index);},
onstop: function(){barOnStop(elements, index);},
});
SoundObject.play();
(其中 whileplaying
、onfinish
和 onstop
参数都调用一个函数,该函数更改 html 文档中的 css 和/或元素类。
我的问题:
- 有没有办法事先知道 SMsound 对象不会播放?
- 假设我在加载它之前不知道它会永远"加载启动"和"停止",有没有办法知道加载后?
我问这个是因为某些曲目会在很长一段时间后加载和播放。
我可以建议一个可能会有所帮助的解决方法。
var mySound = soundManager.createSound({..});
mySound.load();
setTimeout(function() {
if (mySound.readyState == 1) {
// this object is probably stalled
}
},5500);
这是有效的,因为在 html5 中,与 flash 不同,readystate 属性几乎立即从"0"(未初始化 - 尚未加载)跳到"3"(加载),跳过"1"(初始化 - 开始加载),因为如果声音开始缓冲,它可以在 html5 中播放,因此 readystate 返回 3...
在 html5 上,永久(或长时间)停滞的声音将在 load() 尝试后很长时间内具有其就绪状态 = 1。
之后 - 您可以替换/删除/跳过 errouneous 轨道或其他任何东西。
- 注意 - 5500 毫秒应该足够长,你可以试试它,看看什么是最适合你的平台的超时。
希望这对你有帮助。