注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

抓取豆瓣FM音乐进行批量下载(二)  

2011-10-23 14:01:23|  分类: Javafx |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
上次提到如何抓取豆瓣FM的音乐列表。
这次我们有必要,给它提供一个简单的JavaFX2.0  GUI
因为写的比较匆忙,只是一个简单的List的列表,可以use "Next Music List“ Button 进行下一次抓取,当你用鼠标点击List选项时,可以播放该音乐。

1.一进来,显示音乐List,用鼠标点击自己喜欢的歌,进行播放。

抓取豆瓣FM音乐进行批量下载(二) - 星期五 - 星期五
 
2.可以使用Next Music List 进行下一次抓取。(开始听 郭静 那首心墙)

抓取豆瓣FM音乐进行批量下载(二) - 星期五 - 星期五
 
功能是比较简单啦。

Show My Code

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package getdoubanmusic;

import getdoubanmusic.getmusic.GetMusic;
import getdoubanmusic.getmusic.Song;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author hjzheng
 */
public class GetDoubanMusic extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {

        primaryStage.setTitle("Music List");
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0, 0, 0, 0));
        Scene scene = new Scene(vBox, 400, 500);

        Button btn = new Button();
        btn.setMaxWidth(Double.MAX_VALUE);
        btn.setText("Next Music List");
        btn.setAlignment(Pos.BASELINE_CENTER);

        vBox.getChildren().add(btn);

        final GetMusic gm = new GetMusic();
        final ListView<Text> lvList = new ListView<Text>();
        final List<MediaPlayer> mpList = new ArrayList<MediaPlayer>();
        
        try {
            List<Song> list = gm.getSongs();
            ObservableList<Text> items = FXCollections.observableArrayList();
          
           
            for(Song song : list){
                 mpList.add(createMediaPlayer(song.getUrl()));
            }
           
            for (final Song song : list) {
                Text txt = new Text();
                txt.setText(song.getTitle());
             
                txt.setOnMouseClicked(new EventHandler<MouseEvent>() {

                    public void handle(MouseEvent t) {
                        //load music url
                        stopAllMusic(mpList);
                        playMusic(mpList, song.getUrl());
                        primaryStage.setTitle("Music List  " + song.getTitle() + "  playing ...");
                    }
                });
                items.add(txt);
            }


            lvList.setItems(items);
            lvList.setMinHeight(500.0);
            lvList.setMaxHeight(Double.MAX_VALUE);

            vBox.getChildren().add(lvList);
        } catch (Exception ex) {
            Logger.getLogger(GetDoubanMusic.class.getName()).log(Level.SEVERE, null, ex);
        }

        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                stopAllMusic(mpList);
                mpList.clear();
                ObservableList<Text> items = FXCollections.observableArrayList();
                try {
                    List<Song> list = gm.getSongs();
                    for(Song song : list){
                        mpList.add(createMediaPlayer(song.getUrl()));
                    }
            
                    for (final Song song:list) {
                        Text txt = new Text();
                        txt.setText(song.getTitle());
                       
                        txt.setOnMouseClicked(new EventHandler<MouseEvent>() {
                            public void handle(MouseEvent t) {
                                //load music url
                                stopAllMusic(mpList);
                                playMusic(mpList, song.getUrl());
                                primaryStage.setTitle("Music List  " + song.getTitle() + "  playing ...");
                            }
                        });
                       
                        items.add(txt);
                        lvList.setItems(items);
                        lvList.setMinHeight(500.0);
                        lvList.setMaxHeight(Double.MAX_VALUE);
                       
                    }
                } catch (Exception ex) {
                    Logger.getLogger(GetDoubanMusic.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public MediaPlayer createMediaPlayer(String url) {
        Media media = new Media(url);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.stop();
        return mediaPlayer;
    }

    public void stopAllMusic(List<MediaPlayer> list) {
        for (MediaPlayer mp : list) {
            mp.stop();
        }
    }
   
    public void playMusic(List<MediaPlayer> list, String url) {
        for(MediaPlayer m: list){
            if(m.getMedia().getSource().equals(url)){
                m.play();
            }
        }
    }
   
     public void stopMusic(List<MediaPlayer> list, String url) {
        for(MediaPlayer m: list){
            if(m.getMedia().getSource().equals(url)){
                m.stop();
            }
        }
    }
}
  评论这张
 
阅读(906)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017