[JAVA] Download/Abspielen von SoundCloud

01/21/2014 18:52 Zunft#1
Benötigte Externe Bibliotheken:
[Only registered and activated users can see links. Click Here To Register...]
[Only registered and activated users can see links. Click Here To Register...]

Code:
import de.voidplus.soundcloud.*; //SoundCloud API
import ddf.minim.*; //Minim Libary als Medienwiedergabemodul
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Tobias H. (aka Zunft)
 * @date 21.01.2014 
 *
 */
public class SoundDownloader extends Thread
{

    
    static SoundCloud soundcloud;
    static Minim minim;
    static AudioPlayer player;
    static String[] urlParts = null;
    static ArrayList<Track> result = null;
   
    
    public static void downloadbutton()
    {
        soundcloud = new SoundCloud(CLIENT_ID, SECRET_KEY); //Tokens für CLIENT_ID und SECRET_KEY werden nach der Registrierung einer SoundCloud-Software angezeigt
        
        urlananlyzer(); //Url splitten / Track ID herausfinden
        
        result = soundcloud.findTrack(urlParts[4]); //Track ID suchen
        if(result!=null)
		{

        try
        {
            
            download(); 
            
        }
        catch(Exception e)
        {
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
			null,e.getMessage(), "Error",
			javax.swing.JOptionPane.DEFAULT_OPTION);
		}
	}
    }
    public static void download()
 {
     
     
    String site= result.get(0).getStreamUrl(); //Download-Adresse
    String filename=DATA_NAME; //Name, der herunterzuladenen Datei
    try 
	{
		//Download
        URL url = new URL(site);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int filesize = connection.getContentLength();
        float totalDataRead=0;
        java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
        java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
        java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
        byte[] data = new byte[1024];
        int i=0;
        while((i=in.read(data,0,1024))>=0)
        {
            totalDataRead=totalDataRead+i;
            bout.write(data,0,i);
            float Percent=(totalDataRead*100)/filesize;
            jProgressBar1.setValue((int)Percent);
        }  
		
        bout.close();
        in.close();
    }
    catch(Exception e)
    {
         javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null,e.getMessage(), "Error",
         javax.swing.JOptionPane.DEFAULT_OPTION);
    }
 }
    
    public void play()
    {
        soundcloud = new SoundCloud(CLIENT_ID, SECRET_KEY); //s.o.
        
        
        urlananlyzer(); //s.o.
        
        ArrayList<Track> result = soundcloud.findTrack(urlParts[4]); //s.o.
        if(result!=null)
		{
			//Wiedergabe
			minim = new Minim(this);
			player = minim.loadFile(result.get(0).getStreamUrl());
			player.play();
		}
    }
    
    public static void urlananlyzer()
	{
		urlParts = track.split("/"); //URL wird bei jedem '/' gesplittet.
	}
	
}