Android媒体播放器:来自HTTPS Url的流音频文件 [英] Android media player: streaming audio file from HTTPS Url

查看:215
本文介绍了Android媒体播放器:来自HTTPS Url的流音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从应用程序流式传输来自Android媒体播放器的音频文件.最初,要流式传输的文件来自Http://网址,而我使用的是此代码-

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-

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);
}

此代码对于基于http的文件可以正常工作,但是现在音频文件的URL已更改为https://一个(即, https://an.https.url/),并且代码失败,并出现以下异常:

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

mediaPlayer.prepare();

例外是

Prepare failed.: status=0x1

请提出解决方案.

推荐答案

经过漫长的奋斗,我找到了解决方法,

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

Add below code before 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

正在使用此答案的人,请从developer.android.com阅读此警告.警告:许多网站都描述了一个较差的替代解决方案,该解决方案是安装不执行任何操作的TrustManager.如果这样做,您可能也不会加密您的通信,因为任何人都可以使用DNS技巧通过伪装成您的服务器的自己的代理来发送用户的流量,从而在公共Wi-Fi热点攻击您的用户

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

这篇关于Android媒体播放器:来自HTTPS Url的流音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆