哪个jar文件可以用来在android中播放音频文件

时间:2011-05-06 04:17:22

标签: java android jar media-player

当我在谷歌搜索时,我发现了一个用netbeans创建的java程序来计算一首歌的bpm。它正在使用大量的JAR文件。

我为我的Android应用程序使用了相同的代码,由于缺少一个JAR文件,它显示了很多错误。我添加了JLayer1.0.1 jar文件,并清除了所有错误。

现在应用程序运行良好,但bpm计算正在创建一些新问题。它给出了不到1分钟的歌曲的bpm,但是其他歌曲并不总是在进行一个小时。歌曲没有在后台播放。

当我使用Java程序检查时,它正在计算所有歌曲的bpm并且已经播放了歌曲并且我可以听到它。

我面临的问题是什么?这都是因为JAR文件,我应该使用任何其他JAR文件吗?请帮帮我朋友....

我正在添加部分代码

Player player = new Player(new FileInputStream("//sdcard//taxi.mp3"), output);

    public class BPM2SampleProcessor implements SampleProcessor {

        private Queue<Long> energyBuffer = new LinkedList<Long>();

        private int bufferLength = 43;

        private long sampleSize = 1024;
        private long frequency = 44100;
        private long samples = 0;
        private long beats = 0;
        private static int beatThreshold = 3;
        private int beatTriggers = 0;

        private List<Integer> bpmList = new LinkedList<Integer>();

        public void process(long[] sample) {
            energyBuffer.offer(sample[0]);
            samples++;
            if(energyBuffer.size() > bufferLength) {
                energyBuffer.poll();
                double averageEnergy = 0;
                for(long l : energyBuffer)
                    averageEnergy += l;
                averageEnergy /= bufferLength;

                double C = 1.3; //a * variance + b;
                boolean beat = sample[0] > C * averageEnergy;
                if(beat)
                {
                    if(++beatTriggers == beatThreshold)
                        beats ++;
                }
                else
                {
                    beatTriggers = 0;
                }

                if(samples > frequency * 5 / sampleSize) {
                    bpmList.add(getInstantBPM());
                    beats = 0;
                    samples = 0;
                }
            }
        }

        public void init(int freq, int channels) {
            frequency = freq;
        }


        public int getInstantBPM() {
            return (int)((beats * frequency * 60) / (samples * sampleSize));
        }

        public int getBPM() {
            Collections.sort(bpmList);
            return bpmList.get(bpmList.size() / 2);
        }

        public long getSampleSize() {
            return sampleSize;
        }

        public void setSampleSize(long sampleSize) {
            this.sampleSize = sampleSize;
        }

}

public class EnergyOutputAudioDevice extends BaseOutputAudioDevice {
    private int averageLength = 1024; // number of samples over which the average is calculated
    private Queue<Short> instantBuffer = new LinkedList<Short>();

    public EnergyOutputAudioDevice(SampleProcessor processor) {
        super(processor);
    }

    @Override
    protected void outputImpl(short[] samples, int offs, int len) throws JavaLayerException {
        for(int i=0; i<len; i++)
            instantBuffer.offer(samples[i]);

        while(instantBuffer.size()>averageLength*channels)
        {
            long energy = 0;
            for(int i=0; i<averageLength*channels; i++)
                energy += Math.pow(instantBuffer.poll(), 2);

            if(processor != null)
                processor.process(new long[] { energy });
        }
    }

    public int getAverageLength() {
        return averageLength;
    }

    public void setAverageLength(int averageLength) {
        this.averageLength = averageLength;
    }
}

0 个答案:

没有答案