找到你要的答案

Q:How to play an mp4 video from a URL?

Q:如何从一个URL播放MP4视频?

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.

是否有可能发挥一个MP视频从一个网址?问了很多相同的问题,但几乎所有的问题都是从5年前开始的。想知道是否有什么改变。

answer1: 回答1:
        MediaController mediaController = new MediaController(getActivity());
            mediaController.setAnchorView(videoView);
            videoView.setMediaController(mediaController);
            Uri video = Uri.parse("your mp4 source");
            videoView.setVideoURI(video);
            videoView.start();
        MediaController mediaController = new MediaController(getActivity());
            mediaController.setAnchorView(videoView);
            videoView.setMediaController(mediaController);
            Uri video = Uri.parse("your mp4 source");
            videoView.setVideoURI(video);
            videoView.start();
answer2: 回答2:
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.

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.

希望对你有帮助。

answer3: 回答3:
 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
                }
            });
 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();
            }
        }
    }

您还可以设置监听器到您的视频播放器视图

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
                }
            });
answer4: 回答4:
@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();
    }
}
@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();
    }
}
java  android