VideoView支持播放https的url視頻

重寫setVideoUrI方法
  @Override
public void setVideoURI(Uri uri) {
    super.setVideoURI(uri);
    try {
        HttpsURLConnection.setDefaultSSLSocketFactory(SSlUtiles.createSSLSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new SSlUtiles.TrustAllHostnameVerifier());
    } catch (Exception e) {
        e.printStackTrace();
    }
}


SSlUtiles.java

public class SSlUtiles {
/**
 * 默認信任全部的證書
 *
 * [@return](https://my.oschina.net/u/556800)
 */
public static SSLSocketFactory createSSLSocketFactory() {

    SSLSocketFactory sSLSocketFactory = null;

    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[]{new TrustAllManager()},
                new SecureRandom());
        sSLSocketFactory = sc.getSocketFactory();
    } catch (Exception e) {
    }

    return sSLSocketFactory;
}

public static class TrustAllManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType)

            throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}

public static class TrustAllHostnameVerifier implements HostnameVerifier {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}
相關文章
相關標籤/搜索