Android 如何将Youtube URL转换为RTSP URL?

Android 如何将Youtube URL转换为RTSP URL?,android,youtube,android-mediaplayer,rtsp,Android,Youtube,Android Mediaplayer,Rtsp,在我的项目中,我想下载一个Youtube URL并在media player中播放,但就我搜索的结果而言,我认为只有将Youtube URL转换为RTSP文件才能播放。我不知道该怎么做。这将是非常有帮助的,如果你给我一个样本项目 public class Video_Media_PlayerActivity extends Activity { private static final String TAG = "VideoViewDemo"; private VideoVie

在我的项目中,我想下载一个Youtube URL并在media player中播放,但就我搜索的结果而言,我认为只有将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;
        }

    }

}
public class Video\u Media\u player活动扩展活动{
私有静态最终字符串标记=“VideoViewDemo”;
私有视频视图;
私人编辑文本mPath;
私有图像按钮mPlay;
私人使用;
专用图像按钮mReset;
私人图像按钮mStop;
私有串电流;
字符串临时路径;
私有VideoMediaViewApplication appObj;
私人质量=80;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mVideoView=(VideoView)findviewbyd(R.id.surface\u视图);
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);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
播放视频();
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
mVideoView.pause();
}
}
});
mReset.setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
mVideoView.seekTo(0);
}
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
电流=零;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(新的Runnable(){
公开募捐{
播放视频();
}
});
}
私人void playVideo(){
试一试{
最终字符串路径=mPath.getText().toString();
Log.v(标签,“路径:”+path);
if(path==null | | path.length()==0){
Toast.makeText(Video\u Media\u PlayerActivity.this,“文件URL/路径为空”,
Toast.LENGTH_LONG).show();
}否则{
//如果路径没有改变,只需启动媒体播放器
if(path.equals(current)&&mVideoView!=null){
mVideoView.start();
mVideoView.requestFocus();
返回;
}
电流=路径;
setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
}捕获(例外e){
Log.e(标记“error:+e.getMessage(),e);
如果(mVideoView!=null){
mVideoView.stopPlayback();
}
}
}
私有字符串getDataSource(字符串路径)引发IOException{
如果(!URLUtil.isNetworkUrl(路径)){
返回路径;
}否则{
URL=新URL(路径);
URLConnection cn=url.openConnection();
cn.connect();
InputStream=cn.getInputStream();
if(流==null)
抛出新的RuntimeException(“流为null”);
//prepareDirectory();
//文件file1=新文件(路径);
File file1=File.createTempFile(“mediaplayertmp”、“dat”);
file1.deleteOnExit();
tempPath=file1.getAbsolutePath();
FileOutputStream out=新的FileOutputStream(file1);
字节buf[]=新字节[128];
做{
int numread=stream.read(buf);
如果(numread使用api


我可以给你一个名字:JDownloader是一个关于下载管理器的开源项目,也是用Java编写的,它在从Youtube下载视频时工作得非常好,也许你可以下载源代码并搜索正确的模块,或者在他们的论坛上询问
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);
        }
    }