2014-01-07 3 views
0

인터넷에서 오디오 스트림을 읽거나 (예 : http://70.36.96.24:13384/;stream.mp3) 스칼라 언어를 사용하여 콘텐츠를 내 로컬 디스크에 저장할 수 있습니까?스칼라에서 네트워크 스트림을 읽는 방법?

내가 함께 할 것을 시도했다 :

val stream = new URL("http://70.36.96.24:13384/stream.mp3").openStream() 

그러나이 예외가 발생합니다 :이 다른 스트리밍 URL을

java.io.IOException: Invalid Http response 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1342) 
at java.net.URL.openStream(URL.java:1037) 
at .<init>(<console>:15) 
at .<clinit>(<console>) 
at .<init>(<console>:7) 
at .<clinit>(<console>) 
at $print(<console>) 
    ... 

같은 명령이 작동 : 예

http://174-123-32-173.webnow.net.br/98fm.mp3

업데이트 :이 코드는 perf Linux에서는 문제가 있지만 Mac에서는 그렇지 않습니다. 아무도 이유를 알고 있습니까?

업데이트 2 : 실제로이 코드는 Java 6에서는 작동하지만 Java7에서는 작동하지 않습니다. 그것은 OS 문제가되지 않습니다.

+0

링크가 깨졌습니다. 브라우저로 시도해 볼 수 있습니다. – jilen

+1

Linux와 Mac에 어떤 Java 버전을 설치 했습니까? – tuxdna

+1

맞습니다 : Java 6에서는 Java 7에서 오류가 발생합니다 :-) –

답변

3

서버가 유효한 HTTP 응답을 보내지 않아 코드가 작동하지 않습니다. 응답을 기다리고, 전송이 HTTP 요청을 주목 ... 200 없음 헤더가 없다고 가정 HTTP/0.9

$ wget -O- "http://70.36.96.24:13384/;stream.mp3" 
--2014-01-20 17:09:12-- http://70.36.96.24:13384/;stream.mp3 
Connecting to 70.36.96.24:13384... connected. 
HTTP request sent, awaiting response... 200 No headers, assuming HTTP/0.9 
Length: unspecified 
Saving to: `STDOUT' 

    [<=>                             ] 0   --.-K/s    ICY 200 OK 
icy-notice1:<BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR> 
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR> 
icy-name:Radio Brasileirissima 
icy-genre:Musica Brasileira 
icy-url:http://radiobrasileirissima.com.br 
content-type:audio/mpeg 
icy-pub:0 
icy-br:64 

....BINARY CONTENT SKIPPED.... 

당신은 여전히 ​​문자 순서 \r\n\r\n, icy-br:64 끝날 때까지 처음 몇 줄을 건너 뛰는하여 데이터를 읽을 수있는, 실제 오디오 콘텐츠가 시작됩니다.

EDIT1

참고

val timeout = 2000 
val host = "70.36.96.24" 
val port = 13384 
val socket = new Socket 
val endPoint = new InetSocketAddress(host, port) 
socket.connect(endPoint, timeout); 
// Here you send the GET request and read back the response. 
// Every time you read the response, you have to skip 
// the first few bytes until `\r\n\r\n` 
: 귀하의 GET 요청이 포함되어 있어야 데이터를이를 위해 당신은 소켓을 만들 수 있습니다에 연결하고 '/;stream.mp3`에 GET 요청을 전송 읽기

def socketurl = { 
    val timeout = 2000 
    val host = "70.36.96.24" 
    val port = 13384 
    val socket = new Socket 
    val endPoint = new InetSocketAddress(host, port) 
    socket.connect(endPoint, timeout); 
    val (in, out) = (socket.getInputStream(), socket.getOutputStream()) 
    val httpRequest = "GET /;stream.mp3 HTTP/1.1\r\n\r\n" 
    val command = httpRequest.getBytes() 
    println(httpRequest) 
    out.write(command) 

    var buf = Vector(0, 0, 0, 0) 
    var c = in.read() 
    var cont = true 
    while (c != -1 && cont) { 
     buf = buf.drop(1) :+ c 
     print(c.toChar) 
     if (buf(0) == '\r' && buf(1) == '\n' && buf(2) == '\r' && buf(3) == '\n') { 
     cont = false 
     println("Its a match") 
     } 
     c = in.read() 
    } 

    while (c != -1) { 
     // keep buffering the data from here on! 
    } 
    } 

// call the method 
socketurl 
: 여분의 줄 바꿈이

여기
 
GET /;stream.mp3 HTTP/1.1 

가 동일한 URL에 작업 예입니다 HTTP 요청되기 위해서는

+0

이 시도했습니다 : val 소켓 = 새 소켓 val endPoint = new InetSocketAddress (url.getHost(), url.getPort())를 Socket.connect (종점 5000) 브로 스트림 socket.getInputStream =() 브로 경로 url.getPath =() 브로 명령) "n \"를 socket.getOutputStream = ("GET"+ 경로 + socket.getOutputStream(). flush() 하지만 socket.getInputStream()에서 읽을 때 항상 비어 있습니다. ... –

+1

(). GET 요청에는 HTTP 요청이 될 수 있도록 추가 줄 바꿈이 포함되어야합니다. "get /;stream.mp3 HTTP/1.1 \ r \ n \ r \ n" – tuxdna

+0

@DanielCukier : 코드를 업데이트했습니다. 확인해주십시오. 이제 당신을 위해 일해야합니다. – tuxdna

관련 문제