package ru ammalgamma import flash geom Rectangle import flash events

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ru.ammalgamma {
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.media.SoundTransform;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.display.MovieClip;
/**
* @author line
*/
public class AudioPlaya extends MovieClip {
private var sound:Sound;
private static var LOADED_BAR_WIDTH:Number = 110;
private var soundChannel:SoundChannel;
private var soundTransForm : SoundTransform = new SoundTransform();
private var playing:Boolean = true;
private var playingPosition : int = 0;
private var soundLength:int;
private var songsArray: Array = new Array("collab.mp3");
private var currentSong: int = 0;
public function AudioPlaya() {
playBar.loadedBar.width = 1;
playBar.playPosition.x = -6;
playSound();
playPause.addEventListener(MouseEvent.MOUSE_OVER, playPause_mouseListener);
playPause.addEventListener(MouseEvent.MOUSE_OUT, playPause_mouseListener);
playPause.addEventListener(MouseEvent.MOUSE_UP, playPause_mouseListener);
playBar.playPosition.addEventListener(MouseEvent.MOUSE_DOWN, playPosition_mouseListener);
playBar.playPosition.addEventListener(MouseEvent.MOUSE_UP, playPosition_mouseListener);
playBar.playPosition.addEventListener(MouseEvent.MOUSE_OUT, playPosition_mouseListener);
}
private function playSound() {
sound = new Sound(new URLRequest(songsArray[currentSong]));
soundChannel = sound.play();
playPause.gotoAndStop(2);
soundTransForm.volume = .1;
soundChannel.soundTransform = soundTransForm;
soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
addEventListener(Event.ENTER_FRAME, enterFrameListener);
}
private function soundCompleteHandler(event:Event) {
playing = false;
playPause.gotoAndStop(1);
removeEventListener(Event.ENTER_FRAME, enterFrameListener);
playingPosition = 0;
}
private function playPosition_mouseListener(event:MouseEvent):void {
switch(event.type) {
case "mouseDown":
removeEventListener(Event.ENTER_FRAME, enterFrameListener);
soundChannel.stop();
var rectangle : Rectangle = new Rectangle(-6,0,LOADED_BAR_WIDTH,0);
playBar.playPosition.startDrag(false, rectangle);
break;
case "mouseUp":
addEventListener(Event.ENTER_FRAME, enterFrameListener);
playBar.playPosition.stopDrag();
playingPosition = playBar.playPosition.x/LOADED_BAR_WIDTH * soundLength;
soundChannel = sound.play(playingPosition);
break;
}
}
private function playPause_mouseListener(event : MouseEvent):void {
switch(event.type) {
case "mouseUp":
if(playing) {
playingPosition = soundChannel.position;
soundChannel.stop();
playPause.gotoAndStop(1);
}
else {
if (playingPosition == 0) {
playSound();
}
soundChannel = sound.play(playingPosition);
soundChannel.soundTransform = soundTransForm;
playPause.gotoAndStop(2);
}
playing = !playing;
}
}
private function enterFrameListener(event : Event) : void {
var loaded:int = sound.bytesLoaded;
var total:int = sound.bytesTotal;
soundLength = sound.length;
playingPosition = soundChannel.position;
if (loaded > 0) {
var percentBuffered:Number = loaded/total;
playBar.loadedBar.width = LOADED_BAR_WIDTH * (percentBuffered);
soundLength /= percentBuffered;
var percentPlayed:Number = playingPosition / soundLength;
playBar.playPosition.x = percentPlayed*LOADED_BAR_WIDTH;
if (playingPosition >= soundLength - 50) {
dispatchEvent(new Event(Event.SOUND_COMPLETE));
}
}
}
}
}