自娱自乐——flex4自制简易mp3播放器v1.0

刚开始学flex4没多久,练练手,做了个flex4的简易MP3播放器,自娱自乐而已呵呵。

希望大家多多指点一下。

感觉flex4与以前的版本相比变化较大,而且很多教程都还是flex3的,所以在制作的时候还是遇到了些麻烦的事情。

 

此程序为AIR工程,使用Flash Buidler 4 ,制作,新建项目时使用AIR项目。

 

文件结构:

music.swf   主运行文件

musiclist.xml   歌曲列表

music文件夹    存放歌曲的地方

 

在运行本程序的时候记得先编辑歌曲列表,再将MP3歌曲放在music文件夹

嘿嘿,此播放器如果不人工操作,只能单曲循环……

 

贴一下代码:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx"  width="206" height="227"
					   showStatusBar="false"  
					   applicationComplete="init_play()" backgroundColor="#82826b">

	<fx:Script>
		<![CDATA[
			import mx.events.ListEvent;
			
			
			private var goon:Boolean=true;
			private var musicS:Sound;
			private var musicSC:SoundChannel;
			private var url_t:String="music/";
			private var url_s:String="";
			private var temp_name:String="";
			private var pause:Boolean=false;
			private var time:Number;
			
			
			private function init_play():void{
				music_play.enabled=false;
				music_stop.enabled=false
			}
			
			private function musicS_play():void{
				
				if(goon){
					if(pause){
						musicSC=musicS.play(time);
						musicSC.soundTransform=new SoundTransform(volume.value/10);
						music_play.label="暂停";
						goon=false;
						pause=false;
					}else{
						musicS=new Sound(new URLRequest(url_s));
						musicSC=musicS.play();
						musicSC.addEventListener(Event.SOUND_COMPLETE,next_play);
						music_name.text=temp_name;
						musicSC.soundTransform=new SoundTransform(volume.value/10);
						music_play.label="暂停";
						music_stop.enabled=true;
						goon=false;
						pause=false;
					}
				}else{
					music_play.label="开始";
					goon=true;
					pause=true;
					time=musicSC.position;
					musicSC.stop();
				}
				
			}
			private function next_play(event:Event):void{
				goon=true;
				pause=true;
				time=0;
				musicS_play();
				musicSC.addEventListener(Event.SOUND_COMPLETE,next_play);
			}
			
			protected function music_play_clickHandler(event:MouseEvent):void
			{
				musicS_play();
			}

			protected function music_list_changeHandler(event:ListEvent):void
			{
				music_play.enabled=true;
				url_s=url_t+music_list.selectedItem.path;
				temp_name=music_list.selectedItem.title;
			}

			protected function volume_changeHandler(event:Event):void
			{
				if(!goon){
					musicSC.soundTransform=new SoundTransform(volume.value/10);
				}
			}


			protected function music_stop_clickHandler(event:MouseEvent):void
			{
				music_play.enabled=false;
				music_stop.enabled=false;
				goon=true;
				pause=false;
				musicSC.stop();
				music_play.label="开始";
			}

		]]>
	</fx:Script>

	<fx:Declarations>
		<fx:Model id="musicData" source="musiclist.xml"/>
		<s:ArrayList id="musicList" source="{musicData.music}"/>
	</fx:Declarations>
	<s:Button x="10" y="188" label="开始" id="music_play" click="music_play_clickHandler(event)"  width="51" cornerRadius="9"/>
	<s:HSlider x="129" y="188" width="67" height="15" id="volume" change="volume_changeHandler(event)" minimum="0" maximum="10" value="5"/>
	<s:Label x="10" y="10" width="186" height="16" id="music_name" fontSize="12" text="歌曲名称" fontStyle="normal" fontWeight="normal" textDecoration="none" fontFamily="Arial" color="#7C2F00"/>
	<mx:DataGrid y="34" width="187" height="146" id="music_list" dataProvider="{musicList}" change="music_list_changeHandler(event)" dropShadowVisible="false" borderAlpha="0.6" selectedIndex="0" x="9">
		<mx:columns>
			<mx:DataGridColumn headerText="曲目" dataField="title"/>
			<mx:DataGridColumn headerText="path" dataField="path" visible="false"/>
		</mx:columns>
	</mx:DataGrid>
	<s:Button x="70" y="188" label="停止" id="music_stop" width="51" click="music_stop_clickHandler(event)" cornerRadius="9"/>
	<mx:Image x="138" y="2" width="55" height="31" source="EyeT.gif"/>
	
</s:WindowedApplication>

你可能感兴趣的:(xml,Flex,Flash,Adobe,AIR)