In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

イシカワタケループ

soundcloud.comの特定のアーティスト曲をループ再生します。
サンプルとしてイシカワタケル氏を使わせてもらいました。
http://soundcloud.com/takerui

つぶやきボタン付き。
package
{
    import com.bit101.components.PushButton;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    
    import jp.progression.commands.lists.SerialList;
    
    
    [SWF(width="465",height="465",frameRate="60",backgroundColor="0xffffff")]
    public class ASTest extends Sprite
    {
        private var _listGetCommand:ListGetCommand;
        private var _index:int = 0;
        
        private var _nextButton:PushButton;
        private var _prevButton:PushButton;
        private var _toggleButton:PushButton;
        private var _linkButton:PushButton;
        private var _tweetButton:PushButton;
        private var _infoTF:TextField;
        
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _lastPosition:Number;
        
        /**
         * soundcloudのアプリ申請するともらえるクライアントID
         * みんなも早速登録しよう
         * http://soundcloud.com
         */
        private const APP_ID:String = '889391312091fc00a7e03245c4ce7b8e';
        
        /**
         * 取得したい作曲者のsoundcloud上のID。ユーザのページのURLと同じ。
         * サンプルとしてtakerui氏を使わせてもらいました
         * http://soundcloud.com/takerui
         */
        private const ARTIST_ID:String = 'takerui';
        
        public function ASTest()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
            //init();
        }
        
        private function init(evt:Event = null):void
        {
            constructUI();
            constructLogic();
            go();
        }
        
        private function constructUI():void
        {
            _nextButton = new PushButton();
            _prevButton = new PushButton();
            _toggleButton = new PushButton();
            _linkButton = new PushButton();
            _tweetButton = new PushButton();
            _infoTF = new TextField();
            
            _nextButton.label = 'next ->';
            _prevButton.label = '<- prev';
            _toggleButton.label = 'pause';
            _linkButton.label = 'permalink';
            _tweetButton.label = 'tweet';
            
            _toggleButton.x = 110;
            _nextButton.x = _tweetButton.x = 220;
            _linkButton.y = _tweetButton.y = 30;
            
            addChild(_nextButton);
            addChild(_prevButton);
            addChild(_toggleButton);
            addChild(_linkButton);
            addChild(_tweetButton);
            
            _nextButton.addEventListener(MouseEvent.CLICK, onClick);
            _prevButton.addEventListener(MouseEvent.CLICK, onClick);
            _toggleButton.addEventListener(MouseEvent.CLICK, onClick);
            _linkButton.addEventListener(MouseEvent.CLICK, onClick);
            _tweetButton.addEventListener(MouseEvent.CLICK, onClick);
            
            _infoTF = new TextField();
            _infoTF.autoSize = TextFieldAutoSize.LEFT;
            _infoTF.y = 150;
            
            addChild(_infoTF);
        }
        
        private function constructLogic():void
        {
            _listGetCommand = new ListGetCommand(APP_ID, ARTIST_ID);
            _listGetCommand.onComplete = playSound;
        }
        
        private function go():void
        {
            _infoTF.text = 'Loading...';
            _listGetCommand.execute();
        }
        
        private function playSound():void
        {
            if(Values.vector.length == 0)
            {
                _infoTF.text = 'oops something is technically wrong';
                return;
            }
            
            if(_index >= Values.vector.length)
            {
                _index = 0;
                playSound();
                return;
            }
            if(_index < 0)
            {
                _index = Values.vector.length-1;
                playSound();
                return;
            }
            
            if(_channel != null)
            {
                _channel.stop();
                _channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComp);
            }
            
            var song:SongModel = Values.vector[_index];
            _sound = new Sound(new URLRequest(song.stream_url + '?client_id=' + APP_ID));
            _channel = _sound.play(0,1);
            _channel.addEventListener(Event.SOUND_COMPLETE, onSoundComp);
            _lastPosition = 0;
            
            _infoTF.text = 'Now Playing: ' + song.title + ' / ' + song.artist;
            _toggleButton.label = 'pause';
        }
        
        private function pause():void
        {
            if(_channel!=null)
            {
                _lastPosition = _channel.position;
                _channel.stop();
                _toggleButton.label = 'play';
            }
        }
        
        private function resume():void
        {
            if(_channel!=null)
            {
                _channel = _sound.play(_lastPosition, 1);
            }
            else
            {
                playSound();
            }
            _toggleButton.label = 'pause';
        }
        
        private function onSoundComp(evt:Event):void
        {
            _channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComp);
            _channel = null;
            _index ++;
            playSound();
        }
        
        private function onClick(evt:MouseEvent):void
        {
            switch(evt.target)
            {
                case _nextButton:
                    _index ++;
                    playSound();
                    break;
                case _prevButton:
                    _index --;
                    playSound();
                    break;
                case _toggleButton:
                    (_toggleButton.label=='play')?resume():pause();
                    break;
                case _linkButton:
                    pause();
                    navigateToURL(new URLRequest(Values.vector[_index].link), '_blank');
                    break;
                case _tweetButton:
                    navigateToURL(new URLRequest('http://twitter.com/intent/tweet?text=Now Playing: ' + Values.vector[_index].title + ' / ' + Values.vector[_index].artist + '&url=' + Values.vector[_index].link), '_blank');
                    break;
            }
        }
        
    }
}


import com.adobe.serialization.json.JSON;

import flash.net.URLRequest;

import jp.progression.casts.*;
import jp.progression.commands.*;
import jp.progression.commands.display.*;
import jp.progression.commands.lists.*;
import jp.progression.commands.managers.*;
import jp.progression.commands.media.*;
import jp.progression.commands.net.*;
import jp.progression.commands.tweens.*;
import jp.progression.data.*;
import jp.progression.events.*;
import jp.progression.scenes.*;

class ListGetCommand extends SerialList
{
    
    private var _app_id:String;
    private var _artist_id:String;
    
    public function ListGetCommand ( app_id:String = '889391312091fc00a7e03245c4ce7b8e', artist_id:String = 'takerui', initObject:Object = null ) {
        // 親クラスを初期化します。
        super( initObject );
        
        _app_id = app_id;
        _artist_id = artist_id;
        
        // 実行したいコマンド群を登録します。
        addCommand(
            new LoadURL(new URLRequest('http://api.soundcloud.com/users/' + _artist_id + '/tracks.json?client_id=' + _app_id), {catchError:function():void{
                Values.json = null;
                executeComplete();
            }}
            )
            ,new Func(function():void{
                var res:String = this.latestData;
                var json:Object;
                try
                {
                    json = JSON.decode(res);
                } 
                catch(error:Error) 
                {
                    
                }
                Values.json = json;
                Values.vector = new Vector.<SongModel>();
                for each(var item:Object in json)
                {
                    var song:SongModel = new SongModel(item.title, item.user.username, item.permalink_url, item.stream_url);
                    (song.stream_url != null)?Values.vector.push(song):0;
                }
            })
        );
    }
    
    /**
     * インスタンスのコピーを作成して、各プロパティの値を元のプロパティの値と一致するように設定します。
     */
    override public function clone():Command {
        return new ListGetCommand( _app_id, _artist_id, this );
    }
}

class SongModel
{
    public var title:String;
    public var artist:String;
    public var link:String;
    public var stream_url:String;
    //ほかにもいろいろプロパティとれる
    public function SongModel(title:String, artist:String, link:String, stream_url:String)
    {
        this.title = title;
        this.artist = artist;
        this.link = link;
        this.stream_url = stream_url;
    }
}

class Values
{
    public static var json:Object;
    public static var vector:Vector.<SongModel>;
}