problem in streaming audio from https link

2020-06-06 04:22发布

问题:

Hi everyone I'm a bit new to android I have developed an app which is streaming the audio from server everything was going fine until my app crashed, I debugged it and found out the android media player is throwing exception while streaming through HTTPS link. I have almost about 20 links from where I'm streaming audio to my app and about half are HTTPS links and they are not playing. If anyone understand it please answer I need help, if you can give me any piece of code that would be much easier for me to understand.Thanks in advance

回答1:

Android media player doesn't support HTTPS. If you provide a HTTPS URL, then the file will be downloaded.

This "HTTPS" not supported fact is provided in android documentation.



回答2:

Just add below code before calling mediaplayer

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        sf.fixHttpsURLConnection();
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

MySSLSocketFactory: https://github.com/joniks/Android-Async-HTTP/blob/master/library/src/main/java/com/loopj/android/http/MySSLSocketFactory.java