Android媒体播放器:来自HTTPS Url的流媒体音频文件

时间:2022-05-28 14:46:49

I have to stream an audio file from Android media player from an application. Initially the file to be streamed was coming from a Http:// url and for which I was Using code-

我必须从应用程序中流式传输来自Android媒体播放器的音频文件。最初要流式传输的文件来自Http:// url,我正在使用代码 -

public void playSample() {
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected void onPreExecute() {
            mediaPlayer = new MediaPlayer();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                try {
                    mediaPlayer.setDataSource("http://an.http.url/");
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    Log.e("AudioFileError", "Could not open file for playback.", e);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pd.dismiss();
        }
    };
    task.execute((Void[]) null);
}

This code is working as desired with http based file, but now the URL for Audio file was changed to https:// one(i.e., https://an.https.url/) and the code fails with an exception in

此代码根据需要使用基于http的文件,但现在音频文件的URL已更改为https:// one(即https://an.https.url/),并且代码失败并出现异常

mediaPlayer.prepare();

The exception is

例外是

Prepare failed.: status=0x1

Please suggest a solution for it.

请为它建议一个解决方案。

2 个解决方案

#1


0  

Media player in android <4.x supports only HTTP and 4.x n above support for BOTH http and https , so while using https with older API level, please think over it use http instead of https.

android <4.x中的媒体播放器仅支持HTTP和4.x n以上支持BOTH http和https,因此在使用较旧API级别的https时,请考虑使用http而不是https。

#2


0  

After long struggle i found solution,

经过长时间的奋斗,我找到了解

Add below code before setDataSource().

在setDataSource()之前添加以下代码。

    try {
        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);
    } catch (Exception e) {
        e.printStackTrace();
    }

#update1

Those who are using this answer, Read this caution from developer.android.com. Caution: Many web sites describe a poor alternative solution which is to install a TrustManager that does nothing. If you do this you might as well not be encrypting your communication, because anyone can attack your users at a public Wi-Fi hotspot by using DNS tricks to send your users' traffic through a proxy of their own that pretends to be your server

那些使用这个答案的人,请阅读developer.android.com的这个警告。警告:许多网站描述了一个糟糕的替代解决方案,即安装一个什么都不做的TrustManager。如果您这样做,您可能也不会加密您的通信,因为任何人都可以通过使用DNS技巧通过他们自己的代理发送您的用户流量假装成您的服务器,从而在公共Wi-Fi热点攻击您的用户

#1


0  

Media player in android <4.x supports only HTTP and 4.x n above support for BOTH http and https , so while using https with older API level, please think over it use http instead of https.

android <4.x中的媒体播放器仅支持HTTP和4.x n以上支持BOTH http和https,因此在使用较旧API级别的https时,请考虑使用http而不是https。

#2


0  

After long struggle i found solution,

经过长时间的奋斗,我找到了解

Add below code before setDataSource().

在setDataSource()之前添加以下代码。

    try {
        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);
    } catch (Exception e) {
        e.printStackTrace();
    }

#update1

Those who are using this answer, Read this caution from developer.android.com. Caution: Many web sites describe a poor alternative solution which is to install a TrustManager that does nothing. If you do this you might as well not be encrypting your communication, because anyone can attack your users at a public Wi-Fi hotspot by using DNS tricks to send your users' traffic through a proxy of their own that pretends to be your server

那些使用这个答案的人,请阅读developer.android.com的这个警告。警告:许多网站描述了一个糟糕的替代解决方案,即安装一个什么都不做的TrustManager。如果您这样做,您可能也不会加密您的通信,因为任何人都可以通过使用DNS技巧通过他们自己的代理发送您的用户流量假装成您的服务器,从而在公共Wi-Fi热点攻击您的用户