-
* Main.fx
-
*
-
* Created on 9 oct. 2009, 20:18:51
-
*/
-
-
package youtubefx;
-
-
import javafx.stage.Stage;
-
-
import javafx.scene.Scene;
-
import javafx.scene.text.Text;
-
import javafx.scene.media.MediaPlayer;
-
import javafx.data.pull.PullParser;
-
import javafx.io.http.HttpRequest;
-
import javafx.data.pull.Event;
-
import javafx.scene.control.Button;
-
import javafx.scene.control.TextBox;
-
import javafx.scene.layout.VBox;
-
import javafx.scene.layout.HBox;
-
import javafx.scene.control.ListView;
-
import javafx.ext.swing.SwingListItem;
-
import javafx.scene.input.MouseEvent;
-
import javafx.scene.media.MediaView;
-
import javafx.scene.media.Media;
-
import javafx.scene.control.ProgressBar;
-
import javafx.scene.Group;
-
import javafx.scene.shape.Rectangle;
-
import javafx.scene.paint.Color;
-
import javafx.scene.CustomNode;
-
import javafx.scene.Node;
-
-
/**
-
* @author Gaetan Grigis
-
*/
-
class Splash extends CustomNode {
-
var rect = Rectangle {x:0,y:0,width: 400,height: bind scene.height,fill: Color.BLACK};
-
var txt = Text{content: "Hello this is just a simple test to use MediaView\n"
-
"and MediaPlayer with Youtube\n \n"
-
"As the support of streaming by JavaFX is new\n"
-
"sometimes it crash or doesn't work as expected\n"
-
"because of unsupported codec (video or music\n"
-
"isn't played in this case …)\n \n"
-
"(Click somewhere here to close this …\n"
-
"and try this …)",x: 50,y: 50,fill: Color.AQUAMARINE};
-
var groups = Group {
-
content: [rect,txt],
-
onMouseClicked: function( e: MouseEvent ):Void {rect.width=0;txt.content="";txt.x=0;txt.y=0}
-
}
-
-
override function create():Node {return groups;}
-
}
-
var item : SwingListItem[] = [];
-
var xml : PullParser = PullParser {documentType: PullParser.XML};
-
var mus = [];
-
//UI items
-
var scene = Scene {width: 400,height: 250};
-
var searchTxt = TextBox{width:bind (scene.width – 276),text: "Type your text .."};
-
var searchBtn = Button{text: "Search",width: 100,action: function(){searchItem(searchTxt.text)};};
-
var listView : ListView = ListView{width:bind (scene.width-176),height: bind (scene.height – searchTxt.height),items: bind item};
-
var SearchBox = HBox {content: [searchTxt,searchBtn],width: bind scene.width};
-
var PlayBox = VBox{content:[SearchBox,listView],width: bind (scene.width – 176),height: bind scene.height};
-
var player = MediaPlayer{autoPlay:true};
-
var info = Text {content: bind "{player.currentTime.toSeconds()}/{player.media.duration.toSeconds()} Sec.\nStatus : {player.status}\nClick on the progressBar\nif it doesn't start\n\tStatus 2 = playing\n\tStatus 0 = Loading/Crashed", };
-
var pg = ProgressBar{progress: bind ProgressBar.computeProgress(player.media.duration.toSeconds(),player.currentTime.toSeconds()),onMouseClicked:function(e:MouseEvent){if(player.status!=2){player.play();}else{player.pause();}}};
-
scene.content = HBox{content:[Splash{},PlayBox,VBox{content: [MediaView{visible:true, cache: true, mediaPlayer: player},info,pg]}]};
-
-
function searchItem(search: String)
-
{
-
item =
[];//on reset à la barbare
-
var connexion : HttpRequest = HttpRequest {
-
method: HttpRequest.GET
-
location: "http://gdata.youtube.com/feeds/api/videos?v=2&lr=en&format=6&orderby=published&safeSearch=strict&q={search}&max-results=50"
-
onInput: function(is: java.io.InputStream) {
-
try {
-
xml.input = is;
-
var it = SwingListItem{};
-
var i = 0;
-
xml.onEvent = function(event: Event) : Void{
-
if( event.qname.prefix == "media" )
-
{
-
if(event.qname.name == "content" and event.typeName == "START_ELEMENT" and event.getAttributeValue("type") == "video/3gpp" )
-
{
-
//can't get the yt:format, so as we know that mpeg is second we just increment …
-
it.value = event.getAttributeValue("url");
-
i++;
-
}
-
if(event.qname.name == "title" and event.text!="" and event.typeName == "TEXT"){it.text = event.text;}
-
if(it.text !="" and it.value!="" and i > 1 ){
-
insert it into item;it = SwingListItem{};
-
}
-
}
-
};
-
xml.parse();
-
} finally {is.close();}
-
}
-
};connexion.start();
-
}
-
-
listView.onMouseClicked = function (e:MouseEvent):Void{
-
player.stop();
-
listView.disable=true;
-
//hack to stop playing while another is loading
-
player.media.source = null;
-
player.media = null;
-
player.media = Media{source:item[listView.selectedIndex].value.toString(),};
-
player.play();
-
listView.disable=false;
-
}
-
Stage {title: "ytFX",scene: scene,onClose:function(){player.pause();player.media.source = null;player.media = null;}}