Android:来自LAN的Mediaplayer流

时间:2014-09-03 15:36:53

标签: java android audio stream lan

我有一个经典的服务器客户端tcp套接字系统,用于将消息从客户端发送到服务器。这很安静。现在我正在尝试播放声音遥控器。客户端应该发送声音的路径。它是/mnt/sdcard/Music/sound.mp3这是它发送给服务器的内容。

服务器知道客户端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)。声音将被播放。现在我正在尝试播放客户端发送到服务器的声音。但代码在mPlayer.prepare();处于IOException("准备失败:状态0x1和#34;)时中断,我认为URI是错误的。我尝试了多种方式(rtsp://IP:port/pathrtsp://IP/path,与file://http://相同。但我找不到合适的人选。也许我想念一件事?服务器是否有权访问客户端的声音?如果没有,我该如何将它提供给服务器?

感谢您的帮助! Greets,S-Man

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) {...

1 个答案:

答案 0 :(得分:0)

尝试通过MXPlayer播放您的链接(请参阅上下文菜单 - >网络流)以检查它是否正确。