我正在播放m4a文件,这些文件要求我使用NetStream对象并希望使用SoundChannel对象控制音频(因为这是我知道将HSlider控件与音频文件同步的唯一方法)。我的问题是我无法将声道连接到音频源。事件处理程序button1_clickHandler甚至不会重新识别SoundChannel对象的存在。有没有办法用SoundChannel控制这种类型的音频文件?如果没有,我如何设置HSlider的位置 - 我尝试过使用HSlider.value,但不知怎的被覆盖。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="init()">
<mx:Script>
<![CDATA[
private var audioExample:AudioExample_M4A;
public function init():void{
audioExample = new AudioExample_M4A();
}
private function button1_clickHandler(event:MouseEvent):void{
if(audioExample.soundChannel){
audioExample.soundChannel.stop()
trace("Yes")
}else{
trace("No")
}
}
]]>
</mx:Script>
<mx:Button label="Play / Pause" click="button1_clickHandler(event)"/>
</mx:Application>
这是类文件AudioExample_M4A.as:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.media.SoundChannel;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
public class AudioExample_M4A extends Sprite {
public var soundChannel:SoundChannel= new SoundChannel();;
public var temp:String = new String()
public var audioURL:String = "badge.m4a";
private var connection:NetConnection;
public var stream:NetStream;
public function AudioExample_M4A() {
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.connect(null);
}
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.StreamNotFound":
break;
}
}
private function connectStream():void {
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.client={onMetaData:function(obj:Object):void{
trace("metadata: duration=" + obj.duration);
}
}
soundChannel = stream.play(audioURL) as SoundChannel;
}
}}
答案 0 :(得分:1)
您无法使用NetStream
对象控制SoundChannel
对象。这些是两种完全不同的播放媒体的方法,据我所知,它们没有共同的基础。
要允许滑块控制指针位置,您需要执行以下操作:
slider.position * slider.minimum/slider.maximum
,MX中不确定)NetStream.onMetaData
NetStream.seek()
乘以持续时间(seek()
需要以秒为单位的值)。所以NetStream.seek( duration * positionPercentage )