Android (Java) 中 MediaRecorder 的 LocalSocket(Unix 域)客户端-服务器数



我没有得到的是Unix域套接字之间的数据流。

我了解Unix域套接字数据流是

LocalSocket client .connect() --> LocalServerSocket server .accept()

客户端将数据发送到服务器,直接到我理解。

但是,对于来自Android的MediaRecorder的流媒体视频/音频,经过大量研究,我已经看到每个使用LocalSocket而不是MediaRecorder文件的示例原则上如下:

https://static.foxdogstudios.com/static/presentations/keeping-it-real-time.html#/9

LocalServerSocket server = new LocalServerSocket("some name");
receiver = new LocalSocket();
receiver.connect(server.getLocalSocketAddress()); 
LocalSocket sender = server.accept();//LocalSocket not LocalServerSocket as in Source link
// ...
// Give the MediaRecorder our fake file
recorder.setOutputFile(sender.getFileDescriptor());

此代码如下所示:

MediaRecorder --> LocalSocket sender -->

LocalServerSocket server .accept() --> LocalSocket receiver .connect()

这是怎么回事??

似乎,套接字数据以相反的方向流动,因此,我不明白为什么这段代码按预期工作。

你能指教吗?谢谢。

看来基本上,我的想法是正确的。至少我见过的例子,发送者-接收方结构是错误的。

这是我制作的更直接的模型:

package com.example.app;
import android.app.Activity;
import android.media.MediaRecorder;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import java.io.IOException;
public class MainActivity extends Activity
{
    private final String SOCKET_ADDRESS = "socket1";
    private MediaRecorder mRecorder = new MediaRecorder();
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        class IOThread extends Thread
        {
            @Override
            public void run()
            {
                System.out.println("--------IO Thread------ ");
                LocalSocket receiver;
                LocalServerSocket server;
                try
                {
                    server = new LocalServerSocket(SOCKET_ADDRESS);
                    localServerCreated();
                    receiver = server.accept();
                    System.out.println("---------server.accept();------------- ");
                    int ret = 0;
                    while ((ret = receiver.getInputStream().read()) != -1)
                    {
                        System.out.println( "ret =" + ret);
                    }
                    System.out.println("ret =" + ret);
                }
                catch (IOException e)
                {
                }
            }
        }
        IOThread io1 = new IOThread();
        io1.start();

    }
    private void localServerCreated()
    {
        LocalSocket sender;
        try
        {
            sender = new LocalSocket();
            sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mRecorder.setOutputFile(sender.getFileDescriptor());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println("------mRecorder configured--------");
        try
        {
            mRecorder.prepare();
            mRecorder.start();
            System.out.println("------mRecorder.start()--------");
        }
        catch (IllegalStateException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}

相关内容

  • 没有找到相关文章

最新更新