4

我正在尝试在 android 上建立点对点音频呼叫。我使用安卓手机和平板电脑进行通信,但在收到大约 40 个数据包后,手机几乎停止接收数据包,然后突然收到一些数据包并播放它们等等,但等待时间增加了。同样,平板电脑最初会接收数据包并播放它们,但延迟会增加,并且一段时间后语音开始中断,就好像丢失了一些数据包一样。任何想法是什么导致了这个问题......

这是应用程序的代码...我只是在 RecordAudio 类中提供发送者和接收者的 IP 地址,同时在两台设备上运行它。

public class AudioRPActivity extends Activity implements OnClickListener {

    DatagramSocket socketS,socketR;
    DatagramPacket recvP,sendP;
    RecordAudio rt;
    PlayAudio pt;

    Button sr,stop,sp;
    TextView tv,tv1;
    File rf;

    boolean isRecording = false;
    boolean isPlaying = false;

    int frequency = 44100;
    int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.text1);
        tv1 = (TextView)findViewById(R.id.text2);

        sr = (Button)findViewById(R.id.sr);
        sp = (Button)findViewById(R.id.sp);
        stop = (Button)findViewById(R.id.stop);

        sr.setOnClickListener(this);
        sp.setOnClickListener(this);
        stop.setOnClickListener(this);

        stop.setEnabled(false);

        try
        {
        socketS=new DatagramSocket();
        socketR=new DatagramSocket(6000);
        }
        catch(SocketException se)
        {
            tv.setText(se.toString());
            finish();
        }
    }

    public void onClick(View v) {

        if(v == sr)
            record();
        else if(v == sp)
            play();
        else if(v == stop)
            stopPlaying();
    }

    public void play()
    {
        stop.setEnabled(true);
        sp.setEnabled(false);
        pt = new PlayAudio();
        pt.execute();
    }

    public void stopPlaying()
    {
        isRecording=false;
        isPlaying = false;
        stop.setEnabled(false);
    }

    public void record()
    {
        stop.setEnabled(true);
        sr.setEnabled(false);
        rt = new RecordAudio();
        rt.execute();
    }



    private class PlayAudio extends AsyncTask<Void,String,Void>
    {

        @Override
        protected Void doInBackground(Void... arg0)
        {
            isPlaying = true;
            int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);

            byte[] audiodata = new byte[bufferSize];

            try
            {
                AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,frequency,channelConfiguration,
                                                        audioEncoding,4*bufferSize,AudioTrack.MODE_STREAM);
                audioTrack.setPlaybackRate(frequency);
                audioTrack.play();

                while(isPlaying)
                {
                    recvP=new DatagramPacket(audiodata,audiodata.length);
                    socketR.receive(recvP);
                    audioTrack.write(recvP.getData(), 0, recvP.getLength());
                }
                audioTrack.stop();
                audioTrack.release();
            }
            catch(Throwable t)
            {
                Log.e("Audio Track","Playback Failed");
            }
            return null;
        }
        protected void onProgressUpdate(String... progress)
        {
            tv1.setText(progress[0].toString());
        }

        protected void onPostExecute(Void result)
        {
            sr.setEnabled(true);
            sp.setEnabled(true);
        }

    }

    private class RecordAudio extends AsyncTask<Void,String,Void>
    {

        @Override
        protected Void doInBackground(Void... arg0)
        {
            isRecording = true;

            try
            {
                int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);

                AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,frequency,channelConfiguration
                                                            ,audioEncoding,4*bufferSize);   
                byte[] buffer = new byte[bufferSize];
                audioRecord.startRecording();
                int r=0;
                while(isRecording)
                {
                    int brr = audioRecord.read(buffer,0,bufferSize);

                    sendP=new DatagramPacket(buffer,brr,InetAddress.getByName("sender's/receiver's ip"),6000);
                    socketS.send(sendP);
                    publishProgress(String.valueOf(r));

                    r++;
                }

                audioRecord.stop();
                audioRecord.release();

            }
            catch(Throwable t)
            {
                Log.e("AudioRecord","Recording Failed....");
            }


            return null;
        }

        protected void onProgressUpdate(String... progress)
        {
            tv.setText(progress[0].toString());
        }

        protected void onPostExecute(Void result)
        {
            sr.setEnabled(true);
            sp.setEnabled(true);
        }
    }
}
4

1 回答 1

1

通过网络发送语音时,如果频率不是 8000,我会遇到麻烦。44100 听起来很可怕。那可能只是因为我的情况。

另一个困难是,对于 UDP,很难说数据包到达的顺序。我已经看到了一个将它们放回正确顺序的实现,但我现在找不到它。

于 2012-06-12T22:54:45.923 回答