Flex控制声音播放以及SWF播放

时间:2021-06-03 18:50:32
我的flex里想点击tree后,相应的播放文件从指定的时间点播放以及swf(flashpaper)跳转到相应的页码。谢谢大家帮帮我!代码如下
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplete="initApp();">
<mx:Script>
  <![CDATA[
    import mx.controls.List;
    import mx.events.TreeEvent;
    import mx.events.ListEvent;
    import mx.controls.Tree;
    import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.media.SoundMixer;
import mx.events.SliderEvent;
import mx.controls.SWFLoader;
import flash.net.URLRequest;
//
private var sound:Sound;
private var time:Number=0;
private var soundChannel:SoundChannel;
private var myswf1:SWFLoader;
private var position:Number = 0;
private var isPlaying:Boolean = false;

    public var selectedNode:XML , theParentItem:Object,tt:Tree;
    
    private function initApp():void
    {
      
    }
    public function treeChanged(event:Event):void
    {
      selectedNode=Tree(event.target).selectedItem as XML;  
                btn_play.enabled = false;
btn_pause.enabled = false;
//

slider_volume.value = 1;
//
//var url:URLRequest = new URLRequest("weald.mp3");
var url:URLRequest = new URLRequest(selectedNode.@mp3);
sound = new Sound();
sound.addEventListener(Event.COMPLETE, completeHandler);
//sound.addEventListener(Event.SOUND_COMPLETE,mp3EndHandler);
sound.load(url);
//
slider_volume.addEventListener(SliderEvent.CHANGE,changeVolume);

//slider_time.addEventListener(SliderEvent.CHANGE,changetime);
btn_play.addEventListener(MouseEvent.CLICK,playSound);
btn_pause.addEventListener(MouseEvent.CLICK,pauseSound);   
time=selectedNode.@Time;
sound.play(int(time),1);
myswf1.setCurrentPage(selectedNode.@paperPageNum);
    }
    internal function clickItem():void
    {
        time=parseFloat(selectedNode.@Time);
sound.play(time,1);
  
    }
    
    internal function changeVolume(evt:SliderEvent):void{
var soundControl:SoundTransform = soundChannel.soundTransform;
soundControl.volume = evt.value;
soundChannel.soundTransform = soundControl;
}


internal function completeHandler(evt:Event):void{
btn_play.enabled = true;
btn_pause.enabled = true;
}

internal function playSound(evt:MouseEvent):void{
soundChannel = sound.play();
isPlaying = true;
position = 0;

}

internal function pauseSound(evt:MouseEvent):void{

if(isPlaying){
position = soundChannel.position;
soundChannel.stop();

}else{
soundChannel = sound.play(position);

}
isPlaying = !isPlaying;
}
    internal function viewPraventItem(e:Event):void
    {  
      //获取选中目录    
      var treeAddress:String=selectedNode.@label;
      var str:String;
      //把theParentItem初始化为当前选中节点,提供给后面的getParentItem方法调用
      theParentItem=myTree.selectedItem;
      //把树传回来的时间转换成Tree类型,这样才能调用getParentItem方法;
           tt=Tree(e.target);
        while(theParentItem!=null)
        {
                
          theParentItem = tt.getParentItem(theParentItem);
          
                if(theParentItem!=null)
                {
                str=theParentItem.@label+"/"+treeAddress;
                treeAddress=str;
                }
                      
        }
        res_txt.text="当前选中的目录地址是:\n"+treeAddress;
    }

  ]]>
</mx:Script>
<!-- 这里定义树的结构和内容  -->
<mx:XMLList id="files" >
  <folder label="解剖学:">
     <folder label="第一章" >
        <folder label="第一节" >
          <folder label="第一讲"  SwfPath="swfp/1.swf" mp3="weald.mp3" paperPageNum="3"/>
          <folder label="第二讲"  SwfPath="swfp/2.swf" mp3="sound.mp3" paperPageNum="4"/>
        </folder>
          
     </folder>
 
    <folder label="第二章" Time="800"/>
    <folder label="第三章" Time="3000"/>
  
  </folder>
    
</mx:XMLList>

<mx:Tree id="myTree" width="151" height="434" labelField="@label" itemRenderer="nodeItem" showRoot="true"
dataProvider="{files}" itemClick="viewPraventItem(event);" 
  change="treeChanged(event)" x="10" y="0"/>
  
<mx:TextArea id="res_txt" x="169" y="4" height="50"  width="552"/> 
<mx:SWFLoader width="552" height="488" source="{myTree.selectedItem.@SwfPath}" id="myswf1" autoLoad="true" scaleContent="true" x="169" y="62"/>
<mx:Canvas x="9" y="441" width="152.42424" height="109.01515">
    <mx:HSlider id="slider_volume" x="10.85" y="37.65" width="53" minimum="0" maximum="1" liveDragging="true"/>
<mx:Button x="10" y="85" label="播放" id="btn_play" width="53.863636" height="24.015152"/>
<mx:Button x="73" y="85" label="暂停" id="btn_pause" width="53.863636" height="24.015152"/>
<mx:Label x="66" y="39" text="音量" color="#EDF5F7"/>
</mx:Canvas>
  
</mx:Application>

6 个解决方案

#1


wo shi新手,请大家多多帮助,我的flex里想点击tree后,相应的播放文件从指定的时间点播放以及swf(flashpaper)跳转到相应的页码。打算用,Hsilder组件,但我还是实现不了控制mp3的播放进度,
internal function changetime(evt:SliderEvent):void{
//sound.position = evt.value;
sound.play(evt.value); }

还有as3.0里 paper.setCurrentPage(pageNum);//flashpaper会跳到相应页,可在flex里怎么实现呢。。。。

谢谢大家了!!!!!!!!!!!!!!!!

#2


怎么也没有人回答呀?我试了试你的程序,加载了一个新浪的swf文件,可是报错,不知道你试了没有啊?
TypeError: Error #1009: 无法访问空对象引用的属性或方法。
at com.sina.outPlayer::MainView/addStageListener()
at com.sina.outPlayer::MainView/init()
at com.sina.outPlayer::MainView()

#3


我也是新手
不知道帮顶。。。

#4


也正研究这功能,qq389183664

#5


唉,头疼。

#6


顶顶~

#1


wo shi新手,请大家多多帮助,我的flex里想点击tree后,相应的播放文件从指定的时间点播放以及swf(flashpaper)跳转到相应的页码。打算用,Hsilder组件,但我还是实现不了控制mp3的播放进度,
internal function changetime(evt:SliderEvent):void{
//sound.position = evt.value;
sound.play(evt.value); }

还有as3.0里 paper.setCurrentPage(pageNum);//flashpaper会跳到相应页,可在flex里怎么实现呢。。。。

谢谢大家了!!!!!!!!!!!!!!!!

#2


怎么也没有人回答呀?我试了试你的程序,加载了一个新浪的swf文件,可是报错,不知道你试了没有啊?
TypeError: Error #1009: 无法访问空对象引用的属性或方法。
at com.sina.outPlayer::MainView/addStageListener()
at com.sina.outPlayer::MainView/init()
at com.sina.outPlayer::MainView()

#3


我也是新手
不知道帮顶。。。

#4


也正研究这功能,qq389183664

#5


唉,头疼。

#6


顶顶~