无法从JAVA Server在android流中播放mp3文件

时间:2019-02-18 09:11:08

标签: java android sockets tcp mp3

服务器端音频文件音频格式为

new AudioFormat(
                  AudioFormat.Encoding.PCM_SIGNED,
                  44100,
                  16,
                  baseFormat.getChannels(),
                  baseFormat.getChannels() * 2,
                  baseFormat.getSampleRate(),
                  false);

以下是我的服务器读取mp3文件并将其发送到android客户端的java代码

final int READ_THRESHOLD = 2147483647;
                  int framesReaded = READ_THRESHOLD;
                  Header header;
                  for(; framesReaded-- > 0 && (header = bitstream.readFrame()) != null;) {
                      SampleBuffer sampleBuffer = (SampleBuffer) decoder.decodeFrame(header, bitstream);
                      short[] buffer = sampleBuffer.getBuffer();
                      DataOutputStream oos=new DataOutputStream(socket.getOutputStream());

                      for(short ss:buffer) {
                          oos.writeShort(ss);
                      }
                      bitstream.closeFrame();
                      sentCount++;                    
                      //.SendToAll(_buffer, 0, _buffer.length);
                      //oos.close();
                      if(sentCount >=100) {
                          sentCount=0;
                          System.out.println("Connect Recooneted");
                          socket.close();

                          startConnect();

                      }
                  }

以下是我的android代码

static final int frequency = 44100;
    static final int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
    static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

playBufSize=AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, playBufSize, AudioTrack.MODE_STREAM);
        audioTrack.setVolume(10);
  int readCount = _streamIn.read(data, 0, data.length);
                            if (readCount > 0) {

                           audioTrack.write(data, 0, data.length);

                        }

但是我无法在android中播放歌曲。谁能帮帮我。

0 个答案:

没有答案