国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

8.3.Mp3文件的播放和暫停

2024-04-27 13:52:02
字體:
來源:轉載
供稿:網友
8.3.1. 問題
我希望允許用戶播放一系列的MP3 文件。
8.3.2. 解決辦法
使用Sound 和SoundChannel 類,當用戶選擇一個新的MP3 類時,使用漸進式下載方式下在一個新的文件。
8.3.3. 討論
Sound 類的play 方法返回一個SoundChannel 對象,它提供存取的方法和屬性控制左右聲道聲音音量的平衡,還有暫停和恢復一個特定聲音的方法。

例子,你可以使用這樣的代碼來裝載和播放聲音文件:
+展開
-ActionScript
var snd:Sound = new Sound(new URLRequest("sound.mp3"));
var channel:SoundChannel = snd.play();

你不能用ActionScript 代碼暫停一個聲音的播放,你只能使用SoundChannel 的stop 方法停止播放。不過,您可以從任何一個時間點開始播放聲音。也可以紀錄聲音停止的時間點,并且從這個時間點開始播放后面的聲音。

當聲音播放時,SoundChannel.position 屬性指示聲音文件現在播放的位置。當聲音播放停止是紀錄時間點的值:
+展開
-ActionScript
var pausePosition:int = channel.position;
channel.stop();

需要恢復聲音時,傳遞前面紀錄的時間點值,聲音會從前面停止時的位置重新開始播放:
+展開
-ActionScript
channel = snd.play(pausePosition);

下面的完整代碼提供了一個組合框允許用戶選擇不同的MP3 文件,暫停和停止播放使用了SoundChannel 類:
+展開
-XML
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxmlwidth="400height="300">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var sound:Sound;
public var chan:SoundChannel;
public var pausePos:int = 0;
private const server:String = "http://localhost:3001/"
private var dp:ArrayCollection =
new ArrayCollection(["Plans.mp3""Knife.mp3",
"Marla.mp3""On a Neck, On a Spit.mp3",
"Colorado.mp3"])
private function loadSound():void {
if(chan != null) {
//make sure we stop the sound; otherwise, they'll overlap
chan.stop();
}
//re-create sound object, flushing the buffer, and readd the
event listener
sound = new Sound();
sound.addEventListener(Event.SOUND_COMPLETE,
soundComplete);
var req:URLRequest = new URLRequest(server +
cb.selectedItem as String);
sound.load(req);
pausePos = 0;
chan = sound.play();
}p
private function soundComplete(event:Event):void {
cb.selectedIndex++;
sound.load(new URLRequest(server +
cb.selectedItem as String));
chan = sound.play();
}
private function playPauseHandler():void
{
if(pausePlayBtn.selected){
pausePos = chan.position;
chan.stop();
else {
chan = sound.play(pausePos);
}
}

]]>
</mx:Script>
<mx:ComboBox creationComplete="cb.dataProvider=dpid="cb"
change="loadSound()"/>

<mx:Button label="startid="pausePlayBtntoggle="true"
click="playPauseHandler()"/>

<mx:Button label="stopclick="chan.stop()"/>
</mx:HBox>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 顺平县| 青阳县| 樟树市| 玉树县| 边坝县| 津市市| 安宁市| 乌拉特前旗| 江津市| 裕民县| 盈江县| 石家庄市| 荆州市| 盖州市| 红河县| 宁陕县| 宜章县| 灵石县| 水富县| 临颍县| 长治市| 蓝山县| 吴川市| 樟树市| 鄱阳县| 罗定市| 贵德县| 新巴尔虎左旗| 汕头市| 冷水江市| 孝感市| 城口县| 德州市| 泽普县| 汝州市| 巨鹿县| 青浦区| 舟山市| 太和县| 安丘市| 上高县|