New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play youtube video #1033

Closed
shaishavs opened this Issue Feb 7, 2017 · 5 comments

Comments

Projects
None yet
2 participants
@shaishavs

shaishavs commented Feb 7, 2017

Is there any way to play youtube video from url (e.g https://www.youtube.com/watch?v=55q9W6stwP0) ?

I am using mediaplayer and trying to load the video using mediaplyer.setDataSource(url); but getting IOException prepare failed.

P.S: If I use urls other than from youtube (e.g http://html5demos.com/assets/dizzy.mp4) and then do mediaplayer.setDataSource(url); then it works normally, but gives the error while trying to play youtube videos.

@liaxim liaxim added the help wanted label Feb 7, 2017

@liaxim

This comment has been minimized.

Show comment
Hide comment
@liaxim

liaxim Feb 7, 2017

Contributor

Hi. http://stackoverflow.com/questions/11550797/playing-youtube-video-in-android-app contains good tips. When it comes specifically to GVRF, the RTSP URL method and MediaPlayer is the only way it may possibly work.

While we can show a WebView, there is no known way to us to have it successfully play YouTube videos.

The official YouTube API requires your activity to derive from theirs which makes using it with GVRF impossible.

Contributor

liaxim commented Feb 7, 2017

Hi. http://stackoverflow.com/questions/11550797/playing-youtube-video-in-android-app contains good tips. When it comes specifically to GVRF, the RTSP URL method and MediaPlayer is the only way it may possibly work.

While we can show a WebView, there is no known way to us to have it successfully play YouTube videos.

The official YouTube API requires your activity to derive from theirs which makes using it with GVRF impossible.

@shaishavs

This comment has been minimized.

Show comment
Hide comment
@shaishavs

shaishavs Feb 8, 2017

@liaxim thanks for the reply! I've got it working using the RTSP way. For extracting the RTSP url from youtube url I've used this github class.

Here is the snippet of my Scene which adds GVRVideoSceneObject by taking MediaPlayer instance.

// Here **YouTubeExtractorListener** and **YouTubeExtractor** are from above github link.
public class VideoPlayerScene extends GVRScene implements YouTubeExtractor.YouTubeExtractorListener {
    private final float PLAYER_WIDTH = 4.0f;
    private final float PLAYER_HEIGHT = 2.0f;
    private final float PLAYER_POSITION_Z = -5.0f;

    private GVRContext mContext;
    private MediaPlayer mediaPlayer;

    public VideoPlayerScene(GVRContext gvrContext, String url) {
        super(gvrContext);
        this.mContext = gvrContext;

        // Here url would be something like https://www.youtube.com/watch?v=55q9W6stwP0 and we'll extract videoId from it by simply using substring function.
        if(url != null && !url.contentEquals("")) {
            int startIndex = url.indexOf("=");
            String videoId = url.substring(startIndex + 1, url.length());
            getRtspLink(videoId);
        }
    }

    private void getRtspLink(String videoId) {
        YouTubeExtractor youTubeExtractor = new YouTubeExtractor(videoId);
        youTubeExtractor.startExtracting(this);
    }

    private void createMediaPlayer(String url) {
        Log.d("---URL", " : " + url);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d("---onPrepared", " Prepared ");
                mediaPlayer.start();
            }
        });

        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("---Exception", " : " + e.toString());
        }
    }

    private void createVideoPlayerSceneObject() {
        GVRVideoSceneObject videoSceneObject = new GVRVideoSceneObject(mContext, PLAYER_WIDTH, PLAYER_HEIGHT,
                mediaPlayer, GVRVideoSceneObject.GVRVideoType.MONO);
        videoSceneObject.getTransform().setPosition(0.0f, 0.0f, PLAYER_POSITION_Z);
        addSceneObject(videoSceneObject);
    }

    @Override
    public void onSuccess(YouTubeExtractor.YouTubeExtractorResult result) {
        createMediaPlayer(result.getVideoUri().toString());
        createVideoPlayerSceneObject();
    }

    @Override
    public void onFailure(Error error) {
        error.printStackTrace();
    }
}

shaishavs commented Feb 8, 2017

@liaxim thanks for the reply! I've got it working using the RTSP way. For extracting the RTSP url from youtube url I've used this github class.

Here is the snippet of my Scene which adds GVRVideoSceneObject by taking MediaPlayer instance.

// Here **YouTubeExtractorListener** and **YouTubeExtractor** are from above github link.
public class VideoPlayerScene extends GVRScene implements YouTubeExtractor.YouTubeExtractorListener {
    private final float PLAYER_WIDTH = 4.0f;
    private final float PLAYER_HEIGHT = 2.0f;
    private final float PLAYER_POSITION_Z = -5.0f;

    private GVRContext mContext;
    private MediaPlayer mediaPlayer;

    public VideoPlayerScene(GVRContext gvrContext, String url) {
        super(gvrContext);
        this.mContext = gvrContext;

        // Here url would be something like https://www.youtube.com/watch?v=55q9W6stwP0 and we'll extract videoId from it by simply using substring function.
        if(url != null && !url.contentEquals("")) {
            int startIndex = url.indexOf("=");
            String videoId = url.substring(startIndex + 1, url.length());
            getRtspLink(videoId);
        }
    }

    private void getRtspLink(String videoId) {
        YouTubeExtractor youTubeExtractor = new YouTubeExtractor(videoId);
        youTubeExtractor.startExtracting(this);
    }

    private void createMediaPlayer(String url) {
        Log.d("---URL", " : " + url);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d("---onPrepared", " Prepared ");
                mediaPlayer.start();
            }
        });

        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("---Exception", " : " + e.toString());
        }
    }

    private void createVideoPlayerSceneObject() {
        GVRVideoSceneObject videoSceneObject = new GVRVideoSceneObject(mContext, PLAYER_WIDTH, PLAYER_HEIGHT,
                mediaPlayer, GVRVideoSceneObject.GVRVideoType.MONO);
        videoSceneObject.getTransform().setPosition(0.0f, 0.0f, PLAYER_POSITION_Z);
        addSceneObject(videoSceneObject);
    }

    @Override
    public void onSuccess(YouTubeExtractor.YouTubeExtractorResult result) {
        createMediaPlayer(result.getVideoUri().toString());
        createVideoPlayerSceneObject();
    }

    @Override
    public void onFailure(Error error) {
        error.printStackTrace();
    }
}
@liaxim

This comment has been minimized.

Show comment
Hide comment
@liaxim

liaxim Feb 8, 2017

Contributor

Awesome. How do you find the quality? It was mentioned that the RTSP method gets a lower-res version.

Contributor

liaxim commented Feb 8, 2017

Awesome. How do you find the quality? It was mentioned that the RTSP method gets a lower-res version.

@shaishavs

This comment has been minimized.

Show comment
Hide comment
@shaishavs

shaishavs Feb 9, 2017

@liaxim Well in my experience I found the quality to be nice! and in the method I have mentioned to find the RTSP link, there is also an option to set the preferred quality for the video. I think you should test this method and if it works in most cases then add an example or method in library class to make it easier for others who wants to achieve the same.

shaishavs commented Feb 9, 2017

@liaxim Well in my experience I found the quality to be nice! and in the method I have mentioned to find the RTSP link, there is also an option to set the preferred quality for the video. I think you should test this method and if it works in most cases then add an example or method in library class to make it easier for others who wants to achieve the same.

@liaxim

This comment has been minimized.

Show comment
Hide comment
@liaxim

liaxim Feb 9, 2017

Contributor

Thanks. For now we can definitely add more entry to our Q&A section (https://github.com/Samsung/GearVRf/wiki/Q&A).

Contributor

liaxim commented Feb 9, 2017

Thanks. For now we can definitely add more entry to our Q&A section (https://github.com/Samsung/GearVRf/wiki/Q&A).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment