前回ソースから再生リスト作成機能を追加しました(fullscreen用)
/**
* Copyright siouxcitizen ( http://wonderfl.net/user/siouxcitizen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xyzL
*/
// forked from siouxcitizen's 前回ソースからAmazonアフィリエイト用ボタンを追加してみました
// forked from siouxcitizen's 前回ソースから検索機能を追加してみました
// forked from siouxcitizen's 前回ソースから背景サムネイルとの連携機能を追加してみました
// forked from siouxcitizen's JSON形式で読み込まれたYoutube動画ID使って動画連続再生
// forked from fumix's youtube api
//
//前回ソースから再生リスト作成機能を追加しました(fullscreen用)
//
//以下サイトを参考にさせていただきました
//AS3.0で複数のButtonコンポーネントを動的に作成する方法
//http://oshiete.goo.ne.jp/qa/4236824.html
//↑このやり方が動的にボタンを作成するときの基本???
/**
* 前回ソースから再生リスト作成機能を追加しました(fullscreen用)
*
* きっと動くはず。。。
* 画面上部のテキスト部分に動画タイトル表示する機能を予定してましたが未実装(かわりに動画Indexだけ表示中)
*
* 検索結果動画リストから選択した動画の再生リストを作成できるようにしました
*「Add List」ボタンで(動画検索結果から)再生中動画のIDを再生リストに追加します
*「Del List」ボタンで再生リストの最後尾からリストデータを削除していきます
*再生リストの最大サイズは25です
*
*/
package {
import com.adobe.serialization.json.JSON;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.text.TextFieldType;
import com.bit101.components.ComboBox;
/**
* @author fumix
*/
[SWF(backgroundColor="#FFFFFF", frameRate="31", width="465", height="465")]
public class TestYoutube extends Sprite {
private const APIURL : String = 'http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=50&orderby=viewCount&alt=json&vq=';
private const KEYWORD : String = 'AKB48';
private var _urlLoader : URLLoader;
private var _w : int;
private var _h : int;
private var _jsonObject : Object;
private var _base : Sprite;
public var _bollon : Balloon;
private const YOUTUBE_PLAYER_URL : String = "http://www.youtube.com/v/lkHlnWFnA0c?version=3"; //公式ヘビーローテーションがデフォルト再生の1曲目に設定されてます
private var _loader : Loader;
private var _isPlayerDisp : Boolean = true ;
public var _player : Object;
public var _movieIdList : Array;
public var _movieIdListIndex : int = 0;
public var _selectedMovieIdList : Array = new Array();
public var _selectedMovieIdListIndex : int = 0;
public var _oldSelectedMovieIdListLength : int = 0;
private var _searchBox : TextField; // 検索キーワード
private var _box : ComboBox; // コンボボックス(検索キーワード)
private var _searchBtn : CustomButton; //「Search」ボタン
private var _amznAffiBtn : CustomButton; //「Amazon」ボタン
private var _addListBtn : CustomButton; //「Add List」ボタン
private var _delListBtn : CustomButton; //「Del List」ボタン
private var _amznSearchKeyword : String = "AKB48"; //Amazonで検索を行うためのキーワード
private var aButton:Object = {};
public var _selectedMovieTitle : TextField;
public var _isSelectedMoviePlaying : Boolean = false ;
//↓ここを変更してアマゾンアフィリエイトIDに自分のIDを設定します
private const AMAZON_AFFILIATE_ID : String = "myinteresting-22";
public function TestYoutube() {
if (stage) initialize();
else addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(event : Event = null) : void {
removeEventListener(Event.ADDED_TO_STAGE, initialize);
// ステージ設定
// stage.quality = StageQuality.LOW;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// 背景設定
_w = stage.stageWidth;
_h = stage.stageHeight;
var bmd : BitmapData = new BitmapData(_w, _h, false, 0xFFFFFF);
addChild(new Bitmap(bmd));
//ベース
_base = new Sprite();
addChild(_base);
//吹き出し
_bollon = new Balloon(0, 0, _w, _h);
addChild(_bollon);
// youtubeから検索結果のjsonをロード
_urlLoader = new URLLoader();
_urlLoader.addEventListener(Event.COMPLETE, urlLoadCompleteHandler);
_urlLoader.load(new URLRequest(APIURL + KEYWORD));
//Youtube Playerを取得して動画を再生
_loader = addChild(new Loader()) as Loader;
_loader.contentLoaderInfo.addEventListener(Event.INIT, youtubePlayerLoadCompleteHandler, false, 0, true);
_loader.x = 10;
_loader.y = 10;
_loader.load(new URLRequest(YOUTUBE_PLAYER_URL));
//var searchBox:TextField = new TextField();
_searchBox = new TextField();
_searchBox.text = "AKB48";
_searchBox.width = 150;
_searchBox.height = 18;
_searchBox.x = 300;
_searchBox.y = 10;
_searchBox.border = true;
_searchBox.borderColor = 0xFFFFFF;
_searchBox.background = true;
_searchBox.backgroundColor = 0x99DDFF;
_searchBox.type = TextFieldType.INPUT;
_searchBox.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addChild(_searchBox);
//候補の検索ワードなどの設定用
//var box:ComboBox = new ComboBox(this, 300, 30, "AKB48");
_box = new ComboBox(this, 300, 30, "AKB48");
_box.width = 150;
_box.addItem("AKB48");
_box.addItem("Anime OP");
_box.addItem("Aerosmith");
_box.addItem("Bon Jovi");
_box.addItem("U2");
_box.addItem("Oasis");
_box.addEventListener(Event.SELECT, onSelect);
addChild(_box);
_searchBtn = new CustomButton("Search");
_searchBtn.x = 300;
_searchBtn.y = 60;
_searchBtn.addEventListener(MouseEvent.MOUSE_DOWN,onSearchBtnDown);
addChild(_searchBtn);
_amznAffiBtn = new CustomButton("Amazon");
_amznAffiBtn.x = 300;
_amznAffiBtn.y = 90;
_amznAffiBtn.addEventListener(MouseEvent.MOUSE_DOWN,onAmznAffiBtnDown);
addChild(_amznAffiBtn);
_addListBtn = new CustomButton("Add List");
_addListBtn.x = 300;
_addListBtn.y = 140;
_addListBtn.addEventListener(MouseEvent.MOUSE_DOWN,onAddListBtnDown);
addChild(_addListBtn);
_delListBtn = new CustomButton("Del List");
_delListBtn.x = 300;
_delListBtn.y = 170;
_delListBtn.addEventListener(MouseEvent.MOUSE_DOWN,onDelListBtnDown);
addChild(_delListBtn);
_selectedMovieTitle = new TextField();
_selectedMovieTitle.x = 470;
_selectedMovieTitle.y = 10;
_selectedMovieTitle.width = 350;
addChild(_selectedMovieTitle);
}
private function onKeyUp(event:KeyboardEvent):void {
//Enterキーのキーコード13だったら、serchメソッドを実行。引数には適すと入力欄の文字列
if (event.keyCode == 13) {
//encodeURIComponent()を使うことによって、#や日本語にも対応
_urlLoader.load(new URLRequest(APIURL + encodeURIComponent(event.currentTarget.text)));
_amznSearchKeyword = encodeURIComponent(event.currentTarget.text); // アマゾン検索用キーワードを設定
if (_movieIdList.lenght > 0) { //動画リストが存在する場合はプレイ開始
_movieIdListIndex = 0;
_isSelectedMoviePlaying = false;
_player.loadVideoById(_movieIdList[_movieIdListIndex], 0, "default"); //1曲目再生 うまく動いてない???
}
}
}
private function onSelect(event:Event):void {
var box:ComboBox = event.currentTarget as ComboBox;
_searchBox.text = box.selectedItem.toString();
}
private function onSearchBtnDown(event:MouseEvent):void {
//encodeURIComponent()を使うことによって、#や日本語にも対応
_urlLoader.load(new URLRequest(APIURL + encodeURIComponent(_searchBox.text)));
_amznSearchKeyword = encodeURIComponent(_searchBox.text); // アマゾン検索用キーワードを設定
if (_movieIdList.lenght > 0) { //動画リストが存在する場合はプレイ開始
_movieIdListIndex = 0;
_isSelectedMoviePlaying = false;
_player.loadVideoById(_movieIdList[_movieIdListIndex], 0, "default"); //1曲目再生 うまく動いてない???
}
}
private function onAmznAffiBtnDown(event:MouseEvent):void {
var amznAffliateUrl : String = "http://www.amazon.co.jp/gp/search?ie=UTF8&keywords="
+ _amznSearchKeyword
+ "&tag="
+ AMAZON_AFFILIATE_ID
+ "&index=aps&linkCode=ur2&camp=247&creative=1211";
navigateToURL(new URLRequest(amznAffliateUrl), '_blank');
}
private function onDelListBtnDown(event:MouseEvent):void {
var l : int = _selectedMovieIdList.length;
if (l > 0) {
_selectedMovieIdList.pop();
removeChild(aButton[l-1]);
} else {
_selectedMovieTitle.text = "No More List Data to Delete";
}
}
private function onAddListBtnDown(event:MouseEvent):void {
// 再生リストからは動画を再生リストに設定できない仕様にしてます
if (_isSelectedMoviePlaying) {
_selectedMovieTitle.text = "Please Select and Add Movie Data from Movie Search Result";
return;
}
// 再生リストの長さをとりあえず最大25に設定
if (_selectedMovieIdList.length > 24) {
_selectedMovieTitle.text = "25 is Max Movie List Size";
return;
}
//var debug01 : TextField = new TextField();
//debug01.x = 300;
//debug01.y = 160;
//debug01.text = "debug01: id = " + _movieIdList[_movieIdListIndex];
//addChild(debug01);
if (_movieIdList.length > 0) {
_selectedMovieIdList.push(_movieIdList[_movieIdListIndex]);
//var debug012 : TextField = new TextField();
//debug012.x = 400;
//debug012.y = 160;
//debug012.text = " id : " + _selectedMovieIdList[0];
//addChild(debug012);
addToSelectedMovieList();
} else {
return;
}
}
private function addToSelectedMovieList():void {
var l : int = _selectedMovieIdList.length;
//var debug02 : TextField = new TextField();
//debug02.x = 700;
//debug02.y = 160;
//debug02.text = "debug02: l = " + l;
//addChild(debug02);
var col : int = (l - 1) % 5;
var row : int = (l - 1) / 5;
var playMovieButton : PlayMovieButton = new PlayMovieButton(l-1);
aButton[l-1] = playMovieButton;
addChild(aButton[l-1]);
playMovieButton.x = 470 + 100 * col;
playMovieButton.y = 40 + 30 * row;
}
private function urlLoadCompleteHandler(event : Event) : void {
_movieIdList = new Array();
// jsonデータをobjectに
_jsonObject = JSON.decode(event.currentTarget.data);
for (var i : String in _jsonObject['feed']['entry']) {
var movieId : String = String(_jsonObject['feed']['entry'][i]['id']['$t']).replace('http://gdata.youtube.com/feeds/api/videos/', '');
_movieIdList.push(movieId);
var url : String = "http://www.youtube.com/watch?v=" + movieId;
var thumbURL : String = _jsonObject['feed']['entry'][i]['media$group']['media$thumbnail'][0]['url'];
var title : String = _jsonObject['feed']['entry'][i]['title']['$t'];
var thum : ThumbImage = new ThumbImage(thumbURL, url, title, int(i)); //iの値はmovieIdListIndexの値に対応
var c : int = Math.ceil(_w / 134);
var cy : int = int(i) / c;
var cx : int = int(i) - cy * c;
var dx : Number = 134 * cx;
var dy : Number = 101 * cy + 200; //プレイヤーの高さ約200分増やす
thum.x = dx;
thum.y = dy;
_base.addChild(thum);
}
}
private function youtubePlayerLoadCompleteHandler(event : Event) : void {
_loader.content.addEventListener("onReady", onPlayerReady);
_loader.content.addEventListener("onError", onPlayerError);
_loader.content.addEventListener("onStateChange", onPlayerStateChange);
_loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}
private function onPlayerReady(event:Event):void {
_player = _loader.content;
_player.setSize(270, 180);
_isSelectedMoviePlaying = false;
if (_movieIdList.lenght < 1) _player.loadVideoById("lkHlnWFnA0c", 0, "default"); //動画IDを何も取得できなかった場合は公式ヘビーローテーションを再生します
_player.loadVideoById(_movieIdList[_movieIdListIndex], 0, "default"); //1曲目再生
}
private function onPlayerError(event:Event):void {
// Event.data contains the event parameter, which is the error code
}
private function onPlayerStateChange(event:Event):void {
// Event.data contains the event parameter, which is the new player state
//プレイヤーの状態を判定
//未開始(-1)、終了(0)、再生中(1)、一時停止中(2)、バッファリング中(3)、頭出し済み(5)
if (Object(event).data == "0") { //動画が終了した場合
if(!_isSelectedMoviePlaying) { //検索結果動画リストを再生
_movieIdListIndex++;
if(_movieIdListIndex > _movieIdList.length - 1) return; //動画IDリストを超えた場合は処理を行わない
_player.loadVideoById(_movieIdList[_movieIdListIndex], 0, "default");
} else if(_isSelectedMoviePlaying) { //選択・追加された動画再生リストを再生
_selectedMovieIdListIndex++;
if(_selectedMovieIdListIndex > _selectedMovieIdList.length - 1) return; //動画IDリストを超えた場合は処理を行わない
_player.loadVideoById(_selectedMovieIdList[_selectedMovieIdListIndex], 0, "default");
}
}
}
private function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
}
}
}
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.system.LoaderContext;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
class ThumbImage extends Sprite {
private var _img : String;
private var _url : String;
private var _title : String;
private var _index : int;
public function ThumbImage(imageURL : String, youtubeURL : String, title : String, movieIdListIndex : int) {
_img = imageURL;
_url = youtubeURL;
_title = title;
_index = movieIdListIndex;
buttonMode = true;
if (stage) initialize();
else addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(event : Event = null) : void {
removeEventListener(Event.ADDED_TO_STAGE, initialize);
// 画像のロード
var loader : Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
loader.load(new URLRequest(_img), new LoaderContext(true));
// マウスハンドラ
addEventListener(MouseEvent.CLICK, onMouseClick);
addEventListener(MouseEvent.ROLL_OVER, onRollOver);
addEventListener(MouseEvent.ROLL_OUT, onRollOut);
}
private function onRollOut(event : MouseEvent) : void {
var t:TestYoutube = this.parent.parent as TestYoutube;
t._bollon.hide();
}
private function onRollOver(event : MouseEvent) : void {
var t:TestYoutube = this.parent.parent as TestYoutube;
t._bollon.show(" " + (_index+1) + ". " +_title); //表示用に_indexに+1の加算を行う(indexの最初が0なので)
}
private function onMouseClick(event : MouseEvent) : void {
var t:TestYoutube = this.parent.parent as TestYoutube;
t._isSelectedMoviePlaying = false;
t._player.loadVideoById(t._movieIdList[_index], 0, "default");
t._movieIdListIndex = _index; //
}
private function loadCompleteHandler(event : Event) : void {
var bm : Bitmap = event.target.loader.content as Bitmap;
var scale : Number = 100 / bm.height;
bm.height = 100;
bm.scaleX = scale;
bm.smoothing = true;
addChild(bm);
}
}
class Balloon extends Sprite {
private var _tl : TextField;
private var _top : int;
private var _left : int;
private var _right : int;
private var _bottom : int;
private var _sp : Sprite;
public function Balloon(top : int, left : int, right : int, bottom : int) {
_sp = new Sprite();
_sp.graphics.lineStyle(0.1, 0xFFFFFF);
_sp.graphics.beginFill(0x000000);
_sp.graphics.drawRect(0, 0, 124, 20);
_sp.graphics.endFill();
addChild(_sp);
_tl = new TextField();
_tl.wordWrap = true;
_tl.autoSize = TextFieldAutoSize.LEFT;
_tl.width = 180;
_tl.textColor = 0xFFFFFF;
_tl.x = _tl.y = 2;
addChild(_tl);
_top = top;
_left = left;
_right = right;
_bottom = bottom;
visible = false;
mouseEnabled = false;
mouseChildren = false;
}
public function show(text : String) : void {
_tl.text = text;
_sp.width = _tl.width + 4;
_sp.height = _tl.textHeight + 8;
visible = true;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function hide() : void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
visible = false;
}
private function enterFrameHandler(event : Event) : void {
x = stage.mouseX + 5;
y = stage.mouseY + 5;
if (x > _right - width) x = x - width - 5;
if (y > _bottom - height) y = y - height - 5;
}
}
import flash.display.*;
import flash.system.*;
import flash.text.*;
//カスタムボタン
class CustomButton extends SimpleButton {
private var btnName : String = "";//ボタン名
private var btnNo : int = 0;//ボタン番号
//コンストラクタ
public function CustomButton(label:String="",no:int=0) {
btnName = label;
btnNo = no;
//状態
upState = makeSprite(label,0x99DDFF);
overState = upState;
downState = makeSprite(label,0x0000FF);
hitTestState = upState;
}
public function getBtnName():String {
return btnName;
}
public function getBtnNo():int {
return btnNo;
}
//ボタン用スプライト作成
private function makeSprite(text:String,color:uint):Sprite{
//ボタン用ラベル作成
var label : TextField = new TextField();
label.text = text;
label.autoSize = TextFieldAutoSize.CENTER;
label.selectable = false;
//ボタン用スプライト作成
var sp:Sprite = new Sprite();
sp.graphics.beginFill(color);
sp.graphics.drawRoundRect(0, 0, 100, 20, 15);
sp.graphics.endFill();
sp.alpha = 0.8;
sp.addChild(label);
//ラベル用フォーマット設定
var format:TextFormat=new TextFormat();
format.font = "Courier New";
format.bold = true;
format.size = 13;
label.setTextFormat(format);
return sp;
}
}
import flash.display.*;
import flash.system.*;
import flash.text.*;
import flash.events.Event;
import flash.events.MouseEvent;
//選択された動画リストから再生動画を指定するボタン
class PlayMovieButton extends SimpleButton {
private var _listIndex : int = 0; //リストインデックス
//コンストラクタ
public function PlayMovieButton(index:int) {;
_listIndex = index;
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
//状態
upState = makeSprite("ListNo:"+(index+1),0x99DDFF);
overState = upState;
downState = makeSprite("ListNo:"+(index+1),0x0000FF);
hitTestState = upState;
}
public function getListIndex():int {
return _listIndex;
}
private function onMouseDown(event : MouseEvent) : void {
var t:TestYoutube = this.parent as TestYoutube;
t._isSelectedMoviePlaying = true;
t._player.loadVideoById(t._selectedMovieIdList[_listIndex], 0, "default");
t._selectedMovieIdListIndex = _listIndex;
t._selectedMovieTitle.text = "selectedMovieIndex : " + _listIndex;
}
private function onMouseOver(event : MouseEvent) : void {
var t:TestYoutube = this.parent as TestYoutube;
t._selectedMovieTitle.text = "selectedMovieIndex : " + _listIndex;
}
private function onMouseOut(event : MouseEvent) : void {
var t:TestYoutube = this.parent as TestYoutube;
t._selectedMovieTitle.text = "";
}
//ボタン用スプライト作成
private function makeSprite(text:String,color:uint):Sprite{
//ボタン用ラベル作成
var label : TextField = new TextField();
label.text = text;
label.autoSize = TextFieldAutoSize.CENTER;
label.selectable = false;
//ボタン用スプライト作成
var sp:Sprite = new Sprite();
sp.graphics.beginFill(color);
sp.graphics.drawRoundRect(0, 0, 100, 20, 15);
sp.graphics.endFill();
sp.alpha = 0.8;
sp.addChild(label);
//ラベル用フォーマット設定
var format:TextFormat=new TextFormat();
format.font = "Courier New";
format.bold = true;
format.size = 13;
label.setTextFormat(format);
return sp;
}
}