Wednesday, March 16, 2011

Clip Art Of A Sandrail

upload videos to YouTube

upload videos to YouTube

As you know Youtube is one of the main page to view and share videos worldwide. Google which owns YouTube has made a Java library for developers to access the database.

Google has libraries for almost every product they own, this time we only concentrate on Youtube, now we look like uploading videos but soon we will see how to find, see, among other functions.

The libraries of Google's available for download from their official website here .


Images







Code


Top Class



package classes;


import javax.swing.JFrame;


public class Main {

public static void main (String [] args) {


FrameConexion FrameConexion connect = new ();
conec.setLocationRelativeTo (null);
connected. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}}



Class FrameConexion



Classes package;


import java.awt . BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.* ;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.util.AuthenticationException;


public class FrameConexion extends JFrame {

JTextField txtusuario;
JPasswordField txtclave;
JButton btnconectar;

public FrameConexion(){

pdata section size = new JPanel JPanel ();

txtusuario = new JTextField (20);
txtclave = new JPasswordField (20);

btnconectar = new JButton ("Conectarme");
btnconectar.addActionListener (new ActionListener () {


@ Override public
void actionPerformed (ActionEvent e) {
YouTubeService servicio = new YouTubeService("gdataSample-YouTubeAuth-1","AI39si7EmLz9EO7wKpwCcM6cfXxvc9mAvS8T6pq4Qc-t_tkUq8pLpyNyBmo9lz4wavbgbsCMjz_yeH7_FoY0-94kUkRPfCk9KQ");
boolean sw=true;
   try {
     servicio.setUserCredentials(txtusuario.getText(), new String(txtclave.getPassword()));
   } catch (AuthenticationException ex) {
    JOptionPane.showMessageDialog (null, "Error, invalid username or password.");
sw = false;
}
if (sw) {
FrameSubir FrameSubir p = new (service);
FrameConexion.this.setVisible (false);

}
}

});

pdatos.add (new JLabel ("User: "));
pdatos.add (txtusuario)
pdatos.add (new JLabel (" Password :"));
pdatos.add (txtclave)


add (pdata);
add (btnconectar, BorderLayout.SOUTH)

setVisible (true);
setLocationRelativeTo (null);
setBounds (0, 0, 330, 150);
}}



Class FrameSubir



Classes package;


java.awt.BorderLayout import, import
java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;


import javax.swing.* ;


import com.google.gdata.client.media.ResumableGDataFileUploader;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;


public class FrameSubir extends JFrame {

JButton btnseleccionar,btnsubir;
File file;
String user, password;
ArrayList \u0026lt; JProgressBar> bar;
YouTubeService service;

public FrameSubir (YouTubeService serv) {

setLayout (new BoxLayout (this.getContentPane (), BoxLayout.Y_AXIS));
bars = new ArrayList \u0026lt; JProgressBar> ();
servicio=serv;

JPanel pbotones=new JPanel();
ImageIcon imgbtnseleccionar=new ImageIcon(this.getClass().getResource("../Imagenes/buscar.png"));
btnseleccionar=new JButton("Seleccionar",imgbtnseleccionar);
btnseleccionar.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
JFileChooser selector = new JFileChooser ();
selector.showOpenDialog int r = (FrameSubir.this)
if (r == JFileChooser.APPROVE_OPTION) {
selector.getSelectedFile file = ( )
}
btnsubir.setEnabled (true);
}
});
imgbtnsubir = new ImageIcon ImageIcon (this.getClass () . getResource ("../ Images / subir.png "));
btnsubir = new JButton (" Top ", imgbtnsubir)
btnsubir.setEnabled(false);
btnsubir.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
JPanel p=new JPanel(new BorderLayout());
JProgressBar barraprogreso=new JProgressBar(0,100);
barraprogreso.setStringPainted(true);
barras.add(barraprogreso);
ImageIcon imgvideo=new ImageIcon(this.getClass().getResource("../Imagenes/Video.png"));
p.add(new JLabel(archivo.getName(),imgvideo,JLabel.CENTER));
p.add(barraprogreso,BorderLayout.SOUTH);
FrameSubir.this.add(p,BorderLayout.SOUTH);
HiloUploader hu=new HiloUploader(FrameSubir.this,barras.size());
hu.start();
p.updateUI();
repaint();
}
});
pbotones.add(btnseleccionar);
pbotones.add(btnsubir);


add(pbotones,BorderLayout.NORTH);

setVisible(true);
setBounds(0, 0, 400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}


obtenerExtension public String () {
archivo.getAbsolutePath String path = ();
return ruta.substring (ruta.length () -3, ruta.length ()) ;
}}



Class Progress



package classes;


import javax.swing.JProgressBar;


import com.google.gdata.client.uploader.ProgressListener;
import com.google.gdata.client.uploader.ResumableHttpFileUploader;


public class Progress {implements ProgressListener

JProgressBar bar;
int total = 0;

public Progress (JProgressBar b) {
bar = b;
}

public synchronized void ProgressChanged (ResumableHttpFileUploader uploader) {

switch (uploader.getUploadState ()) {
houses COMPLETE:
; barra.setString (Completado ");
break;
houses CLIENT_ERROR:
  barra.setString("Error");
        break;
      case IN_PROGRESS:
      barra.setValue((int)(uploader.getProgress()*100));
      barra.setString(String.format("%3.0f", uploader.getProgress() * 100) + "%");
      break;
case NOT_STARTED:
barra.setString ("Not Started");
break;

}}}




Class HiloUploader



Classes package;


import java.io.IOException;
import java.net.URL;


import javax.swing.JOptionPane;


import com.google.gdata.client.media.ResumableGDataFileUploader;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.ServiceException;


public class HiloUploader extends Thread {

FrameSubir fs;
int indice=0;

public HiloUploader(FrameSubir frames,int ind){
fs=frames;
index = ind;
}

public void run () {
try {
subirVideo (fs.servicio)
} catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} catch (ServiceException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}

private void subirVideo (YouTubeService service) throws IOException, ServiceException, InterruptedException {


if (! fs.archivo.exists ()) {
JOptionPane.showMessageDialog (null, "Error, file not found.");
return;
}


MediaFileSource ms = new MediaFileSource (fs.archivo, fs.obtenerExtension ()) ;


videoTitle = fs.archivo.getName String ();


VideoEntry VideoEntry newEntry = new ();
newEntry.getOrCreateMediaGroup YouTubeMediaGroup mg = ( )
mg.addCategory (new MediaCategory (YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
mg.setTitle (new MediaTitle ());
mg.getTitle (). SetPlainTextContent (videoTitle);
mg.setKeywords (new MediaKeywords () );
mg.getKeywords (). addKeyword ("gdata-test);
mg.setDescription (new MediaDescription ());
mg.getDescription (). setPlainTextContent ( videoTitle);


Progress progress = new Progress (fs.barras.get (index-1));
ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder (service, new URL ("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads), ms, newEntry.) Title (videoTitle) . trackProgress (Progress, 1000). chunksize (10000000). build ();
uploader.start ();
while (! uploader.isDone ()) {
Thread. Sleep (1000);
}


switch (uploader.getUploadState ()) {
almost complete:
JOptionPane.showMessageDialog (null, "File uploaded successfully.");
break;
case CLIENT_ERROR:
JOptionPane.showMessageDialog (null, "Failed to upload the file. ");
break;
default:
JOptionPane.showMessageDialog (null," Unexpected Error ");
break;
}
}}

Class Principal
all it does is call FrameConexion, so that the user logs in with your YouTube or Google.

FrameConexion class connects the user to Youtube, thanks to YoutubeService class and then calls FameSubir class.

FrameSubir class creates the buttons, one to select the file and one to upload it to Youtube. Also in this window is where you put the video information and the progress bar that tells the user how the process. When the user presses the up button, you create a class object HiloUploader which is the one who upload the video.

Progress class, what it does is return the value of the percentage of the entire process and tell us when you have finished uploading the video.

Download

The project can be downloaded from here , including the source code files. Class, catches and the executable.

0 comments:

Post a Comment