如何停止播放视频文件中的音频



此代码播放Youtube视频。该文件使用FlashBuilder中的"调试"或"运行"按钮工作,但在从web服务器或"导出发布"文件夹播放时出现错误:当第二个视频运行时,第一个视频中的音频将继续播放。如何卸载第一个音频?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
    <![CDATA[
        import mx.controls.SWFLoader;
        private var swfLoader:SWFLoader;
        [Bindable] public var  videoToPlay:String
        public function playVideo(videoNum:int):void
        {
            if(swfLoader)
            {
                swfLoader.autoLoad = false;
                swfLoader.unloadAndStop();
                hg.removeChild(swfLoader);             
                SoundMixer.stopAll();
            }
            var videoAddress:String 
            if(videoNum == 1){
                videoAddress = "m2dg6teC7fg";
            }else{
                videoAddress = "0QRO3gKj3qw";               
            }
            videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;    
            play();
        }               
        public function play():void
        {
            swfLoader = new SWFLoader();
            swfLoader.y = 100;
            swfLoader.load(videoToPlay);       
            hg.addChild(swfLoader);
        }
    ]]>
</mx:Script>
<mx:HBox id="hg">
    <mx:Button id="button1"  y="50"  label="Button1"  click="playVideo(1)" useHandCursor="true" buttonMode="true" />
    <mx:Button id="button2"  y="50"  label="Button2"  click="playVideo(2)" useHandCursor="true" buttonMode="true" />        
</mx:HBox>
</mx:Application>

在第二次点击按钮时停止第一个视频/音频的解决方案是:

使用以下代码从swfloader卸载第一个视频:

<fx:Script>
        <![CDATA[
            import mx.controls.SWFLoader;
        private var swfLoader:SWFLoader;
        [Bindable] public var  videoToPlay:String
        public function playVideo(videoNum:int):void
        {
            if(swfLoader)
            {
                swfLoader.autoLoad = false;
                swfLoader.unloadAndStop();
//              swfLoader.autoLoad = true; 
                hg.removeElement(swfLoader);             
                SoundMixer.stopAll();
            }
            var videoAddress:String 
            if(videoNum == 1){
            videoAddress = "m2dg6teC7fg";
            }else{
            videoAddress = "0QRO3gKj3qw";               
            }
            videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;    
            play();
        }               
        public function play():void
        {
            swfLoader = new SWFLoader();
            swfLoader.y = 100;
            swfLoader.load(videoToPlay);       
            hg.addElement(swfLoader);
        }
        ]]>
    </fx:Script>

<s:HGroup id="hg">
    <s:Button id="button1"  y="50"  label="Button1"  click="playVideo(1)" useHandCursor="true" buttonMode="true" />
    <s:Button id="button2"  y="50"  label="Button2"  click="playVideo(2)" useHandCursor="true" buttonMode="true" />        
</s:HGroup>

我编辑了下面的代码,它适用于您的问题(我自己测试过)。试试看,让我知道它有效吗?

编辑:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
    <![CDATA[
        import mx.controls.SWFLoader;
        private var swfLoader:SWFLoader;
        [Bindable] public var  videoToPlay:String
        public function playVideo(videoNum:int):void
        {
            if(swfLoader)
            {
                swfLoader.source = "";
                swfLoader.load(null);
                SoundMixer.stopAll();
            }
            var videoAddress:String 
            if(videoNum == 1){
                videoAddress = "m2dg6teC7fg";
            }else{
                videoAddress = "0QRO3gKj3qw";               
            }
            videoToPlay = "https://www.youtube.com/v/VIDEO_ID?v=" + videoAddress;    
            play();
        }                   
        public function play():void
        {
            swfLoader = new SWFLoader();
            swfLoader.y = 100;
            swfLoader.load(videoToPlay);       
            hg.addChild(swfLoader);
        }
        private function onRemove():void
        {
        }
    ]]>
</mx:Script>
<mx:HBox id="hg">
    <mx:Button id="button1"  y="50"  label="Button1"  click="playVideo(1)" useHandCursor="true" buttonMode="true" />
    <mx:Button id="button2"  y="50"  label="Button2"  click="playVideo(2)" useHandCursor="true" buttonMode="true" />        
</mx:HBox>
</mx:Application>

相关内容

  • 没有找到相关文章