如何从视频链接在android中播放视频

deyfvvtc  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(644)

我如何通过传递任何视频链接(例如-)在android中播放视频https://youtu.be/sid77g9kfha“也可以是另一个媒体门户的另一个视频链接)。我试图这样做,但我得到一个错误。
我的代码

try {
                if (!mVideoView.isPlaying()) {
                    Uri uri = Uri.parse(videoURL);
                    mVideoView.setVideoURI(uri);
                    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {

                        }
                    });
                } else {
                    mVideoView.pause();
                }
            }catch (Exception ex){

            }
            mVideoView.requestFocus();
            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mProgressDialog.dismiss();
                    mp.setLooping(true);
                    mVideoView.start();
                }
            });

错误。

ki1q1bka

ki1q1bka1#

你可以试试这个代码 snippet . 我用这个代码在我的应用程序中播放视频 landscape 模式。

package xyz.abc.package;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class FullVideo extends Activity {

    ProgressDialog pDialog;
    VideoView videoview;
    private int position = 0;
    String videoUrl, imageUrl, cid;

     private MediaController mediaControls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.full_video);

        videoUrl = "https://youtu.be/SiD77g9KfhA"

        if (mediaControls == null) {
            mediaControls = new MediaController(FullVideo.this);
        }

        videoview = (VideoView) findViewById(R.id.videoView);
        // Execute StreamVideo AsyncTask

        // Create a progressbar
        pDialog = new ProgressDialog(FullVideo.this);
        // Set progressbar title
        pDialog.setTitle("Streaming video, please wait.");
        // Set progressbar message
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();

        try {

            mediaControls.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(videoUrl);
            videoview.setMediaController(mediaControls);
            // mediaControls.setMediaPlayer(videoview);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.requestFocus();
                videoview.seekTo(position);
                if (position == 0) {
                    videoview.start();                  
                } else {
                    videoview.pause();
                }
            }
        });

        videoview.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                finish();

            }
        });

    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
            finish();

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.putInt("Position", videoview.getCurrentPosition());
        videoview.pause();

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        position = savedInstanceState.getInt("Position");
        videoview.seekTo(position);
    }

}
r6hnlfcb

r6hnlfcb2#

你所给的是网页网址不是视频网址这个工作我试试这个网址。
http://tamilhd.in/load/tamil%20movies%20trailers/2017%20tamil%20movies%20trailers/baahubali%202%20trailer/320x240/baahubali%202%20trailer.mp4

相关问题