Android Open Source - android-qq Audio






From Project

Back to project page android-qq.

License

The source code is released under:

Apache License

If you think the Android project android-qq listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.myandroid.util;
//from www .  java2 s  . c om
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;

public class Audio extends Thread {
  private ServerSocket sSocket = null;

  public Audio() {
  }

  @Override
  public void run() {
    super.run();
    try {
      sSocket = new ServerSocket(Tools.AUDIO_PORT);// ??????????
      Tools.out("??????????????");
      while (!sSocket.isClosed() && null != sSocket&&(!Tools.stoptalk)) {
        Socket socket = sSocket.accept();
        socket.setSoTimeout(5000);
        audioPlay(socket);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // ????????????????????
  public void audioPlay(Socket socket) {
    new AudioPlay(socket).start();
  }

  // ????????????????????
  public void audioSend(User person) {
    new AudioSend(person).start();
  }

  // ????????
  public class AudioPlay extends Thread {
    Socket socket = null;

    public AudioPlay(Socket socket) {
      this.socket = socket;
      // android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    }

    @Override
    public void run() {
      super.run();
      try {
        InputStream is = socket.getInputStream();
        // ???????????????
        int bufferSize = android.media.AudioTrack.getMinBufferSize(
            8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

        // ??????????
        AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC,
            8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize,
            AudioTrack.MODE_STREAM);

        // ????????????
        player.setStereoVolume(1.0f, 1.0f);
        byte [] bytes_pkg = null ;
        // ???????????
        player.play();
        byte[] audio = new byte[160];// ??????????
        int length = 0;

        while (!Tools.stoptalk) {
          length = is.read(audio);// ???????????????
          //if (length > 0 && length % 2 == 0) {
            // for(int
            // i=0;i<length;i++)audio[i]=(byte)(audio[i]*2);//??????1??
          //  player.write(audio, 0, length);// ???????????
          //}
          bytes_pkg = audio.clone() ;
          player.write(bytes_pkg,0,bytes_pkg.length);
        }
        player.stop();
        is.close();
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  // ??????????
  public class AudioSend extends Thread {
    User person = null;

    public AudioSend(User person) {
      this.person = person;
      // android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    }

    @Override
    public void run() {
      super.run();
      Socket socket = null;
      OutputStream os = null;
      AudioRecord recorder = null;
        LinkedList<byte[]>  m_in_q ;
        byte []     m_in_bytes ;
      try {
        socket = new Socket(person.getIp(), Tools.AUDIO_PORT);
        socket.setSoTimeout(5000);
        os = socket.getOutputStream();
        // ??????????????
        int bufferSize = AudioRecord.getMinBufferSize(8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

        // ???????????
        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);
        m_in_bytes = new byte [bufferSize] ;
        m_in_q=new LinkedList<byte[]>();
        byte [] bytes_pkg ;
        recorder.startRecording();// ?????
        byte[] readBuffer = new byte[640];// ????????
        int length = 0;
        while (!Tools.stoptalk) {
          //length = recorder.read(readBuffer, 0, 640);// ??mic??????????
          
          //if (length > 0 && length % 2 == 0) {
          //  os.write(readBuffer, 0, length);// ????????????????????????????????
          //}
          recorder.read(m_in_bytes, 0, bufferSize);
          bytes_pkg = m_in_bytes.clone() ;
          if(m_in_q.size() >= 2)
          {
           os.write(m_in_q.removeFirst() , 0, m_in_q.removeFirst() .length);
          }  
          m_in_q.add(bytes_pkg) ;

        }
        recorder.stop();
        os.close();
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public void release() {
    try {
      System.out.println("Audio handler socket closed ...");
      if (null != sSocket)
        sSocket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

com.myandroid.activity.ChatActivity.java
com.myandroid.activity.FileAcitivity.java
com.myandroid.activity.MainActivity.java
com.myandroid.activity.MediaDialog.java
com.myandroid.activity.SettingDialog.java
com.myandroid.util.Audio.java
com.myandroid.util.FileTcpClient.java
com.myandroid.util.FileTcpServer.java
com.myandroid.util.MediaTcpClient.java
com.myandroid.util.MediaTcpServer.java
com.myandroid.util.Media.java
com.myandroid.util.Msg.java
com.myandroid.util.Tools.java
com.myandroid.util.User.java