Search code examples
androidyoutube-apilive-streamingandroid-youtube-api

How to live stream my content to android application?


I'm using youtube live to stream my sports event on android mobile application. But it not a viable option because whenever I stop the stream and start a new one, the video code of youtube live stream changes, so I cannot stream again if I stop the current stream. Below is the code for my stream :

youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtube_player_view);
    onInitializedListener = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.play();
            youTubePlayer.setFullscreen(true);
            youTubePlayer.loadVideo("video-code");

        }

Is there any method to put the new video code automatically using youtube API? Please help.


Solution

  • Since nobody answered my question, I'll answer it on my own.

    Instead of using youtube API, we can use a webview. No instead of using a video-code to stream youtube live, we can use channelID in the youtube link to permanently embed the live stream to the webview.

    Here is the code for it:

    public class WebViewPlayer extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web_view_player);
    
            String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"400\" height=\"290\" src=\"https://www.youtube.com/embed/live_stream?channel=YOUR_CHANNEL_ID\" frameborder=\"0\" allowfullscreen=\"true\"></iframe></body></html>";
    
            WebView webView = (WebView)findViewById(R.id.webview);
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
            });
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webView.loadData(frameVideo, "text/html", "utf-8");
        }
    }