0

我目前正在尝试编写一个能够从多个来源流式传输的应用程序,我将把它作为一个列表。我希望用户单击列表中的一个选项,然后单击“播放”按钮,然后开始流式传输。我的代码现在正在运行,但是我已经明确说明了我想要播放的流,我还没有找到一种方法来动态地将 url 传递给 req new URL 请求。代码如下>

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Stream Southeast Ak Stations" destructionPolicy="never">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script
    >
    <![CDATA[
        private var req:URLRequest;
        private var context:SoundLoaderContext = new SoundLoaderContext(8000);
        private var s:Sound;
        private var channel:SoundChannel = new SoundChannel();
        private var playingBln = new Boolean


        private function playAudio():void
        {
            if (playingBln == true)
            {playingBln = false;
                channel.stop();
                s.close()
                req = new URLRequest('http://stream.ktoo.org:8000/ktoo');
                s = new Sound(req, context);
                channel = s.play();
                playingBln = true

            }
            else{
                req = new URLRequest('http://stream.ktoo.org:8000/ktoo');
                s = new Sound(req, context);
                channel = s.play();
                playingBln = true
            }
        }
        private function stopAudio():void
        {
            if (playingBln == true)
                channel.stop();
            s.close()
            playingBln = false;
        }           
    ]]>
</fx:Script>



<s:Label text="Choose a Station"/>
<s:List id="stationList" 
        width="100%" 
        height="100%"
        labelField="name"
        >

<s:ArrayCollection>
<fx:Object name="KTOO" location="Juneau" streamURL="http://stream.ktoo.org:800/ktoo"/>
<fx:Object name="KXLL" location="Juneau" streamURL="http://stream.ktoo.org:800/kxll"/>
<fx:Object name="KRNN" location="Juneau" streamURL="http://stream.ktoo.org:800/krnn"/>
<fx:Object name="KFSK" location="Petersburg" streamURL="null"/>
<fx:Object name="KSTK" location="Wrangell" streamURL="null"/>
<fx:Object name="KCAW" location="Sitka" streamURL="null"/>
<fx:Object name="KRBD" location="Ketchikan" streamURL="null"/>
</s:ArrayCollection>

</s:List>
<s:Button x="10" y="309" width="100" height="42" label="Play" click="playAudio()"/>
<s:Button x="139" y="310" width="99" label="Stop" click="stopAudio()"/>



</s:View>
4

1 回答 1

0

使用 stationList.selectedItem,或者像这样:

   private function playAudio():void
    {
        if (playingBln == true)
        {playingBln = false;
            channel.stop();
            s.close()
        }
        var streamURL :String = stationList.selectedItem.streamURL; 
        req = new URLRequest(streamURL);
        s = new Sound(req, context);
        channel = s.play();
        playingBln = true
    }

我还调整了您的算法,因此您没有两个单独的相同代码段。

于 2013-05-12T12:58:55.383 回答