【问题标题】:Receive Live Audio Data Via Socket on Android在 Android 上通过 Socket 接收实时音频数据
【发布时间】:2017-03-10 03:10:18
【问题描述】:

我编写了一个应用程序以通过 WiFi 连接到 Rpi 上的网络套接字,并成功地向客户端和服务器发送数据,例如字符串。下一步是让客户端(在本例中为我的手机)实时接收和播放从服务器发送的音频数据。

到目前为止,我已经声明了套接字和音频变量。一旦按下按钮,就会启动异步任务“Listen”。我知道它可以连接到套接字。之后,我尝试打开一个输入流并将其输入缓冲区,然后实时播放该缓冲区。

我想我已经接近解决了,但我认为我还没有完全正确。我在代码中用 TODO 标记了我不确定的区域。

public class MainActivity extends AppCompatActivity {
//audio variables
static final int sampleFreq = 44100;
static final int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
static final int streamType = STREAM_MUSIC;
static final int audioMode = MODE_STREAM;
static final int bufferSize = getMinBufferSize(sampleFreq, channelConfig, audioMode);

//socket variables
public Socket mySocket;
private int SERVERPORT = 33333;
private static final String SERVER_IP = "172.24.1.1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}

 public void onClickListen(View view) {
    //call async task
    new Listen().execute();
}


private class Listen extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {

        try {
            //get IP and port number
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            Log.d(debugStr, "In initial listen connect");
            //create socket
            mySocket = new Socket(serverAddr, SERVERPORT);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            if (mySocket == null) {
                str1 = "Socket became null, listener";
                return null;
            }



//TODO here is all the new audio stuff
            try {
                //creates buffer to hold incoming audio data
                byte [] audioBuffer = new byte[4096];

                //creates input stream readers to read incoming data
                BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
                DataInputStream myDis = new DataInputStream(myBis);
                Log.d(debugStr, "Input created, listener");

                // Read the file into the music array.
                int i = 0;

                //TODO unsure of while loop condition
                while (myDis.read() != -1) {
                    //since incoming audio is unknown size, use estimated buffer size
                    audioBuffer[bufferSize-1-i] = myDis.readByte();
                    i++;
                }
                //create audio track object 
                AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);

                //TODO write audio data to somewhere?
                //TODO should this be in a while loop for live stream?
                //TODO will this actually play the audio?
                myAudioTrack.write(audioBuffer, 0, bufferSize);

                //close input streams
                myDis.close();
                myBis.close();


            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                mySocket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }
    }
}
}

我认为我的 while 循环条件不正确,或者这甚至不是从输入流中读取数据的正确方法。我还认为 audioTrack.write 应该在 while 循环中,如果确实可以播放音频的话。

非常感谢任何帮助或澄清!

【问题讨论】:

    标签: java android sockets audio


    【解决方案1】:

    我想通了。

    byte [] audioBuffer = new byte[4096];
    
                    //creates input stream readers to read incoming data
                    BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
                    DataInputStream myDis = new DataInputStream(myBis);
    
                    Log.d(debugStr, "Input created, listener");
                    AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);
                    //Log.d(debugStr, String.valueOf(mySocket.getInputStream().read(audioBuffer)));
    
                    Log.d(debugStr, "track made");
                    // Read the file into the music array.
                    int i = 0;
                    //TODO unsure of while loop condition
                    while (mySocket.getInputStream().read(audioBuffer) != -1) {
    
                        audioBuffer[audioBuffer.length-1-i] = myDis.readByte();
                        myAudioTrack.play();
                        myAudioTrack.write(audioBuffer, 0, audioBuffer.length);
                        i++;
                    }
    
    
    
                    //close input streams
                    myDis.close();
                    myBis.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2013-03-11
      • 1970-01-01
      • 2015-07-31
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多