Как конвертировать URL Youtube в RTSP URL? - PullRequest
0 голосов
/ 28 ноября 2011

В моем проекте я хочу загрузить URL-адрес Youtube и воспроизвести его в медиапроигрывателе, но, насколько я искал, у меня возникла мысль, что URL-адреса 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;
        }

    }

}

1 Ответ

1 голос
/ 08 ноября 2012

Использование 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);
        }
    }
...