Quantcast
Channel: itouhiroはてなブログ
Viewing all articles
Browse latest Browse all 107

Flashで外部swfに埋め込んだサウンドをBGMにする

$
0
0

前の記事: Flashで外部swfに埋め込んだサウンドを使う http://itouhiro.hatenablog.com/entry/20130115/flash


FlashでBGMを再生する方法をメモ。

外部MP3ファイルをswfで読み込んで再生するのは、mp3のライセンス料がかかる危険があるらしい。(「商用利用のときは」ライセンス料必要らしい)
swfにmp3を埋め込んでおけば問題ないという。

しかし、ビルドごとにmp3を埋め込むのも何なので、mp3埋め込みswfを作っておき、それを実行時に読み込むことにする。


実際のFlash:
カーソルキーの ← → を押すと、曲を再生/別の曲を再生。
カーソルキーの ↑ ↓ を押すと、音量UP/DOWN。
スペースキーで曲をストップ。


音楽ファイルは
http://www.ne.jp/asahi/music/myuu/
を使わせていただいた。


環境

ソース

Main.as

package 
{
    importflash.display.Loader;
    importflash.display.Sprite;
    importflash.events.Event;
    importflash.events.KeyboardEvent;
    importflash.events.ProgressEvent;
    importflash.media.Sound;
    importflash.media.SoundChannel;
    importflash.media.SoundTransform;
    importflash.net.URLRequest;
    importflash.system.ApplicationDomain;
    importflash.system.LoaderContext;
    importflash.text.TextField;
    
    /**     * ...     * @author itouhiro     */
    [SWF(width="320",height="240",backgroundColor="0xfcfcfc",frameRate="30")]
    publicclass Main extendsSprite 
    {
        privatevar channel:SoundChannel;
        privatevar preloadText:TextField;
        privatevar songs:Array = [];
        privatevar songIndex:int;
        
        publicfunction Main():void 
        {
            if (stage) init();
            elseaddEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        privatefunctioninit(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point//preloader
            preloadText = newTextField();
            preloadText.x = stage.stageWidth/3; preloadText.y = stage.stageHeight/3;
            preloadText.text = 'Loading: 0 %';
            addChild(preloadText);
            
            //load external swfvar req:URLRequest = newURLRequest('https://sites.google.com/site/itouhiro/2013/20130326soundwavCS6.swf');
            var ld:Loader = newLoader();
            var context:LoaderContext = newLoaderContext();
            context.applicationDomain = ApplicationDomain.currentDomain;
            ld.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
            ld.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
            ld.load(req, context);            
        }
        
        privatefunction loadProgressHandler(e:ProgressEvent):void 
        {
            varpercentLoaded:int = Math.round(e.bytesLoaded / e.bytesTotal * 100);
            preloadText.text = 'Loading: ' + percentLoaded + ' %';
        }        
        
        privatefunction loadCompleteHandler(e:Event):void 
        {
            //removeChild(preloadText);// register resources(music) in the external swfvar wavfiles:Array = 'musicbox loop1 whistle'.split('');
            for (var i:int = 0; i < wavfiles.length; i++) {
                var classRef:Class = ApplicationDomain.currentDomain.getDefinition(wavfiles[i]) as Class;
                var snd:Sound = new classRef();
                songs[i] = snd;
            }
            
            // play sound
            channel = newSoundChannel();
            songIndex = 0;
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            
            // operate soundvar infoText:TextField = newTextField();
            infoText.text = "^ VolUp\nv VolDown\n< PrevSong\n> NextSong";
            addChild(infoText);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        }
        
        privatefunction keyDownHandler(e:KeyboardEvent):void 
        {
            var tran:SoundTransform;
            trace('keyCode:' + e.keyCode);
            if (e.keyCode == 40) {
                tran = channel.soundTransform;
                tran.volume = tran.volume / 2 + 0.01;
                channel.soundTransform = tran;
            }elseif (e.keyCode == 38) {
                tran = channel.soundTransform;
                tran.volume = tran.volume * 2;
                channel.soundTransform = tran;
            }elseif (e.keyCode == 37) {
                channel.stop();
                songIndex = songIndex - 1;
                if (songIndex < 0) { songIndex = songs.length - 1; }
                channel = songs[songIndex].play(0, 100);
            }elseif (e.keyCode == 39) {
                channel.stop();
                songIndex = (songIndex + 1) % songs.length;
                channel = songs[songIndex].play(0, 100);
            }elseif (e.keyCode == 32) {
                channel.stop();
            }
        }
        
        privatefunction enterFrameHandler(e:Event):void 
        {
            preloadText.text = 'PlayingTime: ' + (channel.position/1000).toString().slice(0,4) + ' sec.';
        }
    }
}


Sound.play(0,100) の1番目の引数 0 は曲のはじめから演奏するということ。2番目の引数 100 は100回ループするということ。


SoundChannel.stop() で曲を止める。もし止めずに Sound.play()すると、複数の曲を重ねて演奏する。


サウンド埋め込みswfはFlash CS6にwavファイルを読み込ませ、sound stream設定で mp3 80KHz monoral で[パブリッシュ]。
f:id:itouhiro:20130326232541p:plain





 


Viewing all articles
Browse latest Browse all 107

Trending Articles