2011-11-28 4 views
0

Dans mon projet, je veux télécharger une URL Youtube et la lire dans un lecteur multimédia, mais comme je l'ai cherché, j'ai eu l'idée que les URL Youtube ne peuvent être lues que si elles sont converties en fichier RTSP. Je n'ai aucune idée de comment faire ça. Ce serait très utile si vous m'envoyez un exemple de projet.Comment convertir une URL Youtube en URL RTSP?

public class Video_Media_PlayerActivity extends Activity { 
    private static final String TAG = "VideoViewDemo"; 

    private VideoView mVideoView; 
    private EditText mPath; 
    private ImageButton mPlay; 
    private ImageButton mPause; 
    private ImageButton mReset; 
    private ImageButton mStop; 
    private String current; 
    String tempPath; 
    private VideoMediaViewApplication appObj; 
    private int mVideoQuality = 80; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mVideoView = (VideoView) findViewById(R.id.surface_view); 

     mPath = (EditText) findViewById(R.id.path); 
     mPath.setText("http://daily3gp.com/vids/747.3gp"); 

     mPlay = (ImageButton) findViewById(R.id.play); 
     mPause = (ImageButton) findViewById(R.id.pause); 
     mReset = (ImageButton) findViewById(R.id.reset); 
     mStop = (ImageButton) findViewById(R.id.stop); 

     mPlay.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       playVideo(); 

      } 
     }); 
     mPause.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mVideoView != null) { 
        mVideoView.pause(); 
       } 
      } 
     }); 
     mReset.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mVideoView != null) { 
        mVideoView.seekTo(0); 
       } 
      } 
     }); 
     mStop.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mVideoView != null) { 
        current = null; 
        mVideoView.stopPlayback(); 
       } 
      } 
     }); 
     runOnUiThread(new Runnable(){ 
      public void run() { 
       playVideo(); 


      } 

     }); 
    } 

    private void playVideo() { 
     try { 
      final String path = mPath.getText().toString(); 
      Log.v(TAG, "path: " + path); 
      if (path == null || path.length() == 0) { 
       Toast.makeText(Video_Media_PlayerActivity.this, "File URL/path is empty", 
         Toast.LENGTH_LONG).show(); 

      } else { 
       // If the path has not changed, just start the media player 
       if (path.equals(current) && mVideoView != null) { 
        mVideoView.start(); 
        mVideoView.requestFocus(); 
        return; 

       } 
       current = path; 
       mVideoView.setVideoPath(getDataSource(path)); 
       mVideoView.start(); 
       mVideoView.requestFocus(); 

      } 
     } catch (Exception e) { 
      Log.e(TAG, "error: " + e.getMessage(), e); 
      if (mVideoView != null) { 
       mVideoView.stopPlayback(); 
      } 
     } 
    } 

    private String getDataSource(String path) throws IOException { 
     if (!URLUtil.isNetworkUrl(path)) { 
      return path; 
     } else { 
      URL url = new URL(path); 
      URLConnection cn = url.openConnection(); 
      cn.connect(); 
      InputStream stream = cn.getInputStream(); 
      if (stream == null) 
       throw new RuntimeException("stream is null"); 
      //prepareDirectory(); 
      //File file1 = new File(path); 
      File file1 = File.createTempFile("mediaplayertmp", "dat"); 
      file1.deleteOnExit(); 
      tempPath = file1.getAbsolutePath(); 
      FileOutputStream out = new FileOutputStream(file1); 
      byte buf[] = new byte[128]; 
      do { 
       int numread = stream.read(buf); 
       if (numread <= 0) 
        break; 
       out.write(buf, 0, numread); 
      } while (true); 
      try { 
       stream.close(); 
      } catch (IOException ex) { 
       Log.e(TAG, "error: " + ex.getMessage(), ex); 
      } 
      return tempPath; 
     } 

    } 

} 
+0

je peux vous donner un nom: JDownloader est un projet opensource d'un gestionnaire de téléchargement, est également écrit en Java, et il fonctionne parfaitement lors du téléchargement de la vidéo de Youtube, peut-être vous pouvez télécharger la source et rechercher le bon module ou demander dans leur forum. jdownloader.org – Micro

Répondre

1

Utilisez http://gdata.youtube.com/feeds/mobile/videos/T9W4jgq9RfQ api

public class MainActivity extends Activity { 
String video_id; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     try {  
     String link = "http://www.youtube.com/watch?v=T9W4jgq9RfQ&feature=related"; 
      String pattern = "(?:videos\\/|v=)([\\w-]+)"; 

      Pattern compiledPattern = Pattern.compile(pattern); 
      Matcher matcher = compiledPattern.matcher(link); 

      if(matcher.find()){ 
       System.out.println(matcher.group().substring(2)); 
video_id=matcher.group().substring(2); 
      } 

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 


      Document doc1=db.parse("http://gdata.youtube.com/feeds/mobile/videos/"+video_id); 

      Element rsp = (Element)doc1.getElementsByTagName("media:content").item(1); 

      String anotherurl=rsp.getAttribute("url"); 
      System.out.println("my "+anotherurl); 
     // Element rsp = (Element)doc1.getElementsByTagName("media:content").item(1); 
     } catch (Exception e) { 
      System.out.println("Pasing Excpetion = " + e); 
     } 
    } 
+1

Cela fonctionnera bien –