0

i have a classical server client tcp socket system to send messages from client to a server. That works quiet fine. Now I am trying to play a sound remote. The client should send the path to the sound. It is /mnt/sdcard/Music/sound.mp3 This is what it sends to the server.

The server knows the client IP. Maybe the IP string looks like /192.168.1.2 The server should interprete it and start the sound by streaming.

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();
    }

If I take a simple http URI it works very well (i.e. http://test.com/sound.mp3). The sound will be played. Now I am trying to play the sound the client sends to the server. But the code breaks with an IOException ("Prepared failed: status 0x1") at mPlayer.prepare(); I believe the URI is wrong. I tried several ways (rtsp://IP:port/path, rtsp://IP/path, same with file:// and http://). But I do not find the right one. Maybe I miss a thing? Do the server have permission to access the clients sound? If not, how do I give it to the server?

Thanks for your help! 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) {...
S-Man
  • 22,521
  • 7
  • 40
  • 63
  • can you access the file ? can you check if you can download it ? – osayilgan Sep 03 '14 at 15:48
  • Unless you actually have an http listener (server) on the client device that can stream the file back, this isn't going to work. As far as I know, MediaPlayer only understands local files, resources, and http urls. Given that you already have a TCP connection between client and server, you could easily just let the client push the file to the server. And then the server plays it. – selbie Sep 03 '14 at 16:29
  • @osayilgan Isn't it exactly my problem? How could I access the file from the server if I don't know the URI? – S-Man Sep 03 '14 at 22:44
  • @selbie Hm, you mean it isn't possible in any way? How can I push the file to the server? – S-Man Sep 03 '14 at 22:45
  • When you say "client" and "server", which devices are running Android? Or are one of the devices a PC? And which device actually has the file on it? Which one actually do you want to have "play" the file out the speakers? When you say "server", do you really mean an Android device running code with a listening socket that accepts inbound TCP connections? – selbie Sep 04 '14 at 00:09
  • @selbie: Both are running Android. And yes, the server is running a serversocket (see edit) – S-Man Sep 04 '14 at 11:10
  • I could write this up as an answer, but what you are currently doing is not going to work. The `file://` URL nomenclature doesn't take an IP address parameter. What you should do is go Google for "java web server" or look at [this link](http://stackoverflow.com/questions/2717294/create-a-simple-http-server-with-java) on how to host a light weight web server in your process. If you host a web service on one or both devices, you can pass `"http://"+ _clientIP+"/filename"` to `MediaPlayer.setDataSource`. – selbie Sep 04 '14 at 11:57
  • @selbie Thanks a lot. I did it with a http server (https://stackoverflow.com/questions/19359304/how-to-serve-a-file-on-sdcard-using-nanohttpd-inside-android). But for my application it feels like an overkill... Ok, if mediaplayer doesn't support streaming via LAN it seems to be the best solution. Thank you! – S-Man Sep 04 '14 at 17:38

1 Answers1

0

Try to play your link by MXPlayer (see context menu->Network Stream) to check if it's correct.

dasar
  • 5,321
  • 4
  • 24
  • 36
  • Nice idea, but same result: I do not know which link I have to use :) – S-Man Sep 03 '14 at 22:42
  • First you have to find correct url. Than make sure Android can play it natively without SW decoders which MXPlayer can use. Also you can play/stream audio or video using VLC. – dasar Sep 04 '14 at 05:15