<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="init();"> <s:layout> <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/> </s:layout> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import flash.events.Event; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundTransform; import flash.net.URLRequest; import flash.xml.*; private var _xmlDoc:String; private var _xmlObj:XML; private var _soundObj:Sound; private var _soundTransform:SoundTransform; private var _soundChannel:SoundChannel; private var _currentArrayNum:int; private var _currentSndPosition:Number; private var _isPlayingSound:Boolean; //helps you keep track of if the song is playing or not. private function init():void { _xmlDoc = '<?xml version="1.0" encoding="utf-8"?><mp3><songURL>I Need You Come Back.mp3</songURL>' + '<songURL>Boom Boom Pow.mp3</songURL></mp3>'; _xmlObj = new XML(_xmlDoc); trace(_xmlObj..songURL); trace(_xmlObj..songURL[0]); //if there were more "songURL" tags then you could treat this like like an array. the first song being in position 0 _currentArrayNum = 0; //this var helps keep track of where you are in the array when you click next or previous. _soundObj = new Sound(); _soundObj.load(new URLRequest(_xmlObj..songURL[_currentArrayNum])); _soundChannel = new SoundChannel(); _soundTransform = new SoundTransform(); VSlider.snapInterval =.1; VSlider.addEventListener(Event.CHANGE, ChangeVolume); } private function playSound():void { _soundChannel = _soundObj.play(); _isPlayingSound = true; _soundTransform.volume = .5; _soundChannel.soundTransform = _soundTransform; } private function pauseSound():void { if(_isPlayingSound == true){ //保存播放的点 _currentSndPosition = _soundChannel.position; trace("_currentSndPosition = " + _currentSndPosition); _isPlayingSound = false; _soundChannel.stop(); }else{ //从上次断点开始播放 _soundChannel = _soundObj.play(_currentSndPosition); _isPlayingSound = true; trace("_currentSndPosition = " + _currentSndPosition); } } //if your var's value is not yet the equal fo the highest number in your songURL array. then add 1 and load the next //the opposite is true going backwards private function nextSound():void { trace(_currentArrayNum+" "+_xmlObj..songURL.length()) ; if(_currentArrayNum != _xmlObj..songURL.length()){ _currentArrayNum ++; _soundObj.load(new URLRequest(_xmlObj.songURL[_currentArrayNum])); playSound(); } } private function prevSound():void { if(_currentArrayNum !=0){ _currentArrayNum --; _soundObj.load(new URLRequest(_xmlObj..songURL[_currentArrayNum])); playSound(); } } //the slider has a min value of 0 and a max value of 1 and will snap to each position in between (.1) //what ever that number is in between 0 and 1 is the new volume private function ChangeVolume(evt:Event):void{ _soundTransform.volume = evt.target.value; _soundChannel.soundTransform = _soundTransform; trace(evt.target.value); } ]]> </fx:Script> <s:Label text="MP3播放器" /> <s:HGroup> <s:Button label="Play" click="playSound();"/> <s:Button label="Pause" click="pauseSound();"/> <s:Button label="Next" click="nextSound();"/> <s:Button label="Previous" click="prevSound()"/> <s:HSlider id="VSlider" minimum="0" maximum="1" /> </s:HGroup> </s:Application>