如何将Youtube URL转换为RTSP URL?

时间:2011-11-28 12:03:25

标签: android youtube android-mediaplayer rtsp

在我的项目中,我想下载一个Youtube URL并在媒体播放器中播放它但是到目前为止我已经搜索了一下,我知道Youtube 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;
        }

    }

}

1 个答案:

答案 0 :(得分: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);
        }
    }