如何在原始文件夹中的android中的FileInputStream中设置路径

时间:2011-05-02 12:54:46

标签: android

我必须在FileInputStream中的raw文件夹中指定mp3歌曲的路径。实际上我在java中有这个代码(计算BPM)并转换成android。

package beatit;

import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.player.Player;

/**
 * @author Peter Backx
 */
public class Main {

static Logger log = Logger.getLogger("BeatIt");
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    BPM2SampleProcessor processor = new BPM2SampleProcessor();
    processor.setSampleSize(1024);
    EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);
    output.setAverageLength(1024);

    Player player = new Player(new FileInputStream(args[0]), output); // here i give mp3 from raw instead of arg[0].

    player.play();
    log.log(Level.INFO, "calculated BPM: " + processor.getBPM());
}

}

我尝试了以下内容:

Player player = new Player(new FileInputStream(R.raw.mh), output); 

而不是玩家我使用这个

MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.mh);   

但是我的日志cat文件出错

05-02 17:04:35.180: ERROR/AndroidRuntime(278): Caused by: java.lang.IndexOutOfBoundsException
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     at java.util.LinkedList.get(LinkedList.java:459)
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     at beatit.beatit.BPM2SampleProcessor.getBPM(BPM2SampleProcessor.java:69)
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     at beatit.beatit.Main.onCreate(Main.java:44)
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-02 17:04:35.180: ERROR/AndroidRuntime(278):     ... 11 more

1 个答案:

答案 0 :(得分:0)

使用

getResources().openRawResource(R.raw.mh);

returns您文件的InputStream。