sound.js 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function MusicObject(url,flag){
  2. this.mobj=new Audio();
  3. this.mobj.src=url;
  4. this.enabled=true;
  5. if(flag){
  6. this.mobj.loop=flag;
  7. this.mobj.addEventListener("ended",function(){
  8. this.currentTime=0;
  9. this.play();
  10. });
  11. }
  12. }
  13. MusicObject.prototype.setEabled=function(flag){
  14. this.enabled=flag;
  15. try{
  16. if(this.mobj.isplayed){
  17. this.mobj.pause();
  18. }
  19. }catch(e){
  20. console.error("音乐停止播放错误!");
  21. }
  22. }
  23. MusicObject.prototype.play=function(){
  24. try{
  25. this.mobj.play();
  26. }catch(e){
  27. console.error("音乐播放错误!");
  28. }
  29. }
  30. MusicObject.prototype.pause=function(){
  31. try{
  32. this.mobj.pause();
  33. }catch(e){
  34. console.error("音乐停止播放错误!");
  35. }
  36. }
  37. MusicObject.prototype.replay=function(){
  38. try{
  39. if(this.mobj.isplayed){
  40. this.mobj.currentTime=0;
  41. }
  42. this.mobj.play();
  43. }catch(e){
  44. console.error("音乐重复播放错误!");
  45. this.mobj.play();
  46. }
  47. }