7.5.14 简单实例
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" fontSize="12" initialize="init();" backgroundAlpha="0.6" backgroundColor="#FFFFFF">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.MetadataEvent;
import mx.events.SliderEvent;
import mx.controls.sliderClasses.Slider;
import mx.events.VideoEvent;
import mx.utils.ObjectUtil;
import mx.controls.ProgressBarDirection;
[Embed(source="images/play.png")]
[Bindable]
private var playClass:Class; //播放图标样式
[Embed(source="images/pause.png")]
[Bindable]
private var pauseClass:Class; //暂停图标样式
[Embed(source="images/sound1.png")]
[Bindable]
private var sound1:Class; //声音样式1
[Embed(source="images/sound.png")]
[Bindable]
private var sound:Class; //声音样式2(静音)
[Bindable]
private var videoSource:String; //媒体路径
private var isPause:Boolean = false; //暂停状态
private var isSound:Boolean = true; //声音状态
private var isFullScreen:Boolean = false; //是否是全屏
private var tmpSound:Number = 0; //临时声音大小
[Bindable]
private var fileSize:String; //视频文件大小
[Bindable]
private var playPosition:Number; //播放进度
private function init():void{
videoSource = parameters.videosource;
}
private function playingMove(event:VideoEvent):void{
my_hs.value = myVid.playheadTime; //视频播放时同步进度条状态值
}
//拖动进度条时改变播放位置
private function hs_onchange(event:SliderEvent):void{
if(myVid.playheadTime == -1){
my_hs.value = 0;
return;
}
playPosition = my_hs.value; //保正播放进度統一
myVid.playheadTime = playPosition;
}
//进度条鼠标按下
private function thumbPress():void{
myVid.pause();
}
//进度条鼠标弹起
private function thumbRelease():void{
myVid.playheadTime = playPosition;
myVid.play();
}
//播放,暂停
private function playButton():void{
if(!isPause){
myVid.play();
playBtn.source = pauseClass;
isPause = true;
}else{
myVid.pause();
playBtn.source = playClass;
isPause = false;
}
}
//播放完成
private function vidComplete():void{
playBtn.source = playClass;
isPause = false;
}
//停止播放
private function stopButton():void{
myVid.stop();
playBtn.source = playClass;
isPause = false;
}
//切換全屏顯示
private function display():void{
if(!isFullScreen){
stage.fullScreenSourceRect = new Rectangle(myVid.x,myVid.y,myVid.width,myVid.height);
stage.displayState =StageDisplayState.FULL_SCREEN;
isFullScreen = true;
}else{
stage.displayState = StageDisplayState.NORMAL;
isFullScreen = false;
}
}
//调整声音
private function sound_thumbChanges(event:SliderEvent):void{
myVid.volume = hs_sound.value;
}
//静音
private function closeSound():void{
if(isSound){
closeImg.source = sound;
tmpSound = myVid.volume;
myVid.volume = 0;
isSound = false;
}else{
closeImg.source = sound1;
myVid.volume = tmpSound;
isSound = true;
}
}
//格式化时间
private function formatTime(time:Number):String{
var min:Number = Math.floor(time/60);
var sec:Number = Math.floor(time%60);
var timeResult:String = (min < 10 ? "0"+min.toString() : min.toString()) + ":" + (sec == 10 ? "0"+sec.toString() : sec.toString());
return timeResult;
}
//slider格式化
private function dataTipFormat(data:Number):String{
return formatTime(data);
}
private function myVid_metadataReceived(evt:MetadataEvent):void {
var meta:Object = evt.info; // 视频的元数据信息
fileSize = (meta.filesize/(1024*1024)).toFixed(2).toString()+"MB";//换算成兆字节并保留两位小数
/* 读取所有元数据属性和值
var i:int = 0;
var arr:Array = [];
var item:String;
var value:*;
for (item in meta) {
if (ObjectUtil.isSimple(meta[item])) {
if (meta[item] is Array) {
value = "[Array]";
} else {
value = meta[item]
}
Alert.show(item+":"+value);
}
}*/
}
]]>
</fx:Script>
<s:BorderContainer borderColor="#66ccff" y="0" cornerRadius="0" borderWeight="0" borderVisible="true" dropShadowVisible="false" x="0" width="660">
<mx:VideoDisplay id="myVid" y="10" height="400" width="640" source="{videoSource}" autoPlay="false" buttonMode="true" click="playButton();" ready="myVid.visible = true;" metadataReceived="myVid_metadataReceived(event);"
playheadUpdate="playingMove(event)" complete="vidComplete();" doubleClickEnabled="true" doubleClick="display();" contentBackgroundAlpha="1.0" contentBackgroundColor="#000000" x="10"
/>
<mx:HBox width="640" verticalAlign="middle" x="10" y="415" height="90">
<mx:Image source="{playClass}" click="playButton();" id="playBtn" buttonMode="true"/>
<mx:Label text="{formatTime(myVid.playheadTime)}/{formatTime(myVid.totalTime)} {fileSize}" width="150"/>
<mx:HRule height="0" width="200" buttonMode="true"/>
<mx:Image source="{sound1}" click="closeSound();" id="closeImg" buttonMode="true"/>
<mx:HSlider width="100" id="hs_sound" minimum="0" maximum="1"
change="sound_thumbChanges(event)"
value="{myVid.volume}" buttonMode="true" />
<mx:Button label="全屏" click="display();" buttonMode="true" cornerRadius="20" labelPlacement="right" paddingLeft="6"/>
</mx:HBox>
<mx:HSlider width="640" id="my_hs" minimum="0" maximum="{myVid.totalTime}" height="10" showTrackHighlight="true" buttonMode="true" liveDragging="true"
change="hs_onchange(event)" thumbPress="thumbPress();" thumbRelease="thumbRelease();" x="10" y="422" dataTipFormatFunction="dataTipFormat" />
</s:BorderContainer>
</s:Application>