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

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

8.3.Mp3文件的播放和暫停

2024-04-27 13:52:02
字體:
供稿:網(wǎng)友
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 屬性指示聲音文件現(xiàn)在播放的位置。當聲音播放停止是紀錄時間點的值:
+展開
-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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武功县| 赣榆县| 东乌珠穆沁旗| 溧水县| 石门县| 青神县| 余干县| 嘉峪关市| 罗甸县| 启东市| 东阿县| 衡东县| 天津市| 湘阴县| 永修县| 甘肃省| 邢台县| 屏山县| 巢湖市| 图片| 廊坊市| 揭阳市| 琼海市| 达拉特旗| 客服| 永仁县| 阳高县| 磐安县| 赤水市| 锦州市| 当阳市| 信阳市| 璧山县| 积石山| 麦盖提县| 广南县| 巴马| 东山县| 冀州市| 黑河市| 广水市|