需要帮助调试我的代码

时间:2011-10-27 05:11:29

标签: android debugging audio

我需要一些关于我的代码的输入。 基本上,我有一种从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()方法

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类。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

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

我的猜测是,File in = new File(path);可能在您的decode方法中抛出IOException,该异常首先冒泡到init()然后再冒到playMusic(),它在那里被try catch阻止了。你的捕获是空的,所以你没有看到任何东西。按照我的说法尝试调试或在catch块中添加一些日志信息。

答案 1 :(得分:0)

这只是一个值得关注的内容,但是来自doc页面

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

  

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

如果您的路径有问题,可能是在尝试创建该文件,而您可能没有正确的权限。也许:WRITE_EXTERNAL_STORAGE。

答案 2 :(得分:0)

我知道这篇文章已经过时了,但我只想展示如何获取文件路径来读取/写入其他人的文件,因为我有:

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

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

相关问题