简体   繁体   中英

Android Java: decoding a mp3 file and passing data into a short[] array for analysis

I'm doing a project where I'll decode data from a mp3 file directly and store the pcm data obtained into a temporary folder, temp.pcm in the sdcard for later analysis. I'm having trouble decoding the data....

        //a short array to store raw pcm data
        short[] buffer = new short[bufferSize];

        ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
        File filep = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/onenineoone.mp3");

        InputStream inputStream = new BufferedInputStream(new FileInputStream(filep), 8 * 1024);
        short[] pcm=null;

        try
        {
            Bitstream bitstream = new Bitstream(inputStream);
            boolean done = false;

            while (!done)
            {

                javazoom.jl.decoder.Header frameHeader = bitstream.readFrame();
                javazoom.jl.decoder.Decoder decoder = new javazoom.jl.decoder.Decoder;

                SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
                ****Log.i("decoder", "error in the samplebuffer??");****
                if (output.getSampleFrequency() != 44100 || output.getChannelCount() != 1) throw new DecoderException("Stereo or non-44100 sampling rate .mp3 not supported", null);
                    pcm = output.getBuffer();

                for (short s:pcm) {
                   outStream.write(s);
                 }
                buffer=pcm;
            }    
            done = true;
            bitstream.closeFrame();

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


I declared private javazoom.jl.decoder.Decoder decoder as a global key, but it seems that the error why it would not enter the SampleBuffer is that I had to declare it as javazoom.jl.decoder.Decoder decoder = new javazoom.jl.decoder.Decoder.

It does not make sense (to me) to open twice an InputStream on File filep. You are not even closing the first stream before you open the second. Please adapt that code first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM