将mp3解码为pcm,并在Google Android中使用audiotrack

时间:2013-09-26 10:40:52

标签: java android audio mp3 jlayer

首先,如果不使用函数decode_path,我可以使用我的代码播放.wav文件,并且它可以正常使用Jlayer和音轨来播放歌曲。

其次,如果我使用函数decode_path,它可以解码mp3到pcm文件,并将byte[]传递给函数PlayAudioTrack,让它播放。

问题是,我不知道我的代码在哪里出错,我使用320Kbps,44.1Khz立体声类型,Layer3 mp3,但AudioTrack播放噪音但没有音乐〜!!!!

任何人都可以吗? ???

我的代码

 public void PlayAudioTrack(String filePath) throws IOException{
    int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
    AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
    AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);

    //Reading the file..
    int count = 512 * 1024; // 512 kb
    //        byte[] byteData = null;        
    //        byteData = new byte[(int)count];

    //we can decode correct byte data here
    byte[] byteData = null;
    byteData = decode_path(filePath, 0, 20000);

    File file = null; 
    file = new File(filePath);
    FileInputStream in = null;

    try {
        in = new FileInputStream( file );
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    int bytesread = 0, ret = 0;
    int size = (int) file.length();
    at.play();
    while (bytesread < size) {
        Log.e("devon","write byte array with sizes");
        ret = in.read( byteData,0, count);
        if (ret != -1) {
            Log.e("devon","Write the byte array to the track");
            at.write(byteData,0, ret); 
            bytesread += ret;  
        }else break;
    }
    at.stop(); 
    at.release();
}

public static byte[] decode_path(String path, int startMs, int maxMs) 
        throws IOException{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);

        float totalMs = 0;
        boolean seeking = true;

        File file = new File(path);
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file), 8 * 1024);
        try {
          Bitstream bitstream = new Bitstream(inputStream);
          Decoder decoder = new Decoder();

          boolean done = false;
          while (! done) {
            Header frameHeader = bitstream.readFrame();
            if (frameHeader == null) {
              done = true;
            } else {
              totalMs += frameHeader.ms_per_frame();

              if (totalMs >= startMs) {
                seeking = false;
              }

              if (! seeking) {
                SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);

                if (output.getSampleFrequency() != 44100
                    || output.getChannelCount() != 2) {
                    throw new IllegalArgumentException("mono or non-44100 MP3 not supported");
                }

                short[] pcm = output.getBuffer();
                for (short s : pcm) {
                  outStream.write(s & 0xff);
                  outStream.write((s >> 8 ) & 0xff);
                }
              }

              if (totalMs >= (startMs + maxMs)) {
                done = true;
              }
            }
            bitstream.closeFrame();
          }

          return outStream.toByteArray();
        } catch (BitstreamException e) {
          throw new IOException("Bitstream error: " + e);
        } catch (DecoderException e) {
          Log.w(TAG, "Decoder error", e);
          throw new IOException("Decoder error: " + e);
        }
      }

1 个答案:

答案 0 :(得分:-2)

public void PlayAudioTrack(String filePath) throws IOException{
    int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
            AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
            AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);

    //Reading the file..
    int count = 512 * 1024; // 512 kb
    //        byte[] byteData = null;
    //        byteData = new byte[(int)count];

    //we can decode correct byte data here
    byte[] byteData = null;
    byteData = decode_path(filePath, 0, 20000);

    int temp =0;
    at.play();
    while (temp<byteData.length)
    {
        at.write(byteData, temp, count);
        temp+= count;
    }
    at.stop();
    at.release();
}