2012-11-27 92 views
2

我正在开发一个音乐应用程序,其中正在使用一个Seekbar。我有一个方法可以处理seekbar的性能,而且效果很好。但如果搜索条是在行动有内存泄漏和日志猫节目如Android TextView内存泄漏

GC_CONCURRENT freed 1692K, 34% free 10759K, paused 3ms+8ms 

这个不断到来时,搜索条进展

我发现,这个问题是由于的TextView的更新用,动态地显示当前歌曲的持续时间。

我该如何解决这个问题。请帮我这个问题

我的功能是

public void seekbarProgress(){ 

     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       //current position of the play back 
       currPos = harmonyService.player.getCurrentPosition(); 
       if(isPaused){ 
        //if paused then stay in the current position 
        seekBar.setProgress(currPos); 
        //exiting from method... player paused, no need to update the seekbar 
        return; 
       } 

       //checking if player is in playing mode 
       if(harmonyService.player.isPlaying()){ 
        //settting the seekbar to current positin 
        seekBar.setProgress(currPos); 
        //updating the cuurent playback time 
        currTime.setText(getActualDuration(""+currPos)); 
        handler.removeCallbacks(this); 
        //callback the method again, wich will execute aftr 500mS 
        handler.postDelayed(this, 500); 
       } 
       //if not playing... 
       else{ 
        //reset the seekbar 
        seekBar.setProgress(0); 
        //reset current position value 
        currPos = 0; 
        //setting the current time as 0 
        currTime.setText(getActualDuration(""+currPos)); 
        Log.e("seekbarProgress()", "EXITING"); 
        //exiting from the method 
        return; 
       } 
      } 
     }, 500); 
    } 
+0

为什么要用每个搜索栏进度更新曲目的长度?您可以在MediaPlayer准备好之后更新一次。 – Rotem

+0

我想在一个文本视图中更新当前搜索时间,还有一个文本视图用于歌曲的整个持续时间,此文本视图用于正在播放的歌曲的播放时间。 – AndEngine

+0

您多久更换一次seekbar进度?如果在文本中不显示毫秒,则可以根据搜索栏创建一个每秒更新TextView的线程。 – Rotem

回答

2

GC_CONCURRENT线并不意味着你有内存泄漏。这只是垃圾收集器清理未使用的内存。

编辑 这条线:

currTime.setText(getActualDuration(""+currPos)); 

不会造成内存泄漏(除非getActualDuration()做一些有趣的事情)。它只是每500毫秒创建一个新的String,但它不是内存泄漏。旧的将会像你的情况一样被垃圾收集。

+0

谢谢,但是,在这种情况下,如何避免留下未使用的内存。当我注释掉这一行 currTime.setText(getActualDuration(“”+ currPos)); 没有GC_CONCURRENT,释放未使用的内存。所以我很困惑 – AndEngine

+0

你在说什么?在Java中,你通常不需要担心内存泄漏问题。如果有一个未使用的对象,它将自动释放(除非在某处保留对它的引用)。 – Caner

+0

(working on Android) this line currTime.setText(getActualDuration(“”+ currPos)); 在Log cat中,类似于这个消息的连续不断。 GC_CONCURRENT释放1692K,34%免费10759K,暂停3毫秒+ 8ms的 会造成任何性能问题, – AndEngine