AS3 CookBook学习整理(十二)

1. 获得声音文件及当前下载量的大小

通过Sound对象的bytesTotal和bytesLoaded属性

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Sound;
 import flash.media.SoundLoaderContext;
 import flash.net.URLRequest;
 public class Sample0618 extends Sprite
 {
  private var bar:Sprite;
  private var music:Sound;
  
  public function Sample0618()
  {
   var progressBar:Sprite = new Sprite();
   progressBar.graphics.beginFill(0xFFFFFF);
   progressBar.graphics.drawRect(0,0,300,5);
   progressBar.graphics.endFill();
   progressBar.x = progressBar.y = 100;
   
   bar = new Sprite();
   progressBar.addChild(bar);
   this.addChild(progressBar);
   
   music = new Sound(new URLRequest("http://bbs.yawb.net/music/nobody.mp3"));
   music.play();
   
   this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   if(music.bytesTotal>0)
   {
    var percent:Number = music.bytesLoaded/music.bytesTotal;
    bar.graphics.clear();
    bar.graphics.beginFill(0xCCCCCC);
    bar.graphics.drawRect(0,0,300*percent,5);
    bar.graphics.endFill();
   }
  }
 }
}

2. 读取声音文件的ID3标签数据

MP3声音文件可以包含ID3标签,标签大多包含一些如songname,artist,album,genre,year等元数据,不过并不是都有,但大多数情况下都有songname和artist标签

通过Sound对象的id3属性可获得这些数据,如:_sound.id3.songName

如果歌曲还没有下载到swf中,id3数据是不能够被访问的,可以监听Sound对象的ID3事件来判断id3数据是否已下载:_sound.addEventListener(Event.ID3, onID3);

package {
 import flash.display.Sprite;
 import flash.media.Sound;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.text.TextField;
 public class Sample0618 extends Sprite
 {
  private var _sound:Sound;
  
  public function Sample0618()
  {
   _sound = new Sound(new URLRequest("Sleepsong.mp3"));
   _sound.addEventListener(Event.ID3, onID3);
   _sound.play( );
  }
  
  public function onID3(event:Event):void 
  {
   // Create a text field and display it 
   var id3Display:TextField = new TextField( );
   addChild(id3Display);
   id3Display.x = 10;
   id3Display.y = 20;
   id3Display.width = 200;
   id3Display.height = 200;
   id3Display.background = true;
   id3Display.multiline = true;
   id3Display.wordWrap = true;
   id3Display.appendText(_sound.id3.songName + "\n");
   id3Display.appendText(_sound.id3.artist + "\n");
   id3Display.appendText(_sound.id3.album + "\n");
   id3Display.appendText(_sound.id3.year + "\n");
  }
 }
}

3. 判定音乐是否播放完毕(soundComplete事件)

很多情况下我们需要知道因为是否播放完毕,例如,音乐播放器的播放列表,需要判定是否播放完毕以便播放下一首音乐

当调用Sound对象的play()方法时,它会返回一个SoundChannel(声音通道)对象,因此每一首正在播放的音乐都会产生一个SoundChannel对象,当声音播放完毕时,对应的SoundChannel对象会发出soundComplete事件,就是flsh.events.Event.SOUND_COMPLETE

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Sound;
 import flash.media.SoundChannel;
 import flash.net.URLRequest;
 public class Sample0618 extends Sprite
 { 
  private var songList:Array;
  private var music:Sound;
  private var index:int = 0;
  
  public function Sample0618()
  {
   songList = ["songs/orc.mp3","songs/dwarf.mp3","songs/night.mp3","songs/human.mp3"];
   playNextSong();
  }
  
  private function playNextSong():void
  {
   if(index<songList.length)
   {
    music = new Sound(new URLRequest(songList[index]));
    var sc:SoundChannel =music.play();
    sc.addEventListener(Event.SOUND_COMPLETE,onSoundComplete);
    index++;
   }
  }
  
  private function onSoundComplete(event:Event):void
  {
   playNextSong();
  }
 }
}

4. 跟踪音乐播放进度

使用Sound.length得到歌曲当前下载的长度(毫秒),SoundChannel.position得到当前的播放位置(毫秒)

Sound.length得到的并不是整首歌曲的长度,而是当前已下载的长度(毫秒)

要得到歌曲的总长度,要先得到"缓冲百分比"(bytesLoaded/bytesTotal),然后用Sound.length除以缓冲百分比,就得到了歌曲的真实长度(毫秒)

PS:可以这样理解,下载量有两种表示方式,一种是字节(byte)表示,一种是时间(毫秒)表示;字节所占的百分比 == 时间所占的百分比,用 下载量的时间表示 × 这个百分比值,就得到了歌曲总大小的时间表示

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Sound;
 import flash.media.SoundChannel;
 import flash.media.SoundLoaderContext;
 import flash.net.URLRequest;
 public class Sample0618 extends Sprite
 {
  private var music:Sound;
  private var sc:SoundChannel;
  private var downloadBar:Sprite;
  private var playBar:Sprite;
  
  public function Sample0618()
  {
   var progressBox:Sprite = new Sprite();
   progressBox.graphics.beginFill(0xFFFFFF);
   progressBox.graphics.drawRect(0,0,300,5);
   progressBox.graphics.endFill();
   progressBox.x = progressBox.y = 100;
   
   downloadBar = new Sprite();
   playBar = new Sprite();
   
   progressBox.addChild(downloadBar);
   progressBox.addChild(playBar);
   this.addChild(progressBox);
      
   music = new Sound(new URLRequest("http://www.hongshizi.net/yywz/mp3/zxgq/058.mp3"));
   sc = music.play();
   
   this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   if(music.bytesTotal>0)
   {
    var downPercent:Number = music.bytesLoaded / music.bytesTotal;
    downloadBar.graphics.clear();
    downloadBar.graphics.beginFill(0xCCCCCC);
    downloadBar.graphics.drawRect(0,0,300*downPercent,5);
    downloadBar.graphics.endFill();
    
    if(music.length>0 && downPercent>0)
    {
     var songLength:int = music.length / downPercent;
     var playPercent:Number = sc.position / songLength;
     
     playBar.graphics.clear();
     playBar.graphics.beginFill(0xFF0000);
     playBar.graphics.drawRect(0,0,300*playPercent,5);
     playBar.graphics.endFill();
    }
   }
  }
 }
}

5. 暂停和继续播放音乐

通过调用Sound对象的close()方法可以停止播放,但是这样也停止了声音流,要想重新播放,必须再次调用load()方法

SoundChannel类提供了一个stop()方法,它可以让音乐暂停而不影响声音流中断,要想重新播放,调用play()方法即可

你会发现,当再次调用play()方法时,音乐会从头开始播放而不是从暂停的地方开始,这个时候需要记录SoundChannel类的position属性,把它作为play()方法的第一个参数

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.Sound;
 import flash.media.SoundChannel;
 import flash.net.URLRequest;
 import flash.text.TextField;
 public class Sample0619 extends Sprite
 { 
  private var music:Sound;
  private var sc:SoundChannel;
  private var playPostion:int= 0;
  private var isPlay:Boolean = false;
  
  public function Sample0619()
  {
   music = new Sound(new URLRequest("钟无艳.mp3"));
   
   var btnPlay:TextField = new TextField();
   btnPlay.background = true;
   btnPlay.backgroundColor = 0xFFFF00;
   btnPlay.text = "播放/暂停";
   btnPlay.x = btnPlay.y = 100;
   btnPlay.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
   this.addChild(btnPlay);
  }
  
  private function onMouseUp(event:MouseEvent):void
  {
   isPlay = !isPlay;
   
   if(isPlay)
   {
    sc = music.play(playPostion);
   }
   else
   {
    playPostion = sc.position;
    sc.stop();
   }
  }
 }
}

6. 获得音乐的左声道和右声道

任何声音,当在播放时产生强或弱的声波,我们称之为振幅,ActionScript3.0可获得一个立体声的左右声道的振幅,分别为SoundChannel的leftPeak和rightPeak属性,它们的值范围是0.0到1.0,1.0表示最大的音量

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Sound;
 import flash.media.SoundChannel;
 import flash.net.URLRequest;
 
 public class Sample0619 extends Sprite
 { 
  private var sc:SoundChannel;
  private var leftPeakBox:Sprite;
  private var rightPeakBox:Sprite = new Sprite();
  
  public function Sample0619()
  {
   var music:Sound = new Sound(new URLRequest("牧师们都有爱.mp3"));
   sc = music.play();
   
   leftPeakBox = new Sprite();
   leftPeakBox.x = 100;
   leftPeakBox.y = 100;
   rightPeakBox = new Sprite();
   rightPeakBox.x = 100;
   rightPeakBox.y = 130;
   this.addChild(leftPeakBox);
   this.addChild(rightPeakBox);
   
   this.addEventListener(Event.ENTER_FRAME,onEnterFrame);   
  }
  
  private function onEnterFrame(event:Event):void
  { 
   leftPeakBox.graphics.clear();
   leftPeakBox.graphics.beginFill(0xFFFF00);
   leftPeakBox.graphics.drawRect(0,0,sc.leftPeak * 100,10);
   leftPeakBox.graphics.endFill();
   
   rightPeakBox.graphics.clear();
   rightPeakBox.graphics.beginFill(0xFFFF00);
   rightPeakBox.graphics.drawRect(0,0,sc.rightPeak * 100,10);
   rightPeakBox.graphics.endFill();
  }
 }
}

7. 停止播放所有的音乐

使用SoundMixer.stopAll()

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.Sound;
 import flash.media.SoundMixer;
 import flash.net.URLRequest;
 
 public class Sample0619 extends Sprite
 { 
  public function Sample0619()
  {
   var music1:Sound = new Sound(new URLRequest("牧师们都有爱.mp3"));
   music1.play();
   
   var music2:Sound = new Sound(new URLRequest("钟无艳.mp3"));
   music2.play();  
   
   stage.addEventListener(MouseEvent.CLICK,onClick);
  }
  
  private function onClick(event:MouseEvent):void
  {
   SoundMixer.stopAll();
  }
 }
}

8. 读取声音文件的声谱(波形图)

要获得声谱数据,首先要创建一个空的ByteArray:

var spectrum:ByteArray = new ByteArray();

然后再将ByteArray作为SoundMixer.computeSpectrum()方法的参数:

SoundMixer.computeSpectrum(spectrum);

当调用方法结束后,ByteArray里会生成512个浮点值(范围为-1.0到1.0),其中前256个值表示左声道,后256个值表示右声道

package {
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Sound;
 import flash.media.SoundMixer;
 import flash.net.URLRequest;
 import flash.utils.ByteArray;
 
 public class Sample0619 extends Sprite
 {
  private var bitmapData:BitmapData;
  
  public function Sample0619()
  {
   bitmapData = new BitmapData(256,150,true,0xff000000);
   var bitmap:Bitmap = new Bitmap(bitmapData);
   bitmap.x = bitmap.y = 100;
   this.addChild(bitmap);
    
   this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    
   var music:Sound = new Sound(new URLRequest("钟无艳.mp3"));
   music.play();
  }
  private function onEnterFrame(event:Event):void
  {
   //每次都要清空位图颜色,否则会被每次生成的颜色填满位图,就没有动态效果了 
   bitmapData.fillRect(bitmapData.rect,0xff000000);
   
   //每次都获取当前声音波形的快照,存入数组 
   var array:ByteArray = new ByteArray();
   SoundMixer.computeSpectrum(array);
   //读取前256个浮点值,即左声道 
   for(var i:int=0;i<256;i++)
   {
    bitmapData.setPixel32(i,array.readFloat()*20+40,0xFFFF0000);
   }
   
   //读取后256个浮点值,即右声道 
   for(var j:int=0;j<256;j++)
   {
    bitmapData.setPixel32(j,array.readFloat()*20+80,0xFF0000FF);
   }
  }
 }
}

PS:ByteArray()有一个游标,每次调用writeByte()、readFloat()等方法时,游标都会前移,可以通过访问或设置ByteArray.position来确定游标的位置

9. 改变音乐的音量和声道(平衡)

可以通过修改SoundChannel对象的soundTransform属性来设置

soundTransform属性包含两个属性:

volume(音量) -- 范围是0.0到1.0

pan(平衡) -- 范围是-1.0到1.0,-1为全左声道,1为全右声道

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.Sound;
 import flash.media.SoundChannel;
 import flash.media.SoundTransform;
 import flash.net.URLRequest;
 
 public class Sample0619 extends Sprite
 { 
  private var sc:SoundChannel;
  
  public function Sample0619()
  {
   var music:Sound = new Sound(new URLRequest("蜀绣.mp3"));
   sc = music.play();
   
   stage.addEventListener(MouseEvent.CLICK,onClick);
  }
  
  private function onClick(event:MouseEvent):void
  {
   var transform:SoundTransform = new SoundTransform();
   transform.volume = .5;
   transform.pan = -1;
   
   //sc.soundTransform = new SoundTransform(.5,-1); 
   sc.soundTransform = transform;
  }
 }
}

10. 载入并播放视频

大部分Flash视频内容都是以.flv格式存储的,通过ActionScript在运行时载入到Flash播放器

Flash视频载入有两种形式:渐进式下载和流下载。flv流视频需要流服务器,比如Flash Media Server。相反,渐进式下载不需要额外的软件,不过ActionScript处理这两种的方式是相同的

加载并播放视频分下面几个步骤:

<1> 创建一个NetConnection对象,并调用该对象的connect()方法。如果连接没有使用流服务器的本地flv文件,则connect(null)

var nc:NetConnection = new NetConnection();

nc.connect(null);

<2> 基于NetConnection创建一个NetStream对象并指定要加载的flv文件

var ns:NetStream = new NetStream(nc);

<3> 设置NetStream对象的client属性

ns.client = this;

//……

//public function onMetaData(event:Object):void

//……

<4> 创建Video对象,并调用该对象的attachNetStream()方法附加NetStream对象

var video:Video = new Video();

video.attachNetStream(ns);

this.addChild(video);

<5> 调用NetStream对象的play()方法播放视频

ns.play("demo.flv");

package {
 import flash.display.Sprite;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0622 extends Sprite
 {
  public function Sample0622()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   
   var ns:NetStream = new NetStream(nc);
   ns.client = this;
   
   var video:Video = new Video();
   video.attachNetStream(ns);
   this.addChild(video);
   
   ns.play("demo.flv");
  }
  
  public function onMetaData(infoObject:Object):void
  {
   trace("metadata:");
   for (var propName:String  in  infoObject) 
   {
    trace(propName + " = " + infoObject[propName]);
   }
  }
 }
}
posted @ 2010-07-15 15:21  CoderWayne  阅读(1094)  评论(0编辑  收藏  举报