如何通过视频链接在Android中播放视频

时间:2017-07-14 13:04:33

标签: java android video video-streaming

如何通过传递任何视频链接在Android中播放视频(例如 - " https://youtu.be/SiD77g9KfhA"或者它可以是来自其他媒体门户的另一个视频链接)。我试图这样做,但我收到了一个错误。

我的代码

try
{
    // your code
}
catch(FtpCommandException ex) 
       when (ex.Value == "file/directory not found")
{
   // do something with this exception
}

错误。

enter image description here

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:-1)

您可以尝试使用此代码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);
    }

}