在Lollipop上播放URL中的视频

时间:2015-10-07 13:33:09

标签: android video android-5.0-lollipop

我试图从像这样的网址打开.mp4视频

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.parse("https://yaddayadda/video.mp4");
    intent.setDataAndType(data, "video/*");


    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(intent);
    }

当我尝试使用Google相册应用程序打开视频时,这对Lollipop无效(在Nexus上,这是唯一的选择)。在Kitkat和其他版本,照片工作,只在Lollipop它没有。

有没有人知道如何使用照片应用程序在Lollipop上播放视频?

1 个答案:

答案 0 :(得分:0)

要从链接中查看活动中的视频,请使用以下代码:

// Declare variables
ProgressDialog pDialog;
VideoView videoview;

// Insert your Video URL
String VideoURL = "http://<video-url>";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the layout from video_main.xml
    setContentView(R.layout.videoview_main);
    // Find your VideoView in your video_main.xml layout
    videoview = (VideoView) findViewById(R.id.VideoView);
    // Execute StreamVideo AsyncTask

    // Create a progressbar
    pDialog = new ProgressDialog(VideoViewActivity.this);
    // Set progressbar title
    pDialog.setTitle("Video");
    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar
    pDialog.show();

    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                VideoViewActivity.this);
        mediacontroller.setAnchorView(videoview);
        // Get the URL from String VideoURL
        Uri video = Uri.parse(VideoURL);
        videoview.setMediaController(mediacontroller);
        videoview.setVideoURI(video);

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

    videoview.requestFocus();
    videoview.setOnPreparedListener(new OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            pDialog.dismiss();
            videoview.start();
        }
    });

}

如果您没有播放视频的活动并希望在外部播放器中播放,则可以使用以下代码:

public class MainActivity extends Activity implements OnClickListener {

private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.button1) {
        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setDataAndType(Uri.parse("http://ur URL"), "video/*");

        startActivity(Intent.createChooser(intent, "Complete action using"));
    }

}

}