打印

[FLEX] 背景音乐停止

引用:
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationCompleteEffect="{sound_effect}">
    <!--声音效果对象-->
    <mx:SoundEffect id="sound_effect" source="@Embed('assets/sound.mp3')"

     panFrom="-1" panTo="1" loops="1" volumeFrom = "1" volumeTo="0.1" duration="3000"

    useDuration="false"/>

    <!--定义按钮的鼠标按下的行为-->

<mx:Button x="198" y="65" label="Play" width="103" fontSize="12" mouseDownEffect=

    "{sound_effect}"/>
     <mx:Button x="78" y="65" label="Stop" width="103" fontSize="12" mouseDownEffect=

    "{sound_effect.pause()}"/>

</mx:Application>
当一运行时候就自动播放背景音乐;但是在音乐停止按钮(stop)却不作用
    thanks
mouseDown=
"sound_effect.pause()"

這樣呢?
紫色的風.net
迷一样打不开的地址

回复 enc0717 在 2# 的帖子

sorry
试了不行
有什么錯誤提示嗎?
紫色的風.net
迷一样打不开的地址

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!

回复 enc0717 在 4# 的帖子

没有
<mx:Button x="198" y="65" label="Play" width="103" fontSize="12" mouseDown=
    "sound_effect.play()"/>
<mx:Button x="78" y="65" label="Stop" width="103" fontSize="12" mouseDown=
    "sound_effect.pause()"/>

都改成这样后,点击stop只能停止一次;再继续点击play出现多个重复播放,点stop无反应。

TOP

在as3里面,暫停之后我記得是不會恢復播放的。
你需要記錄下當前播放位置,
在下一次播放的時候加入這個值來實現繼續播放。

而在點擊多次播放需要作出處理。
比如如果正在播放,就把那一次的點擊給忽略掉,應該播放正在播放的東西,是沒有那個需要的。

我封裝過一個類,可以解決這些麻煩,你可以直接拿著用。
需要的話晚上給你。
紫色的風.net
迷一样打不开的地址

TOP

复制内容到剪贴板
代码:
<mx:Button x="198" y="65" label="Play" width="103" fontSize="12" click="sound_effect.resume();"/>
<mx:Button x="78" y="65" label="Stop" width="103" fontSize="12" click="sound_effect.pause();"/>
SoundEffect为什么还要用实现播放暂停?

TOP

使用方法。
复制内容到剪贴板
代码:
import YrctnodMp3Player;
var myPlayer:YrctnodMp3Player = new YrctnodMp3Player ();
myPlayer.contentList = ["清凉01.mp3"];
myPlayer.load ();
//目前完成的主要调用接口
//myPlayer.pause ();
//myPlayer.play  ();
//myPlayer.stop  ();
//myPlayer.next  ();
//myPlayer.prev  ();
//使用方法2
//var myPlayer:YrctnodMp3Player = new YrctnodMp3Player ();
//myPlayer.load ("清凉01.mp3");
//這個是沒有開發完成的類,沒有加入自動下一麯,循環模式等
//但適用你的需求。
播放器类
复制内容到剪贴板
代码:
package
{//////////////////////////////////////////////////////////////////////////////
    
    import    flash.media.ID3Info;
    import    flash.media.Sound;
    import    flash.media.SoundChannel;
    import    flash.media.SoundLoaderContext;
    import    flash.media.SoundMixer;
    import    flash.media.SoundTransform;
    import    flash.net.URLLoader;
    import    flash.net.URLRequest;
    import    flash.utils.ByteArray;
    import    flash.utils.Timer;
    import    flash.events.Event;
    import    flash.events.EventDispatcher;
    import    flash.events.IOErrorEvent;
    import    flash.events.ProgressEvent;
    import    flash.events.TimerEvent;
    import flash.errors.IOError;
    //
    // Class YrctnodMp3Player
    //    
    public class YrctnodMp3Player extends EventDispatcher {
        
        
        //------------------------------
        // properties
        // 屬性
        //------------------------------
        /**
         * if auto play
         * 是否自動播放
         */
        public var autoPlay:Boolean;
        /**
         * the stored "url" list of our musics
         * 內容列表 "url"地址
         */
        public var contentList:Array;
        /**
         * the index of playing music in the list
         * 當前播放音樂在列表中所在的下標
         */
        public var currentIndex:Number;
        /**
         * the period of time on loading
         * 加載消耗的時間
         */
        public var usedSeconds:Number;
        /**
         * the music object
         * 音樂發生器
         */
        protected var music:Sound;
        /**
         * the music that is playing
         * 當前播放的音樂
         */
        protected var currentMusic:SoundChannel;
        /**
         * the sound transform   to control the volum
         * 轉換器 音量控制來源
         */
        protected var transform:SoundTransform;
        /**
         * the buffer of the music
         * 音樂的流式緩衝
         */
        protected var context:SoundLoaderContext;
        /**
         * period of time of loading
         * 加載消耗的時間
         */
        protected var loadingTimer:Timer;
        /**
         * the info from the music
         * 音樂文件信息
         */
        protected var id3info:ID3Info;
        /**
         * mixer
         * 混音器
         */
        protected var mix:SoundMixer;
        /**
         * default period of time of forward action
         * and backward action
         * 默認快進快退時間
         */
        protected var defaultSkipSeconds:Number;
        /**
         * loop mode
         * 循環模式
         */
        protected var loopMode:String;
        /**
         * music's infomation
         * 歌曲信息
         */
        protected var musicInfo:ID3Info;
        /**
         * save point of pausing
         * 暫停存儲點
         */
        private var pausePoint:Number;
        /**
         * if it is playing
         * 是否正在播放
         */
        private var _isPlaying:Boolean;
        /**
         * if it is pausing
         * 是否暫停中
         */
        private var _isPausing:Boolean;
        
        
        //------------------------------
        // constructor
        // 構造器
        //------------------------------
        /**
         * constructor
         * 構造器
         */
        public function YrctnodMp3Player () {
            super ();
            init ();
        }
        
        //------------------------------
        // protected functions
        // 保護級方法
        //------------------------------
        /**
         * initialization
         * 用于初始化
         */
        protected function init ():void {
            //
            //打開自動播放
            autoPlay     = true;
            //初始化音樂內容列表初始爲空數組
            contentList  = new Array ();
            //加載消耗的時間默認因爲不加載而無須賦值
            usedSeconds = 0;
            //剛剛載入的時候是默認沒有音樂在播放的
            currentMusic = null;
            //默認從第一首歌開始播放
            currentIndex = 0;
            //初始化緩衝上下文
            context      = new SoundLoaderContext ( 5000 );
            //初始化加載時間計時器
            loadingTimer = new Timer ( 1000 );
        }
        
        //------------------------------
        // public functions
        // 公開級方法
        //------------------------------
        /**
         * play the appionted position of the music
         * 跳轉到指定位置播放
         * @param pos - the appointed position
         */
        public function seek ( sec:Number ):Boolean {
            if ( currentMusic == null ) return false;
            
            if ( sec <= music.length ) {
                music.play ( sec , 1 , transform );
                return true;
            } else {
                return false;
            }
        }
        /**
         * load the sound file from appointed url
         * 加載指定地址的音樂文件
         * @param url - the appointed url
         * @return Boolean
         */
        public function load ( url:String=null ):Boolean {
            //
            //刷新音樂對象
            //
            music = new Sound ();
            var _url:URLRequest;
            if ( url == null ) {
                //
                //鏦列表中播放
                //
                if ( contentList == null || contentList.length == 0 ) {
                    //
                    //列表空 結束
                    //
                    return false;
                } else {
                    //
                    //加載列表索引標識的URL項
                    //
                    _url = new URLRequest ( contentList[currentIndex] );
                }
            } else {
                //
                //直接加載指定音樂
                //
                _url = new URLRequest ( url );
            }
            //
            //加載指定音樂
            //
            music.load ( _url , context );
            usedSeconds = 0;
            loadingTimer.reset();
            loadingTimer.addEventListener
            ( TimerEvent.TIMER , handleLoadingTimer );
            music.addEventListener
            ( Event.COMPLETE , handleMusicComplete );
            //music.addEventListener
            //( IOErrorEvent.IO_ERROR , handleIoError );
            loadingTimer.start();
            return true;
        }
        /**
         * play the sound
         * 播放音樂
         * @return Boolean
         */
        public function play ():Boolean {
            if ( currentMusic != null ) {
                if ( isPausing () ) {
                    //
                    //如果音樂暫停中
                    //
                    playStatus ();
                    currentMusic = music.play ( pausePoint , 1 , transform );    
                    return true;
                } else if ( isPlaying () ){
                    //
                    //如果音樂再播放
                    //
                    return false;
                } else {
                    //
                    //如果音樂處于停止狀態
                    //
                    playStatus ();
                    currentMusic = music.play ( 0 , 1 , transform );
                    return true;
                }
            } else {
                //
                //無音樂就緒
                //
                return false;
            }
        }
        /**
         * pause the music that is playing
         * 暫停當前正在播放的音樂
         * @return Boolean
         */
        public function pause ():Boolean {
            if ( currentMusic == null ) {
                return false;
            } else {
                //
                //設定儲存點
                //
                pausePoint = currentMusic.position;
                //
                //停止
                //
                currentMusic.stop();
                pauseStatus ();
                return true;
            }
        }
        /**
         * stop the music that is playing
         * 停止正在播放的音樂
         * @return Boolean
         */
        public function stop  ():Boolean {
            if ( currentMusic == null ) {
                return false;
            } else {
                stopStatus ();
                //
                //重置儲存點
                //
                pausePoint = 0;
                //
                //停止
                //
                currentMusic.stop();
                return true;
            }
        }
        /**
         * play the previous music
         * 播放上一首音樂
         * @return Boolean
         */
        public function prev  ():Boolean {
            if ( contentList == null || contentList.length == 0 ) {
                return false;
            } else {
                stop ();
                currentIndex--;
                if ( currentIndex <= -1 )
                currentIndex = contentList.length-1;
                //
                //裝載當前索引指向的URL
                //
                load ();
                return true;
            }
        }
        /**
         * play the next music
         * 播放下一首音樂
         * @return Boolean
         */
        public function next  ():Boolean {
            if ( contentList == null || contentList.length == 0 ) {
                return false;
            } else {
                stop ();
                currentIndex++;
                if ( currentIndex >= contentList.length )
                currentIndex = 0;
                //
                //裝載當前索引指向的URL
                //
                load ();
                return true;
            }
        }
        /**
         * forward
         * 快進
         * @param sec - the time you want to forward
         */
        public function forward ( sec:Number=0 ):Boolean {
            return true;
        }
        /**
         * backward
         * 快退
         * @param sec - the time you want to backward
         */
        public function backward ( sec:Number=0 ):Boolean {
            return true;
        }
        /**
         * reset player
         * 重置播放器
         */
        public function reset ():void {
        
        }
        /**
         * 開啓混音器波譜輸出
         */
        public function openSpectrum ():void {
        
        }
        /**
         * 關閉混音器波譜輸出
         */
        public function closeSpectrum ():void {
        
        }
        /**
         * revoke the buffering mode
         * 撤銷流式緩衝
         */
        public function revokeContext ():void {
            this.context = new SoundLoaderContext ( 5000 );
        }
        /**
         * resume the buffering mode
         * 恢復流式緩衝
         */
        public function resumeContext ():void {
            this.context = null;
        }
        /**
         * set time of the buffer
         * 設置緩衝的時間
         * @param milliseconds - the time of buffer
         */
        public function setBufferSize ( milliseconds:Number ):void {
            this.context.bufferTime = milliseconds;
        }
        /**
         * set volum
         * 設置音量
         * @param volum - volum
         */
        public function setVolum ( volum:Number ):void {
            this.transform.volume = volum;
        }
        /**
         * set the content list
         * 設定內容列表
         * @param list - the music list
         */
        public function setContentList ( list:Array ):void {
            this.contentList = list;
        }
        /**
         * set the loop mode
         * 設置循環模式
         * @param mode - the loop mode
         */
        public function setLoopMode ( mode:String ):void {
            this.loopMode = mode;
        }
        /**
         * set the default time for
         * forward action and backward action
         * 設置快進和快進的默認時間
         * @param sec - the period of time
         */
        public function setDefaultSkipSeconds ( sec:Number ):void {
            this.defaultSkipSeconds = sec;
        }
        /**
         * get the infomation of the music
         * 獲取音樂信息
         * @return ID3Info
         */
        public function getInfo ():ID3Info {
            return this.music.id3;
        }
        /**
         * get the music length (millisecond)
         * 獲得音樂長度 毫秒
         * @return Number
         */
        public function getLength ():Number {
            return this.music.length;
        }
        /**
         * get the current position
         * 獲得當前播放的位置 毫秒
         * @return Number
         */
        public function getPosition ():Number {
            return this.currentMusic.position;
        }
        /**
         * get the bytes have loaded
         * 獲取加載的字節數
         * @return Number
         */
        public function getBytesLoaded ():Number {
            return this.music.bytesLoaded;
        }
        /**
         * get the total bytes
         * 獲取字節總數
         * @return Number
         */
        public function getBytesTotal ():Number {
            return this.music.bytesTotal;
        }
        /**
         * 獲取內容列表
         * @return Array
         */
        public function getContentList ():Array {
            return this.contentList;
        }
        /**
         * get volum
         * 獲取音量
         * @return Number
         */
        public function getVolum ():Number {
            return this.transform.volume;
        }
        /**
         * get the mode of loop
         * 獲取循環模式
         * @return String
         */
        public function getLoopMode ():String {
            return this.loopMode;
        }
        /**
         * get the data of spectrum
         * 獲取波譜信息
         * @return ByteArray
         */
        public function getSpectrum ():ByteArray {
            return null;
        }
        /**
         * get the address of the current music
         * 獲取正在播放的網絡MP3地址
         * @return String
         */
        public function getPlayingURL ():String {
            return this.music.url;
        }
        /**
         * get the visited music list
         * 獲取訪問過的音樂列表
         * @return Array
         */
        public function getVisitedList ():Array {
            return null;
        }
        /**
         * get the milliseconds that playing
         * 獲取正在播放的位置毫秒數
         * @return Number
         */
        public function getMusicPosition ():Number {
            return this.currentMusic.position;
        }
        /**
         * get the length(millisecond) of the music
         * 獲取音樂毫秒長度
         * @return Number
         */
        public function getMusicLength ():Number {
            return this.music.length;
        };
        /**
         * wether it is been buffered
         * 是否帶有緩衝區
         */
        public function isBuffering ():Boolean {
            return this.music.isBuffering;
        }
        /**
         * wether it is playing
         * 是否正在播放
         * @return Boolean
         */
        public function isPlaying ():Boolean {
            return this._isPlaying;
        }
        /**
         * wether it is pausing
         * 是否暫停中
         * @return Boolean
         */
        public function isPausing ():Boolean {
            return this._isPausing;
        }
        //------------------------------
        // private functions
        // 私有級方法
        //------------------------------
        /**
         * play the music when it has been loaded
         * 當音樂加載完成開始播放
         * @param event - event
         */
        private function handleMusicComplete ( event:Event ):void {
            currentMusic = ( Sound ( event.target ) ).play( 0 , 1 , transform );
            playStatus ();
            if ( autoPlay == false ) {
                currentMusic.stop();
            } else {
                play ();
            }
            //加載計時器歸零用于下一次計算
            loadingTimer.stop();
            loadingTimer.reset();
            //移除再播放中不需要試用的監聽
            music.removeEventListener
            ( Event.COMPLETE , handleMusicComplete );
            loadingTimer.removeEventListener
            ( TimerEvent.TIMER , handleLoadingTimer );
        }
        /**
         * call it every one second when loading
         * 加載時間每一秒執行一次
         */
        private function handleLoadingTimer ( event:TimerEvent ):void {
            usedSeconds ++;
        }
        /**
         * 音樂文件路徑錯誤
         * @param event - error event
         */
        private function handleIoError ( event:IOErrorEvent ):void {
            trace ("error path");
        }
        /**
         * 設定爲播放狀態
         */
        private function playStatus ():void {
            _isPlaying = true;
            _isPausing = false;
        }
        /**
         * 設定爲停止狀態
         */
        private function stopStatus ():void {
            _isPlaying = false;
            _isPausing = false;
        }
        /**
         * 設定爲暫停狀態
         */
        private function pauseStatus ():void {
            _isPlaying = false;
            _isPausing = true;
        }
    }//here ending of the class
    
}//////////////////////////////////////////////////////////////////////////////
下载


[ 本帖最后由 enc0717 于 2008-5-10 01:34 编辑 ]
附件: 您所在的用户组无法下载或查看附件,您需要注册/登录后才能查看!
紫色的風.net
迷一样打不开的地址

TOP