【问题标题】:Decode mp3 to pcm, and play with audiotrack in Google Android将 mp3 解码为 pcm,并在 Google Android 中播放音轨
【发布时间】:2013-10-02 06:54:51
【问题描述】:

首先,如果不使用函数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);
        }
      }

【问题讨论】:

  • 一定要使用AudioTrack吗?如果您使用 MediaPlayer 类,它将为您处理解码。
  • 是的,我知道。但我需要从 MP3 中获取 PCM,我只是想检查我的解码器是否正确。
  • 主要目的是做一个可以得到PCM码流的mp3解码器。
  • 感谢您的错字编辑...谢谢
  • 好吧,为什么你的播放循环中有这个:ret = in.read( byteData,0, count);byteData 不应该来自decode_path 吗?

标签: java android audio mp3 jlayer


【解决方案1】:
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();
}

【讨论】:

  • 能否补充一些简短的解释,让OP既能解决问题,又能看懂答案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 2018-09-25
  • 1970-01-01
  • 2011-01-16
  • 2019-06-27
  • 2011-07-22
  • 1970-01-01
相关资源
最近更新 更多