如何阻止音频播放视频文件?

时间:2015-02-17 20:57:13

标签: actionscript-3 youtube-api flash-builder

此代码播放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>

1 个答案:

答案 0 :(得分:0)

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

在代码中使用以下内容从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>