7

Video not working properly in the below mentioned code. What could be the problem for this?

MediaController mediaController = new MediaController(getBaseContext());
mediaController.setAnchorView(videoweb);

Uri video = Uri.parse("http://www.youtube.com/v/wwI2w2YHkCQ?fs=1");
videoweb.setMediaController(mediaController);
videoweb.setVideoURI(video);
videoweb.start();

Error:

Cannot play video
Sorry,this video cannot be played.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jack Dsilva
  • 1,504
  • 3
  • 24
  • 44
  • Can you post the logcat messages? – bluefalcon Jan 10 '12 at 08:42
  • 1
    @Ravi-error is-01-05 11:21:30.290: E/libEGL(2214): call to OpenGL ES API with no current context (logged once per thread) Warning-01-05 11:21:31.020: W/webcore(2214): Can't get the viewWidth after the first layout – Jack Dsilva Jan 10 '12 at 10:01
  • Non of the answers working for you? if you are still facing the problem, update us on what is the current issue you are facing. – Karthik Jan 16 '12 at 08:36

5 Answers5

6

The link you have provided, http://www.youtube.com/v/wwI2w2YHkCQ?fs=1, is for an HTML page. The URI to be provided for setVideoURI() should be a media file such as MP4 or AVI.

VideoView cannot parse a HTML page. It can only decode and play a video file or stream a video content (in this case the URI should point to the media file, such as http://people.sc.fsu.edu/~jburkardt/data/mp4/cavity_flow_movie.mp4). See Stack Overflow question Using VideoView for streaming or progressive-download video.

You should use a Webview for opening the YouTube link.

Community
  • 1
  • 1
Karthik
  • 3,509
  • 1
  • 20
  • 26
  • @Karthik-Yes you are right.. but i tried to use webview and loaded this url using loadurl function but nothing is displaying... any soln? – Jack Dsilva Jan 04 '12 at 11:59
  • Do you get any warnings or errors in Logcat? check internet access permission etc. – Karthik Jan 04 '12 at 11:59
  • @Karthik-I have given `INTERNET` permission in manifest and error is-01-05 11:21:30.290: E/libEGL(2214): call to OpenGL ES API with no current context (logged once per thread) Warning-01-05 11:21:31.020: W/webcore(2214): Can't get the viewWidth after the first layout – Jack Dsilva Jan 05 '12 at 06:00
  • What context are you passing while creating webview? Try using `this`. `getBaseContext()` will not be suitable here as only `this` (instance of `activity`) will have the view details. – Karthik Jan 05 '12 at 08:47
2

VideoView and Mediaplayer can play only formats given in the document Android Supported Media Formats.

The link of the YouTube video you gave is for an HTML page. If you are playing an HTML page as mentioned by Karthik, it is better to use Webview.

String url = "your_youtube_link";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

If you want only the video to be viewed from that link, leave all other details in the page. Suppose this is the YouTube link http://www.youtube.com/watch?v=ZC7ZOGpM2cU&feature=g-logo&context=G233b464FOAAAAAAABAA

If you parse the source of the YouTube HTML page, you may see this portion:

<link rel="alternate" type="application/json+oembed" href="http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DZC7ZOGpM2cU&amp;format=json" title="Bigg Boss 5: Juhi Parmar wins Big Boss season 5">
<link rel="alternate" type="text/xml+oembed" href="http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DZC7ZOGpM2cU&amp;format=xml" title="Bigg Boss 5: Juhi Parmar wins Big Boss season 5">
<meta property="fb:app_id" content="87741124305">
<meta property="og:url" content="http://www.youtube.com/watch?v=ZC7ZOGpM2cU">
<meta property="og:title" content="Bigg Boss 5: Juhi Parmar wins Big Boss season 5">
<meta property="og:description" content="Ntv News: Juhi Parmar wins Bigg Boss 5 - Juhi Parmar wins &#39;Bigg Boss 5&#39;, takes home Rs.1 crore - No kid for now, keen for good work: Juhi Parmar">
<meta property="og:type" content="video">
<meta property="og:image" content="http://i3.ytimg.com/vi/ZC7ZOGpM2cU/hqdefault.jpg">
<meta property="og:video" content="http://www.youtube.com/v/ZC7ZOGpM2cU?version=3&amp;autohide=1">
<meta property="og:video:type" content="application/x-shockwave-flash">
<meta property="og:video:width" content="396">
<meta property="og:video:height" content="297">
<meta property="og:site_name" content="YouTube">

In this, extract the following

<meta property="og:video" content="http://www.youtube.com/v/ZC7ZOGpM2cU?version=3&amp;autohide=1">

In this, the <http://www.youtube.com/v/ZC7ZOGpM2cU?version=3&amp;autohide=1> link will show it fullscreen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sonu thomas
  • 2,161
  • 4
  • 24
  • 38
1

Actually, in order to play any streaming video I am using

startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/v/wwI2w2YHkCQ?fs=1")));

But I don't think this is going to work for your link. Better to try

mediaplayer.setdataSource("your link").
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ranjit Mishra
  • 440
  • 4
  • 8
1

OK, try the following code. It will work.

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.youtube.com/v/wwI2w2YHkCQ?fs=1"));
startActivity(i);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ranjit Mishra
  • 440
  • 4
  • 8
0
private VideoView mVideoView;

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.main);
        mVideoView = (VideoView) findViewById(R.id.surface_view);
    }

    private void playVideo() {
        try {
            final String path = "http://www.youtube.com/v/wwI2w2YHkCQ?fs=1"

            System.out.println("path  "+path);
            Log.v(TAG, "path: " + path);
            if (path == null || path.length() == 0) {
                Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                        Toast.LENGTH_LONG).show();
            }
            else {
                System.out.println("else  ");
                // If the path has not changed, just start the media player
                if (path.equals(current) && mVideoView != null) {
                    System.out.println("mVideoView.start()  ");

                    mVideoView.start();
                    mVideoView.requestFocus();
                    return;
                }
                current = path;
                //mVideoView.setVideoPath(getDataSource(path));
                mVideoView.setVideoURI(Uri.parse(path));
                mVideoView.start();
                mVideoView.requestFocus();
            }
        }
        catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
        }
    }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
>
    <EditText android:id="@+id/path"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
            />

    <LinearLayout android:orientation="horizontal"
                  android:layout_height="wrap_content"
                  android:layout_width="fill_parent"
    >
        <ImageButton android:id="@+id/play"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/play"/>

        <ImageButton android:id="@+id/pause"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/pause"/>
        <ImageButton android:id="@+id/reset"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/reset"/>
        <ImageButton android:id="@+id/stop"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/stop"/>
    </LinearLayout>

    <VideoView android:id="@+id/surface_view"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent">
    </VideoView>
</LinearLayout>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jennifer
  • 8,133
  • 22
  • 69
  • 96