我有一个经典的服务器-客户端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("Prepared failed:status 0x1"),我认为URI是错误的。我尝试了几种方法(rtsp://IP:port/path
、rtsp://IP/path
,与file://
和http://
相同)。但我找不到合适的。也许我错过了什么?服务器是否有访问客户端声音的权限?如果没有,如何将其提供给服务器?
谢谢你的帮助!贪婪,S-Man
第1版: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) {...
尝试通过MXPlayer播放链接(请参阅上下文菜单->网络流)以检查其是否正确。