加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Flex4做一个mp3 播放器

发布时间:2020-12-15 04:04:57 所属栏目:百科 来源:网络整理
导读:http://www.cnblogs.com/modou/articles/1897088.html 1.先来做一个最简单的MP3播放功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ?xml version= "1.0" ? encoding= "utf-8" ? s:Application x

http://www.cnblogs.com/modou/articles/1897088.html

1.先来做一个最简单的MP3播放功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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"? minWidth= "955"? minHeight= "600" >
???? <s:layout>
???????? <s:BasicLayout/>
</s:layout>
?
<fx:Script>
<![CDATA[
?????????????
???????????? private? var? snd:Sound;
channel:SoundChannel;
?????????????
protected? function? button1_clickHandler(event:MouseEvent): void
???????????? {
???????????????? snd = new? Sound( URLRequest( "http://localhost/flex/1.mp3" ));
channel = snd.play();
}
?
button3_clickHandler(event:MouseEvent): void
{
channel.stop();
}
?
]]>
</fx:Script>
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:BorderContainer x= "127"? y= "170"? width= "273"? height= "43" >
<s:Button x= "47"? "10"? label = "播放"? click= "button1_clickHandler(event)" />
"151"? "停止"? "button3_clickHandler(event)" />
</s:BorderContainer>
</s:Application>

?

2.在上面例子的基础上,做一些改进,增加对进度,以及音量的控制

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import? mx.controls.Alert;
mx.events.FlexEvent;
?????????????
snd:Sound;
channel:SoundChannel;
?????????????
void
{
));
channel = snd.play();
}
?
void
{
channel.stop();
}
?
hslider1_changeEndHandler(event:FlexEvent): void
{
channel.stop();
channel = snd.play(hslider1.value/hslider1.maximum * snd.length);
}
?????????????
hslider2_changeEndHandler(event:FlexEvent): void
{
???????????????? trans:SoundTransform = SoundTransform(hslider2.value/hslider2.maximum, 0 );
channel.soundTransform = trans;
}
?
]]>
</fx:Script>
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Label x= "126"? text= "进度:"? "45" />
<s:HSlider id= "hslider1"? x= "179"? "221"? changeEnd= "hslider1_changeEndHandler(event)"? maximum= "100"? showDataTip= "false" />
"146"? "音量:"? />
"hslider2"? "180"? "150"? "hslider2_changeEndHandler(event)"? "10"? />
?????
>
/>
/>
</s:BorderContainer>?
3.更近一步完善MP3播放功能

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"600"? enterFrame= "application1_enterFrameHandler(event)" trans:SoundTransform;
playStatus: Number? = ;
playPosition: ;
?????????????
void
{
???????????????? // 标示当前播放状态,0是未加载,1是播放,2是暂停
???????????????? if (playStatus== 0? || playStatus== 1 )
???????????????????? )
???????????????????? {
???????????????????????? ));
trans = SoundTransform();
trans.volume = hslider2.value/hslider2.maximum;
}
?????????????????????
playButton. label? = "暂停" ;
channel = snd.play(playPosition);
playStatus = 2 ;
}
else? )
{
"播放" ;
channel.stop();
;
}
}
void
{
channel.stop();
channel = snd.play(hslider1.value/hslider1.maximum * snd.length);
}
?????????????
void
{
trans.volume = hslider2.value/hslider2.maximum;
channel.soundTransform = trans;
}
?????????????
???????????? // 把毫秒格式化为时间
formatDate(num: Number ): String
{
total: int? = int (num / 1000 );
second: = total% 60 ;
total = (total-second)/ ;
minute: ;
total = (total-minute)/ ;
hour: = total;
?????????????????
returnValue: String? "" ;
(hour!= ) returnValue = String (hour) + ":" ;
(minute< 10 ) returnValue += "0" ;
returnValue += (minute) + ;
(second< ;
(second);
return? returnValue;
}
?
?
// 把播放进度绑定到播放时间的标签上,以及调整进度的组件上
application1_enterFrameHandler(event:Event): void
{
timeLabel.text = formatDate(channel.position) + " / "? + formatDate(snd.length);
playPosition = channel.position;
hslider1.value = channel.position/snd.length * hslider1.maximum;
}
?
]]>
</fx:Script>
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
"131"? "147"? "369"? "108" >
"83"? />
"135"? "48"? />
"67"? />
"136"? "71"? value= "5"? />
<s:Button id= "playButton"? "12"? "53"? "button1_clickHandler(event)"? "55" />
<s:Label id= "timeLabel"? "139"? "00:00 / 00:00"? />
"播放进度:" />
</s:BorderContainer>?
这里针对上一个例子,做了如下修改:

1)可以显示当前MP3播放时间

2)用一个按钮控制MP3的播放、暂停状态

3) 把播放进度绑定到调整进度条组件上,随时更新进度条位置

做了这些改动,基本上算是一个MP3播放器了,虽然依然很简陋,毕竟只是一个学习的例子,就将就着吧,呵呵

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!