English 中文(简体)
对等端对端音频呼叫 Android : 音频断音和滞后( 接收包的延迟) 增加
原标题:Peer to Peer Audio Calling on Android : Voice breaks and lag(delay in receiving packets) increases
  • 时间:2012-05-26 19:52:53
  •  标签:
  • java
  • android

我试图建立Peer-Peor-People 音频呼叫和机器人。我用一个机器人电话和一个平板电脑进行通信,但在收到大约40个包之后,电话几乎停止接收这些包,然后突然收到几个包并播放,等等,但这个等待时间会增加。同样,平板电脑最初也接收和播放这些包,但滞后增加,而且声音在一段时间后开始崩溃,好像有些包丢失了。任何想法导致这一问题的原因...

这是 App... 的代码, i 只是在两个设备上运行时, 在 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);
        }
    }
}
问题回答

当在网络上发话时,我遇到麻烦了, 如果频率只有8000, 听起来很可怕。那可能只是因为我的情况。

另一个困难是UDP公司很难说包裹是哪批运到的。 我看过一个能让它们重新回到正轨的操作程序,但现在我找不到它。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签