2010-12-14 19 views
2

我想通过Android上的套接字音频流。 我使用AudioRecord获取客户端录制的音频和AudioTrack类以在服务器端播放原始数据。但我无法做到。 有没有其他的程序可以做到这一点?AudioStreaming通过套接字在android

audiosender文件

public void run() 
{ 

    BufferedOutputStream bufferedStreamInstance = null; 
    try 
    { 
    Socket socket = new Socket("192.168.1.25", 1234); 
    bufferedStreamInstance = new BufferedOutputStream(
     socket.getOutputStream()); 
    } catch (FileNotFoundException e) 
    { 
    throw new IllegalStateException("Cannot Open File", e); 
    } catch (UnknownHostException e) 
    { 
    e.printStackTrace(); 
    } catch (IOException e) 
    { 
    e.printStackTrace(); 
    } 
    DataOutputStream dataOutputStreamInstance = new DataOutputStream(
     bufferedStreamInstance); 

    int bufferRead = 0; 
    int bufferSize = AudioRecord 
     .getMinBufferSize(this.getFrequency(), this.getChannelConfiguration(), this.getAudioEncoding()); 
    AudioRecord recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, this.getFrequency(), this 
     .getChannelConfiguration(), this.getAudioEncoding(), bufferSize); 
    short[] tempBuffer = new short[bufferSize]; 

    recordInstance.startRecording(); 
    while (this.isRecording) 
    { 
    bufferRead = recordInstance.read(tempBuffer, 0, bufferSize); 
    if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) 
    { 
     throw new IllegalStateException(
      "read() returned AudioRecord.ERROR_INVALID_OPERATION"); 
    } 
    else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) 
    { 
     throw new IllegalStateException(
      "read() returned AudioRecord.ERROR_BAD_VALUE"); 
    } 
    else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) 
    { 
     throw new IllegalStateException(
      "read() returned AudioRecord.ERROR_INVALID_OPERATION"); 
    } 
    try 
    { 
     for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer) 
     { 
     dataOutputStreamInstance.writeShort(tempBuffer[idxBuffer]); 
     } 
    } catch (IOException e) 
    { 
     throw new IllegalStateException(
      "dataOutputStreamInstance.writeShort(curVal)"); 
    } 

    } 
    recordInstance.stop(); 
    try 
    { 
    dataOutputStreamInstance.close(); 
    bufferedStreamInstance.close(); 

    } catch (IOException e) 
    { 
    throw new IllegalStateException("Cannot close buffered writer."); 
    } 
} 

audioreceiver

public void run() 
{ 

    musicLength = 66535; 
    music = new short[musicLength]; 
    try 
    { 
    ServerSocket serverSocket = new ServerSocket(1234); 
    Socket socket = serverSocket.accept(); 
    InputStream is = socket.getInputStream(); 
    BufferedInputStream bis = new BufferedInputStream(is); 

    dis = new DataInputStream(bis); 
    while (dis.available() > 0) 
    { 
     music[musicLength - 1 - i] = dis.readShort(); 
     i++; 
    } 
    try 
    { 
     dis.close(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, 
     AudioFormat.CHANNEL_CONFIGURATION_MONO, 
     AudioFormat.ENCODING_PCM_16BIT, musicLength, 
     AudioTrack.MODE_STREAM); 
    audioTrack.play(); 
    audioTrack.write(music, 0, musicLength); 
    } catch (Throwable t) 
    { 
    Log.e("AudioTrack", "Playback Failed"); 
    } 
} 

回答

2

你无法发送的数据?或者无法在接收端正确播放?与MediaRecorder类一样,AudioRecorder类可能会为标题留下一些空间,然后在录制完成后执行查找以填充标题。由于无法在套接字上查找,因此标头将写入文件的头部。我认为你最好的行动方式是将文件写入SD卡,然后使用hexeditor将字节与正在工作的音频文件进行比较。然后,您应该能够确定标题的格式,并且在收到标题时可以查找文件的开头(或缓冲区的开始)并将标题写入适当的位置。