PageRenderTime 77ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/files/audiojs/0.1/audiojs.as

https://gitlab.com/Mirros/jsdelivr
ActionScript | 146 lines | 117 code | 29 blank | 0 comment | 12 complexity | 2bad4d3c39d693a8d152be4ee587f356 MD5 | raw file
  1. package {
  2. import flash.display.Sprite;
  3. import flash.external.ExternalInterface;
  4. import flash.net.URLRequest;
  5. import flash.media.Sound;
  6. import flash.media.SoundChannel;
  7. import flash.media.SoundTransform;
  8. import flash.events.Event;
  9. import flash.errors.IOError;
  10. import flash.events.IOErrorEvent;
  11. import flash.events.ProgressEvent;
  12. import flash.events.TimerEvent;
  13. import flash.utils.Timer;
  14. import flash.system.Security;
  15. public class audiojs extends Sprite {
  16. private var _channel:SoundChannel;
  17. private var sound:Sound;
  18. private var duration:Number;
  19. private var playerInstance:String;
  20. private var pausePoint:Number = 0;
  21. private var playing:Boolean = false;
  22. private var volume:Number = 1;
  23. private var timer:Timer = new Timer(250, 0);
  24. private function get channel():SoundChannel {
  25. return this._channel;
  26. }
  27. private function set channel(channel:SoundChannel):void {
  28. this._channel = channel;
  29. this._channel.addEventListener(Event.SOUND_COMPLETE, this.soundEnded);
  30. }
  31. public function audiojs():void {
  32. Security.allowDomain("*");
  33. this.playerInstance = root.loaderInfo.parameters.playerInstance+'.';
  34. ExternalInterface.addCallback('init', init);
  35. ExternalInterface.addCallback('load', load);
  36. ExternalInterface.addCallback('playPause', playPause);
  37. ExternalInterface.addCallback('pplay', play);
  38. ExternalInterface.addCallback('ppause', pause);
  39. ExternalInterface.addCallback('skipTo', skipTo);
  40. ExternalInterface.addCallback('setVolume', setVolume);
  41. ExternalInterface.call(this.playerInstance+'loadStarted');
  42. }
  43. private function updatePlayhead(e:TimerEvent = null):void {
  44. var targetPosition:Number = e ? this.channel.position : this.pausePoint;
  45. var playProgress:Number = targetPosition / this.duration;
  46. if (playProgress > 1) playProgress = 1;
  47. if (playProgress > 0) {
  48. ExternalInterface.call(this.playerInstance+'updatePlayhead', playProgress);
  49. }
  50. }
  51. private function loadProgress(e:ProgressEvent):void {
  52. this.duration = (e.bytesTotal / (e.bytesLoaded / this.sound.length))
  53. var loadPercent:Number = e.bytesLoaded / e.bytesTotal;
  54. if (loadPercent > 1) loadPercent = 1;
  55. if (loadPercent > 0) {
  56. ExternalInterface.call(this.playerInstance+'loadProgress', loadPercent, (this.duration/1000));
  57. }
  58. }
  59. private function init(mp3:String):void {
  60. this.load(mp3);
  61. }
  62. private function load(mp3:String):void {
  63. if (this.channel) this.channel.stop();
  64. if (this.sound) this.sound.removeEventListener(ProgressEvent.PROGRESS, this.loadProgress);
  65. this.channel = new SoundChannel();
  66. this.sound = new Sound(new URLRequest(mp3));
  67. this.pausePoint = 0;
  68. this.sound.addEventListener(IOErrorEvent.IO_ERROR, this.loadError);
  69. this.sound.addEventListener(ProgressEvent.PROGRESS, this.loadProgress);
  70. this.timer.addEventListener(TimerEvent.TIMER, this.updatePlayhead);
  71. this.timer.start();
  72. }
  73. private function loadError(e:IOErrorEvent):void {
  74. ExternalInterface.call(this.playerInstance+'loadError');
  75. }
  76. private function play():void {
  77. this.channel = this.sound.play(this.pausePoint);
  78. this.setVolume(this.volume);
  79. this.playing = true;
  80. this.timer.start();
  81. }
  82. private function pause():void {
  83. this.pausePoint = this.channel.position;
  84. this.channel.stop();
  85. this.playing = false;
  86. this.timer.stop();
  87. }
  88. private function playPause():void {
  89. if (this.playing) {
  90. this.pause();
  91. } else {
  92. this.play();
  93. }
  94. }
  95. private function skipTo(percent:Number):void {
  96. this.channel.stop();
  97. this.pausePoint = this.duration * percent;
  98. if (this.playing) {
  99. this.channel = this.sound.play(this.pausePoint);
  100. this.setVolume(this.volume);
  101. } else {
  102. this.updatePlayhead();
  103. }
  104. }
  105. private function setVolume(vol:Number):void {
  106. this.volume = vol;
  107. var transform:SoundTransform = this.channel.soundTransform;
  108. if (vol < 0) vol = 0;
  109. if (vol > 1) vol = 1;
  110. transform.volume = vol;
  111. channel.soundTransform = transform;
  112. }
  113. private function soundEnded(e:Event):void {
  114. ExternalInterface.call(this.playerInstance+'trackEnded');
  115. }
  116. }
  117. }