2011-10-27 176 views
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){ 

    } 
} 

类加载音乐也就是说方法将调用在乙

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

类的init()方法在该方法中的方法,它初始化一切并调用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()未被调用,并且没有任何错误。在这一点上,我一直在指出问题。所以,如果有任何输入,请帮助我。

编辑:我试图跟踪“路径”变量后,它返回NULL,所以错误是NullPointerException。似乎来自类A的“fileName”变量不会传递给类B.任何建议?

+0

File的定义在哪里? – Soren

+0

看看'playMusic()'中是否抛出异常,把'e。在catch块中找到printStackTrace()' – Reno

+0

想通了问题,我编辑了问题 –

回答

0

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

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

+0

想通了问题,我编辑了问题 –

+0

使用我提到的调试工具。跟踪路径变量。 – aromero

0

我知道这个帖子是老了,但我只是想说明如何获取文件路径为读/写文件其他人遇到这个职位,因为我有:

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

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