java - 将 mp3 解码为 pcm,并在 Google Android 中播放音轨

标签 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);
        }
      }

最佳答案

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();
}

关于java - 将 mp3 解码为 pcm,并在 Google Android 中播放音轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025977/

相关文章:

java - 如何摆脱警告: Nashorn engine is planned to be removed from a future JDK release?

java - 并发事务的问题

java - 禁用和重新启用 EditText 字段 : Cursor and keyboard do not reappear after re-enabling it

android - 应用恢复后触发 onQueryTextChange

c# - 使用Windows.Media.Transcoding从WAV创建m4a

java - 从 lib 目录加载 jar 文件的顺序

java - 通过 id 从嵌套对象序列化

android - 如何在 Amazon Fire TV 的 WebView 中播放 YouTube 视频?

html - Chrome 中的音频问题

android - 在进入扬声器之前处理声波