GoogleDrive视频直链播放

String url = file.getDownloadUrl();
String token = GoogleDriveManager.getInstance().getCredential().getToken();
package com.itant.videotest;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //String url = "https://doc-0g-1o-docs.googleusercontent.com/docs/securesc/hnejk8e4m5ljdqoos3m8k3lldf9tt5nk/0kg5e03nhk0hmqcrgd92b2vr1fdgc8fr/1546581600000/07258121021452555024/07258121021452555024/1200HuUW9BrXR7r_W3EG_pKxxZ4AuZi8F?e=download";
        String url = "https://doc-0g-1o-docs.googleusercontent.com/docs/securesc/hnejk8e4m5ljdqoos3m8k3lldf9tt5nk/89okorllo65tr6cd1nteilutn3rktium/1546588800000/07258121021452555024/07258121021452555024/1200HuUW9BrXR7r_W3EG_pKxxZ4AuZi8F?e=download&gd=true";
        //String url = "https://doc-0g-bg-docs.googleusercontent.com/docs/securesc/hoa4m7qp1k7nbdbg50sb59c1gkga9h1t/8lesleda49mnpg3bo96pha6434e6qd75/1546581600000/13601509188492607983/13601509188492607983/1B-Jvtf-1oPq6rR3JCf705iYJGmFeQA36?e=download&gd=true";
        String token = "Bearer ya29.GlyHBoZTYH0WGA3Z_xOnlSADSSjNoLYIpGIAFKBcseFqohdtvo36qLF74EN_khG6oYWtHMzWmVHrsI2Pz5BW_41OtwxQuXrzH_8ej43nCbUVicKUQLmxKQ1VvhX2rQ";
        //synCookies(this, url, value);




        
        VideoView videoView = findViewById(R.id.vv_test);
        Map header = new HashMap<>();
        //header.put("Cookie", "AUTH_q8npsoq7ovao00gfjit4n2rsea0q8fkp=07258121021452555024|1546560000000|ocrmq10miua1sj1e05gi335ras2mov7e");
        //header.put("accept-encoding", "gzip");
        //header.put("authorization", "Bearer ya29.GlyHBtiS8VpVVbSY6u20L60zYGhuohb9Hg_RHxUS08vHc5jbLq-Vqo5retJU2mgUT9fXeSHJGQ2X4pfJ25IS3srpaYzKBCx_YoaA6-2RNxgdDFNS7oXIhXlqI6yzLQ");
        header.put("authorization", token);
        //header.put("user-agent", "Google-HTTP-Java-Client/1.27.0 (gzip)");

        videoView.setVideoURI(Uri.parse(url), header);

        // the rest is just standard VideoView stuff
        MediaController mc = new MediaController(this);
        mc.setAnchorView(videoView);
        videoView.setMediaController(mc);
        videoView.start();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //mp.setLooping(true);
                mp.start();// 播放
                Toast.makeText(MainActivity.this, "开始播放!", Toast.LENGTH_LONG).show();
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(MainActivity.this, "播放完毕", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public static void synCookies(Context context, String url, String value) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setCookie(url, value);
        CookieSyncManager.getInstance().sync();
    }
}

你可能感兴趣的:(Android)