Android Media Player does not work with the local jack, and what is the workaround for streaming data for VoiceChat?

I did a simple test so MediaPlayer would play some streaming streaming data through localSocket.

    class IOLoop extends Thread
    {
        @Override
        public void run()
        {
            try
            {
                MediaPlayer mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                System.out.println("----======MediaPlayer()============-- ");
                LocalSocket receiver = new LocalSocket();
                System.out.println("----======new LocalSocket()============-- ");
                FileDescriptor fd = receiver.getFileDescriptor();
                System.out.println("----fd============-- ");

                    mPlayer.setDataSource(fd);  //<-- error
                    mPlayer.prepare();

                System.out.println("----=========mPlayer set===============-- ");

            }
            catch (IOException e)
            {//
            }
        }
    }

    IOLoop io00 = new IOLoop();
    io00.start();

This code does not work, IllegalArgumentException

02-14 05:16:46.418  20424-20436/com.example.app I/System.out﹕ ----fd============--
02-14 05:16:46.426  20424-20436/com.example.app W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0xa61ea908)
02-14 05:16:46.426  20424-20436/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-197
    java.lang.IllegalArgumentException
            at android.media.MediaPlayer.setDataSource(Native Method)
            at android.media.MediaPlayer.setDataSource(MediaPlayer.java:976)
            at com.example.app.MainActivity$1IOLoop.run(MainActivity.java:51)

therefore googled .

Basically, they say that LocalSocket FileDescriptor is not searchable, so it is not suitable for a data source.

However, according to AndroidDeveloper-Media Playback

http://developer.android.com/guide/topics/media/mediaplayer.html

It clearly states:

Android , , . , ( ), , API MediaPlayer.

, .

, , LINE .. ?

? .


EDIT:

:

MediaPlayer

Android InputStream MediaPlayer?

AAC Android

https://code.google.com/p/aacdecoder-android/

.


EDIT2

.

https://github.com/fyhertz/libstreaming

, MediaCodec - MediaRecorder ..

+3
2

. , . , :

protected String doInBackground() throws Exception {
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info);
microphone.open(af);
Socket conn = new Socket(SERVER,3000);
microphone.start();
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
int bytesRead = 0;
byte[] soundData = new byte[1];
Thread inThread = new Thread(new SoundReceiver(conn));
inThread.start();
while(bytesRead != -1)
{
    bytesRead = microphone.read(soundData, 0, soundData.length);
    if(bytesRead >= 0)
    {
        dos.write(soundData, 0, bytesRead);
    }
}
    // TODO Auto-generated method stub
    return null;
}

}

:

   public SoundReceiver(Socket conn) throws Exception
      {
    connection = conn;
    soundIn = new DataInputStream(connection.getInputStream());
    AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
    inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
    inSpeaker.open(af);
}

public void run()
{
    int bytesRead = 0;
    byte[] inSound = new byte[1];
    inSpeaker.start();
    while(bytesRead != -1)
    {
        try{bytesRead = soundIn.read(inSound, 0, inSound.length);} catch (Exception e){}
        if(bytesRead >= 0)
        {
            inSpeaker.write(inSound, 0, bytesRead);
        }
    }
}
+2

MediaPlayer.setDataSource(), FileDescriptor, MediaPlayer , . .

" , ", , http/rtsp, URL , - mPlayer.setDataSource("http://localhost:12345")

+1

All Articles