java - Android 5.0 Lollipop 中的 mediaPlayer,我无法播放,来自 url 的长声音。在 Android 5.0 到来之前我从未遇到过任何问题

标签 java android audio callback uri

public void onClick(View v) {
    toSound.setVisibility(View.INVISIBLE);
    toPro.setVisibility(View.VISIBLE);
    toSound1 = toSound;
    toPro1 = toPro;

    if(isPLAYING){
        mp.stop();
        mp.reset();
    }

    try{
        isPLAYING = true;
        mp.setDataSource("http://..."
                + result.get(arg0).toCode
                +"&ie=UTF-8"
                + "&q="
                + result.get(arg0).toText);
        mp.prepareAsync();

    }catch(IOException e){
        Log.i("error", "prepare() failed");
    }
}



mp.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp1) {
        mp.start();
        toPro1.setVisibility(View.INVISIBLE);
        toSound1.setVisibility(View.VISIBLE);
        fromPro1.setVisibility(View.INVISIBLE);
        fromSound1.setVisibility(View.VISIBLE);
    }
});

我的日志

V/MediaPlayer(6196): setVideoSurfaceTexture
V/MediaPlayer(6196): prepareAsync
I/System.out(6196): KnoxVpnUidStorageknoxVpnSupported API value returned is false
D/MediaHTTPConnection(6196): setReadTimeout with 30000ms
I/System.out(6196): (HTTPLog)-Static: isSBSettingEnabled false
V/ActivityThread(6196): updateVisibility : ActivityRecord{27745ca2 token=android.os.BinderProxy@c738f70 show : true
D/MediaHTTPConnection(6196): setReadTimeout with 30000ms
I/System.out(6196): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(6196): KnoxVpnUidStorageknoxVpnSupported API value returned is false
D/MediaHTTPConnection(6196): setReadTimeout with 30000ms
I/System.out(6196): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(6196): KnoxVpnUidStorageknoxVpnSupported API value returned is false
D/MediaHTTPConnection(6196): setReadTimeout with 30000ms
I/System.out(6196): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(6196): KnoxVpnUidStorageknoxVpnSupported API value returned is false
V/MediaPlayer(6196): message received msg=100, ext1=1, ext2=-2147483648
E/MediaPlayer(6196): error (1, -2147483648)
V/MediaPlayer(6196): callback application
V/MediaPlayer(6196): back from callback
E/MediaPlayer(6196): Error (1,-2147483648)
V/MediaPlayer-JNI(6196): reset
V/MediaPlayer(6196): reset
V/MediaPlayer(6196): message received msg=8, ext1=0, ext2=0
V/MediaPlayer(6196): notify(8, 0, 0) callback on disconnected mediaplayer

我在 list 中拥有 API 级别 21,并尝试使用

SetDataSource(context,Uri.parse(your url))

最佳答案

嘿,在依赖项中添加以下行

编译'com.loopj.android:android-async-http:1.4.9'

然后在MainActivy中添加以下代码

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

                    mediaPlayer.setDataSource(MusicPlayerTestActivity.this, Uri.parse(urlFromServer)); // setup song from https://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source

                    mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
                    mediaPlayer.start();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

现在运行应用程序,音乐将正常播放。谢谢

关于java - Android 5.0 Lollipop 中的 mediaPlayer,我无法播放,来自 url 的长声音。在 Android 5.0 到来之前我从未遇到过任何问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143790/

相关文章:

android - 当 Activity 进入后台时,MediaPlayer 停止播放声音

java - 与应用程序打包在一起的最轻的数据库

java - 有没有一种有效的方法来舍入内部具有双值的字符串并得到 double 答案?

android - 在用户之间发送图像并在 Android 中将其保存在服务器中一段时间​​的最佳方式是什么?

Android访问媒体商店的权限

延长音频文件持续时间的Linux命令

java - 如何使用内连接按页面请求排序

java - 在 Android App 中打包 1 个以上的 widget

android - 如何让用户恢复到应用自定义声音?

php - 音频/音乐社交网站托管服务