android – 意外的状态行:URL openStream()方法的ICY 200 OK?

前端之家收集整理的这篇文章主要介绍了android – 意外的状态行:URL openStream()方法的ICY 200 OK?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据kitakt 4.4的变化,播放shoutcast流有些问题(那些返回“ICY”而不是“HTTP / 1.x”响应).

因此,kitkat的解决方案是在我们打开流之前在JVM中重新注册“icy”协议前缀:

try {
        java.net.URL.setURLStreamHandlerFactory( new java.net.URLStreamHandlerFactory(){
            public java.net.URLStreamHandler createURLStreamHandler( String protocol ) {
                Log.d( LOG,"Asking for stream handler for protocol: '" + protocol + "'" );
                if ("icy".equals( protocol )) return new com.spoledge.aacdecoder.IcyURLStreamHandler();
                return null;
            }
        });
}
catch (Throwable t) {
        Log.w( LOG,"Cannot set the ICY URLStreamHandler - maybe already set ? - " + t );
}

我有开放音频流的问题,使其注册.在我调用url.opnestream(stream)后,我得到了异常:

java.net.ProtocolException: Unexpected status line: ICY 200 OK

我该怎么办呢?

这是注册音频的样本,到目前为止我做了什么..

try {
    URL url = null;
    url = new URL(u);
    inputStream = url.openStream();

    startTime = System.currentTimeMillis();

    Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    String fileName = File.separator + "radio_" + "recording_" + channelMetadata.replaceAll("\\W","") + System.currentTimeMillis();

    if(isSDPresent)
    {
        outputSource = Environment.getExternalStorageDirectory() + fileName;
    }
    else
    {
        outputSource = Environment.getDataDirectory() + fileName;
    }

    if(contentType.equals("audio/aacp"))
        fileOutputStream = new FileOutputStream(outputSource  + ".acc");
    else if(contentType.equals("audio/mpeg"))
        fileOutputStream = new FileOutputStream(outputSource + ".mp3");
    else
        fileOutputStream = new FileOutputStream(outputSource + ".nieznany_format");

    int bytesRead = 0;
    int bytes;
    while (((bytes = inputStream.read()) != -1) && isRecording) {

        fileOutputStream.write(bytes);
        bytesRead++;

        stopTime = System.currentTimeMillis();

        long seconds = (Math.abs(startTime-stopTime));
        int minutes = 1000 * 60 * 60;

        if(minutes<=seconds)
        {
            Log.d("xxx","recording task exceed stopped");
            break;
        }
    }

    inputStream.close();
    fileOutputStream.close();

} catch (IOException e) {
    e.printStackTrace();
    isRecording = false;
}

isRecording = false;
return null;

解决方法

对Android 4.4进行了各种更改,似乎Android不支持非标准的ShoutCast ICY标头.

似乎OkHttp的好人已经在几天前解决了这个问题(issue 1issue 2).
你可以做的只是直接在你的应用程序中使用OkHttp lib,你将使用更新的OkHttp版本(带有修复的版本),而不是Android附带的版本(你必须等待操作系统)更新).

这也将修复您在其他可能遇到该问题的设备上运行的应用程序.

猜你在找的Android相关文章