简体   繁体   中英

How to play an mp4 video from a URL?

Is it possible to play an mp video from a URL? There are quite a lot of questions asking the same, but almost all of them are from 5 years ago. Wondering if anything has changed.

        MediaController mediaController = new MediaController(getActivity());
            mediaController.setAnchorView(videoView);
            videoView.setMediaController(mediaController);
            Uri video = Uri.parse("your mp4 source");
            videoView.setVideoURI(video);
            videoView.start();
 private void playVideoFile() throws Throwable
    {
        try
        {
            MediaController m_mc = new MediaController(this);
            m_mc.setAnchorView(m_vwVideoPlayerView);
            m_mc.setMediaPlayer(m_vwVideoPlayerView);
            m_vwVideoPlayerView.setMediaController(m_mc);

            m_vwVideoPlayerView.setVideoPath("Your video url");

            m_vwVideoPlayerView.requestFocus();
            m_vwVideoPlayerView.start();

        }
        catch (Throwable e)
        {
            if (m_vwVideoPlayerView != null) {
                m_vwVideoPlayerView.stopPlayback();
            }
        }
    }

You can also set listener to your video player view

m_vwVideoPlayerView.setOnPreparedListener(new OnPreparedListener(){
                public void onPrepared(MediaPlayer p_arg0)
                {
                    if (!isFinishing()&& m_vwVideoPlayerView != null)
                    {
                        m_vwVideoPlayerView.start();
                    }
                }
            });


m_vwVideoPlayerView.setOnCompletionListener(new OnCompletionListener()
{
@Override
    public void onCompletion(MediaPlayer mp)
    {
        finish();
    }
});


m_vwVideoPlayerView.setOnErrorListener(new MediaPlayer.OnErrorListener(){
                @Override
                public boolean onError(MediaPlayer p_mp, final int p_what, final int p_extra)
                {
                    // Display Message whcih you want to display
                }
            });
MediaController mc = new MediaController(this);
                mc.setAnchorView(video);
                mc.setMediaPlayer(video);
                url = url.replace(" ", "%20");
                Log.e("url",url);
                Uri videoLInk = Uri.parse(url);
                video.setMediaController(mc);
                video.setVideoURI(videoLInk);
                video.requestFocus();
                video.setOnPreparedListener(new OnPreparedListener() {

                    public void onPrepared(MediaPlayer arg0) {
                        pDialog.dismiss();
                        video.start();
                    }
                });

You can pass any video url. Note : I have replace space with %20 because of if you running 3G then your video won't get play. So whenever space is there you need to first replace it with %20 .

Hope it helps you.

@Override
protected void onCreate(Bundle savedInstanceState)
     // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.videodisplay);
        String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(link);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM