2014-09-03 3 views
0

나는 클라이언트에서 서버로 메시지를 보내는 고전적인 서버 클라이언트 tcp 소켓 시스템을 가지고있다. 그건 조용하게 잘됩니다. 이제 사운드 리모컨을 재생하려고합니다. 클라이언트는 사운드 경로를 보내야합니다. 그것은 /mnt/sdcard/Music/sound.mp3입니다. 이것이 서버로 보내는 것입니다.Android : Mediaplayer stream from LAN

서버는 클라이언트 IP를 알고 있습니다. 어쩌면 IP 문자열이 /192.168.1.2 처럼 보일 것입니다. 서버가이를 해석하고 스트리밍을 통해 사운드를 시작해야합니다.

private void playSound (String soundFileName) { 
     Uri myUri1 = Uri.parse("file:/" + _clientIP + soundFileName); 
     _txtHint.setText(myUri1.toString()); 

     mPlayer = new MediaPlayer(); 
     mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     try { 
      mPlayer.setDataSource(getApplicationContext(), myUri1); 
     } catch (IllegalArgumentException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
     } catch (SecurityException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
     } catch (IllegalStateException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      mPlayer.prepare(); 
     } catch (IllegalStateException e) { 
      Toast.makeText(getApplicationContext(), "MISE: You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "MIOE: You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
     } 
     mPlayer.start(); 
    } 

간단한 http URI를 사용하면 매우 효과적입니다 (예 : http://test.com/sound.mp3). 사운드가 재생됩니다. 이제 클라이언트가 서버에 보내는 소리를 재생하려고합니다. 하지만 코드가 IOException ("Prepared failed : status 0x1")으로 깨졌습니다. mPlayer.prepare(); URI가 잘못되었다고 생각합니다. 몇 가지 방법을 시도했습니다 (rtsp://IP:port/path, rtsp://IP/path, file://http://과 동일). 그러나 나는 옳은 것을 찾지 못합니다. 어쩌면 내가 놓친거야? 서버가 클라이언트 사운드에 액세스 할 수있는 권한을 가지고 있습니까? 그렇지 않다면 서버에 어떻게 넘겨 줄 수 있습니까?

도움 주셔서 감사합니다. 접견, S-남자

EDIT1 : ServerSocket의

try {  
    _serverSocket = new ServerSocket(SocketServerPORT); 

      while (true) { 
       socket = _serverSocket.accept(); 
       dataInputStream = new DataInputStream(
         socket.getInputStream()); 
       dataOutputStream = new DataOutputStream(
         socket.getOutputStream()); 

       _message = dataInputStream.readUTF(); 
       _clientIP = socket.getInetAddress().toString(); 

       Server.this.runOnUiThread(new Runnable() { 

        @Override 
        public void run() {  
         //Here the Sound should be played. 
         Server.this.playSound(_message); 
        } 
       }); 

       dataOutputStream.writeUTF(_message); 
     } 
} catch (IOException e) {... 
+0

파일에 액세스 할 수 있습니까? 다운로드 할 수 있는지 확인해 주시겠습니까? – osayilgan

+0

파일을 다시 스트리밍 할 수있는 클라이언트 장치에 실제로 http 리스너 (서버)가 없으면 이것이 작동하지 않습니다. MediaPlayer는 로컬 파일, 리소스 및 http URL 만 이해합니다. 이미 클라이언트와 서버 사이에 TCP 연결이 있다면 클라이언트가 파일을 서버로 푸시하도록 할 수 있습니다. 그런 다음 서버가 재생합니다. – selbie

+0

@osayilgan 정확히 내 문제가 아닌가요? URI를 모르는 경우 어떻게 서버에서 파일에 액세스 할 수 있습니까? –

답변

0

MXPlayer하여 링크를 재생하려고은 올바른 있는지 확인하기 위해 (컨텍스트 메뉴 -> 네트워크 스트림 참조).

+0

좋은 생각이지만 같은 결과 : 어떤 링크를 사용해야하는지 모르겠습니다 :) –

+0

먼저 정확한 URL을 찾아야합니다. 안드로이드가 MXPlayer가 사용할 수있는 SW 디코더없이 네이티브로 재생할 수 있는지 확인하십시오. 또한 VLC를 사용하여 오디오 또는 비디오를 재생/스트리밍 할 수 있습니다. – dasar