2011-11-28 102 views
0

在我的項目中,我想下載Youtube網址並在媒體播放器中播放它,但就我搜索的內容而言,我有一個想法,即Youtube網址只有在它們被轉換爲RTSP文件時才能播放。我不知道如何做到這一點。如果你給我一個樣本項目,這將是非常有幫助的。如何將YouTube網址轉換爲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

我可以給你一個名字:JDownloader是關於一個下載管理器的開源項目,也是用Java編寫的,並從Youtube下載視頻時,它完美的作品,也許你可以下載源代碼並搜索正確的模塊或者在他們的論壇中提問。 jdownloader.org – Micro

回答

1

使用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

這將工作正常 –