声音类 的练习

                                                                         播放与停止

砸在舞台添加一个小按钮,然后在第1帧写上如下代码:

import flash.net.URLRequest;

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.events.MouseEvent;

var url:URLRequest = new URLRequest("kate havnevik - solo.mp3");

var sound:Sound = new Sound();

sound.load(url);

var position:Number = 0;

var channel:SoundChannel = new SoundChannel();

btn.label = "播放";

btn.addEventListener(MouseEvent.CLICK,PS);

function PS(e:MouseEvent):void

{

    if(btn.label == "播放")

      {

         channel = sound.play(position);

        btn.label = "停止";

      }

  else

 {

  channel.stop();

  position = channel.position;

  btn.label = "播放";

 }

}

                                                                              播放与暂停

与播放与停止类似,相差不大。

import flash.media.Sound;

import flash.net.URLRequest;

import flash.media.SoundChannel;

import flash.events.MouseEvent;

 

var sound:Sound = new Sound();

sound.load(new URLRequest("kate havnevik - solo.mp3"));

var p:Number = 0;

var channel:SoundChannel = new SoundChannel();

btn.label = "播放";

btn.addEventListener(MouseEvent.CLICK,PS);

function PS(e:MouseEvent):void

{  

if(btn.label == "播放")

 {  

 channel = sound.play(p);

  btn.label = "暂停";

 }

else

 {  

 p = channel.position;

  channel.stop();

  btn.label = "播放";

 }

}

 

                                                 加载进度条

 

import flash.display.Sprite;

import flash.net.URLRequest;

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.events.ProgressEvent;

import flash.events.Event;

 

//进度条

var bar:Sprite = new Sprite();

bar.graphics.beginFill(0x123456);

bar.graphics.drawRect(0,0,350,20);

bar.graphics.endFill();

addChild(bar);

bar.x = 75;

bar.y = 350;

bar.scaleX = 0;

 

var bar2:Sprite = new Sprite();

bar2.graphics.beginFill(0x876543);

bar2.graphics.drawRect(0,0,350,20);

bar2.graphics.endFill(); addChild(bar2);

bar2.x = 75;

bar2.y = 350;

bar2.scaleX = 0;

 

var url:URLRequest = new URLRequest("1.mp3");

var sound:Sound = new Sound();

sound.load(url);

 

var channel:SoundChannel = new SoundChannel();

sound.addEventListener(ProgressEvent.PROGRESS,Progress);

sound.addEventListener(Event.COMPLETE,addSound);

addEventListener(Event.ENTER_FRAME,changeV);

function Progress(e:ProgressEvent):void

{

 bar.scaleX = e.bytesLoaded/e.bytesTotal;

 trace(e.bytesLoaded/e.bytesTotal)

}

function addSound(e:Event):void

{

 channel = sound.play();

}

function changeV(e:Event):void

{

 bar2.scaleX = channel.position/sound.length ;

 trace(channel.position/sound.length);

}

                                                                      控制声音和声像

import flash.media.Sound;

import flash.media.SoundTransform;

import flash.media.SoundChannel;

import flash.net.URLRequest;

import flash.events.Event;

var snd:Sound = new Sound(new URLRequest("1.mp3"));

var trans:SoundTransform = new SoundTransform(0.6,-1);

var channel:SoundChannel = snd.play(0,1,trans);

var hd:Number = 0;

addEventListener(Event.ENTER_FRAME,aa)

function aa(e:Event):void

{  

  hd+=0.1;

  trans = new SoundTransform(1,Math.sin(hd));

  channel.soundTransform = trans;  

  trace(hd);

}

                                                                                   修改MP3的声音

 

import flash.media.Sound;

import flash.net.URLRequest;

import flash.events.Event;

import flash.events.SampleDataEvent;

import flash.utils.ByteArray;

 

var mysound:Sound = new Sound();

var sourceSnd:Sound = new Sound();

var url:URLRequest =new URLRequest("1.mp3");

sourceSnd.load(url);

sourceSnd.addEventListener(Event.COMPLETE,loaded);

function loaded(e:Event):void

{  

mysound.addEventListener(SampleDataEvent.SAMPLE_DATA,processSound);

 mysound.play();

}

function processSound(e:SampleDataEvent):void

{

        var bytes:ByteArray = new ByteArray();

        sourceSnd.extract(bytes,8192);  

       e.data.writeBytes(upOctve(bytes));

}

function upOctve(bytes:ByteArray):ByteArray

{

    var returnBytes:ByteArray = new ByteArray();

    bytes.position = 0;

    while(bytes.bytesAvailable > 0)

    {     

      returnBytes.writeFloat(bytes.readFloat());   

      returnBytes.writeFloat(bytes.readFloat());

           if (bytes.bytesAvailable > 0)     

         {        

            bytes.position += 8;   

         }   

    }  

   return returnBytes;

}

 

                                           

 

posted @ 2012-11-09 22:25  泡沫小灰灰  阅读(191)  评论(0编辑  收藏  举报