需要帮助调试我的代码

发布于 2024-12-12 01:45:42 字数 3603 浏览 0 评论 0原文

我需要一些关于我的代码的输入。 基本上,我有一个从 A 类加载音乐的方法

public void onListItemClick(ListView parent, View v, int position, long id){
    musicIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    cursor.moveToPosition(position);
    filePath = cursor.getString(musicIndex);
    fileName = new File(filePath).getName();
    playMusic();//Play the selected music
}

public void playMusic(){
    if(mPlayer.isPlaying()){
        mPlayer.reset();
    }
    try{
        mPlayer.setDataSource(filePath);
        mPlayer.prepare();
        mPlayer.start();
        BeatDetection beatDetect = new BeatDetection();
        beatDetect.init();
    }catch (Exception e){

    }
}

该方法将调用 B 类中的 init() 方法

public void init() throws Exception{
    energy = 0;
    variance = 0;
    constant = 0;
    isBeat = false;
    sensitivity = 0;
    dBuffer = new float[sampleRate / bufferSize];
    eBuffer = new float[sampleRate / bufferSize];
    timer = System.currentTimeMillis();
    MusicLoad msc = new MusicLoad();

    totalMs = 0;
    seeking = true;
    //msc.printText();
    decode(msc.fileName, 25, 40);
}

在该方法中,它会初始化所有内容并调用decode() > method

public void decode(String path, int startMs, int maxMs)
  throws IOException, javazoom.jl.decoder.DecoderException {

    debug();
    File in = new File(path);
    InputStream inStream = new BufferedInputStream(new FileInputStream(in), 8 * 1024);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
    try {
         Bitstream bitstream = new Bitstream(inStream);
        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 javazoom.jl.decoder.DecoderException("mono or non-44100 MP3 not supported", null);
                    }

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

        byte[] abAudioData = outStream.toByteArray();
        calculation(abAudioData);
    } catch (BitstreamException e) {
        throw new IOException("Bitstream error: " + e);
    } catch (DecoderException e) {
        Log.w("Decoder error", e);
        throw new javazoom.jl.decoder.DecoderException("Error",e);
    } finally {
        inStream.close();
    }
}

不要介意阅读所有代码行。如果你们注意到我在开头放置了 debug() 来查看该方法是否被调用。此时,debug() 已正确调用。但是,如果我将 debug() 放在 File in = new File(path); 行之后,则 debug() 将不会不再打电话了。似乎代码在那时停止运行。

最终的结果是,我可以毫无问题地加载和播放歌曲。但是,decode() 没有被调用,也没有任何错误。我现在一直坚持指出问题。因此,如果有任何意见请帮助我。

编辑:在我尝试跟踪“path”变量后,它返回 NULL,因此错误是 NullPointerException。似乎 A 类的“fileName”变量没有传递给 B 类。有什么建议吗?

I need some input about my code.
Basically, I have a method to load music from Class A

public void onListItemClick(ListView parent, View v, int position, long id){
    musicIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    cursor.moveToPosition(position);
    filePath = cursor.getString(musicIndex);
    fileName = new File(filePath).getName();
    playMusic();//Play the selected music
}

public void playMusic(){
    if(mPlayer.isPlaying()){
        mPlayer.reset();
    }
    try{
        mPlayer.setDataSource(filePath);
        mPlayer.prepare();
        mPlayer.start();
        BeatDetection beatDetect = new BeatDetection();
        beatDetect.init();
    }catch (Exception e){

    }
}

That method will call the init() method in Class B

public void init() throws Exception{
    energy = 0;
    variance = 0;
    constant = 0;
    isBeat = false;
    sensitivity = 0;
    dBuffer = new float[sampleRate / bufferSize];
    eBuffer = new float[sampleRate / bufferSize];
    timer = System.currentTimeMillis();
    MusicLoad msc = new MusicLoad();

    totalMs = 0;
    seeking = true;
    //msc.printText();
    decode(msc.fileName, 25, 40);
}

In that method, it initializes everything and call the decode() method

public void decode(String path, int startMs, int maxMs)
  throws IOException, javazoom.jl.decoder.DecoderException {

    debug();
    File in = new File(path);
    InputStream inStream = new BufferedInputStream(new FileInputStream(in), 8 * 1024);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
    try {
         Bitstream bitstream = new Bitstream(inStream);
        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 javazoom.jl.decoder.DecoderException("mono or non-44100 MP3 not supported", null);
                    }

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

        byte[] abAudioData = outStream.toByteArray();
        calculation(abAudioData);
    } catch (BitstreamException e) {
        throw new IOException("Bitstream error: " + e);
    } catch (DecoderException e) {
        Log.w("Decoder error", e);
        throw new javazoom.jl.decoder.DecoderException("Error",e);
    } finally {
        inStream.close();
    }
}

Don't mind reading all the code lines. If you guys notice I put debug() in the beginning to see whether the method is called or not. At this point, the debug() is properly called. However, if I put the debug() after the line File in = new File(path);, the debug() will not be called anymore. It seems like the code is stop running at that point.

The ultimate result is, I can load and play the song without any problem. However, the decode() is not called and there is no error whatsoever. I'm stuck at pointing out the problem at this point. So if there's any input please help me.

EDIT: After I tried tracing the "path" variable, it returns NULL so the error is NullPointerException. Seems like the "fileName" variable from Class A is not passed to Class B. Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜访吸血鬼 2024-12-19 01:45:42

如果您将 Eclipse 与 ADT 一起使用,那么调试 Android 应用程序非常容易,只需添加一个断点(可能在 new File(...) 行中)并看看会发生什么。

我的猜测是 File in = new File(path); 可能在您的 decode 方法中抛出 IOException,该异常首先冒泡到 init() 然后到 playMusic(),在那里它被 try catch 块捕获。你的渔获是空的,所以你什么也看不到。尝试按照我所说的进行调试,或者在 catch 块中添加一些日志信息。

If you are using Eclipse with ADT then it's very easy to debug your Android apps, just add a breakpoint (probably in the new File(...) line) and see what happens.

My guess here is that File in = new File(path); probably is throwing a IOException in your decode method, that exception is bubbling first to init() and then to playMusic(), where it is caught by try catch block. Your catch is empty so you are not seeing anything. Try debugging as I said or add some logging info in the catch block.

萌吟 2024-12-19 01:45:42

这只是要看一下,但是从文档页面

http://developer.android.com/reference/java/io/File.html#File%28java.lang.String%29

“文件引用的实际文件可能存在,也可能不存在。尽管名称为文件,但它也可能是目录或其他非常规文件。”

如果您的路径错误,则它可能正在尝试创建该文件,而您可能没有正确的权限来执行此操作。也许:WRITE_EXTERNAL_STORAGE。

This is just something to look at, but from the doc page

http://developer.android.com/reference/java/io/File.html#File%28java.lang.String%29

"The actual file referenced by a File may or may not exist. It may also, despite the name File, be a directory or other non-regular file."

If you had the path wrong, it may be trying to create the file and you may not have the correct permission to do so. Perhaps: WRITE_EXTERNAL_STORAGE.

命硬 2024-12-19 01:45:42

我知道这篇文章很旧,但我只是想展示如何获取文件路径来为遇到这篇文章的其他人读取/写入文件,因为我有:

String filePath = myContext.getFilesDir().getPath().toString() + "/sysout.log";
File file  = new File(filePath);

这两行将创建(如果存在,则打开,并覆盖)文件夹 /data/data/com.app.name/files/ 中名为“sysout.log”的文件; myContext 只是当前上下文。使用此技术可以缓解定义您自己的路径名的问题。希望这对某人有帮助。

I know this post is old, but I just wanted to show how to get the file path to read/write files for others that come across this post as I have:

String filePath = myContext.getFilesDir().getPath().toString() + "/sysout.log";
File file  = new File(filePath);

These two lines will create (open if it exists, and overwrite) a file named "sysout.log" in the folder /data/data/com.app.name/files/; myContext is just the current context. Using this technique alleviates problems with defining your own path name. Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文