如何播放m4a声音文件

时间:2013-11-19 15:06:54

标签: actionscript-3 flash flex flash-builder

下面的代码导入并控制mp3声音文件。当我从mp3更改为m4a文件时,代码不起作用。我是否需要使用其他类来播放m4a文件?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"  creationComplete="init()" >
    <mx:Script>
        <![CDATA[    
            import mx.events.ItemClickEvent;
            import mx.rpc.events.ResultEvent;
            private var sound:Sound;
            private var soundChannel:SoundChannel = new SoundChannel;
            private var loaderContext:SoundLoaderContext;
            private var trackPosition:Number = 0;
            private var timer:Timer;
            private var leftGraphic:Sprite;
            private var rightGraphic:Sprite;
            private function init():void{
                var audioFile:String = "recording.mp3";             
                sound = new Sound(new URLRequest(audioFile));               
                sound.addEventListener(Event.COMPLETE, onSoundLoaded);          
                function onSoundLoaded(event:Event):void{
                    maxTime.text = convertMillesecs(sound.length)   
                    trackSlider.y-= 5;
                    trackSlider.maximum = sound.length;
                }
            }
            private var counter:Number
            private function soundComplete( e:Event ):void{
                maxTime.text = convertMillesecs(soundChannel.position);
                maxTime.text = convertMillesecs(sound.length)
            }
            private function checkTrack(event:Event):void{
                // manage track counter
                trackSlider.value = soundChannel.position;
                currentVal.text = String(convertMillesecs(soundChannel.position));

            }
            private function convertMillesecs(time:Number):String{
                var h:Number = new Number(Math.floor(time/1000/60/60));
                var m:Number = new Number(Math.floor(time/1000/60)-(h*60));
                var s:Number = new Number(Math.floor(time/1000)-(m*60));
                var hours:String;
                var minutes:String;
                var seconds:String
                //minutes and seconds always two digits
                if(m.toString().length == 1) {
                    minutes = "0"+m;
                } else {
                    minutes = m.toString();
                }
                if(s.toString().length == 1) {
                    seconds = "0"+s;
                } else {
                    seconds = s.toString();
                }
                // last two digits represent actual seconds
                seconds = seconds.slice(seconds.length-2, seconds.length);
                return minutes + ":" + seconds;
            }

            private function controlChange1(event:MouseEvent):void{
                var optionString:String;
                switch(event.target.id){
                    case "playButton":
                        play();
                        break;
                    case "pauseButton":
                        pause();
                        break;
                    default:
                        break;
                }
            }
            private function play():void{
                playButton.visible = false;
                pauseButton.visible = true;                 
                timer = new Timer(100);
                timer.addEventListener(TimerEvent.TIMER, checkTrack);
                soundChannel.stop();
                soundChannel = sound.play(pausePosition);
                timer.start();
            }
            private var pausePosition:int
            private function pause():void{
                pausePosition = soundChannel.position; 
                soundChannel.stop();
                playButton.visible = true;
                pauseButton.visible = false;                    
            }
            private function onTrackSliderChange(e:Event):void{
                soundChannel.stop()
                soundChannel = sound.play(e.target.value * sound.length / trackSlider.maximum);
                playButton.visible = false;
                pauseButton.visible = true;                 
            }
            private function formatButton(val:String):String{
                return convertMillesecs(soundChannel.position)
            }


        ]]>
    </mx:Script>
    <mx:HBox   backgroundColor="0x000000" horizontalCenter="0" verticalCenter="0" verticalAlign="middle" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
        <mx:Canvas  id="controlBar1"  paddingLeft="1"   buttonMode="true" useHandCursor="true" >
            <mx:Button id="pauseButton" width="40"  height="40"  click="controlChange1(event)"  visible="false" color="0x0B333C"/>
            <mx:Button id="playButton" width="40"  height="40"  click="controlChange1(event)" color="0x000000"    />
        </mx:Canvas>
        <mx:Label  id="currentVal" text="00:00" color="0xffffff"/>
        <mx:HSlider id="trackSlider" height="10" width="500" liveDragging="false"  change="onTrackSliderChange(event);"  
                        dataTipFormatFunction="formatButton" showTrackHighlight = "true"  mouseEnabled="true" useHandCursor="true" />
        <mx:Label id="maxTime" text="00:00" color="0xffffff"/>              
    </mx:HBox>  
</mx:Application>

1 个答案:

答案 0 :(得分:1)

Sound类可以播放以下两种格式之一:mp3和AS3记录的原始格式。因此,不幸的是,不可能使用Sound类播放m4a。

幸运的是, 可以使用另一个类播放m4a:NetStream。如果没有SoundTransformSoundMixer类,你可能会或可能没有对它进行控制,但是你仍然可以播放文件没问题(我过去做过,尽管它没有'最终工作的方式与我希望的方式完全相同,所以我最终选择了mp3而不是

Adobe Article on m4a playback