Search code examples
androidandroid-fragmentsvideo-streaminglive-streaming

How to stream a live video from a URL?


I want to embed this link in my android application. I tried doing this, but the video won't load. Im also getting an error java.io.FileNotFoundException: No content provider:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_cctv, container, false);

    try {
        String cctv = "http://121.58.202.110:8080/jpeg?cam=4";
        VideoView videoView = getView().findViewById(R.id.cctvfragment);
        MediaController mediaController = new MediaController(getActivity());
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(cctv);
        videoView.setMediaController(null);
        videoView.setVideoURI(video);
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

            }
        });
    } catch (Exception e) {
        // TODO: handle exception
        Log.d("CctvDebug", "ERROR LOADING VIDEO");
    }
    return view;
}

Solution

  • On the question, Native Video View is very limited when it comes to urls that are newer and is (*i think) no longer updated. try using the Vitamio video player instead. It is very useful for a variety of streaming functionalities.

    <io.vov.vitamio.widget.VideoView
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/VideoView"/>        
    

    Also, do not call start() method right away. Only call it on your OnPreparedListener() callback.

    Vitamio.isInitialized(getApplicationContext());
    
    mVideoView.setVideoPath(path);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.requestFocus();
    
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            // optional need Vitamio 4.0
            mediaPlayer.setPlaybackSpeed(1.0f);
        }
    });
    

    Or you could also use KickFlip. It's very easy to setup,

    Kickflip.initWithApiKey(API_KEY, API_SECRET);
    Kickflip.startBroadcastActivity(this, mBroadcastListener);
    

    Edit:

    So I tried working on the stream since yesterday. I wasn't able to work it out on vitamio (Unfortunately). Though tried using the url you gave and streamed it through the vlc app. Though I haven't tried it, you might be able to use LibVLC . I ended up using webview with some settings so that it will not look like its using webview.

    webView = findViewById(R.id.wvWebview);
    
    webView.loadUrl("http://121.58.202.110:8080/jpeg?cam=4");
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    

    sample stream