2011-08-13 153 views
0

當我啓動我的應用程序時,出現主菜單,當我點擊2個按鈕中的任何一個時,它會使按鈕發聲,然後轉到指定的佈局。我遇到的問題是,如果我從佈局返回到主屏幕並嘗試再次按下按鈕,它仍然會將我帶到佈局,但不會使按鈕響起。有任何想法嗎?謝謝您的幫助。按鈕聲音問題

DragonFruitActivity.java

package com.Dragon_Fruit; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageButton; 

public class DragonFruitActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // ***BUTTON SOUND***// 
     final MediaPlayer buttonSound = MediaPlayer.create(
       DragonFruitActivity.this, R.raw.button_click); 

     ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); 
     playbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       arg0.setBackgroundResource(R.drawable.playbuttonselected); 
       // TODO Auto-generated method stub 
       buttonSound.start(); 
       startActivity(new Intent(DragonFruitActivity.this, 
         playbutton.class)); 
      } 

     }); 
     ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); 
     settingsbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       buttonSound.start(); 
       startActivity(new Intent(DragonFruitActivity.this, 
         settingsbutton.class)); 
      } 

     }); 
    } 
} 

更新DragonFruitActivity.java

package com.Dragon_Fruit; 

import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageButton; 

public class DragonFruitActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // ***BUTTON SOUND***// 
     final MediaPlayer buttonSound = MediaPlayer.create(
       DragonFruitActivity.this, R.raw.button_click); 

     ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); 
     playbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       arg0.setBackgroundResource(R.drawable.playbuttonselected); 
       // TODO Auto-generated method stub 
       try { 
        buttonSound.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       buttonSound.start(); 
       startActivity(new Intent(DragonFruitActivity.this, 
         playbutton.class)); 
      } 

     }); 
     ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); 
     settingsbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       try { 
        buttonSound.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       buttonSound.start(); 
       startActivity(new Intent(DragonFruitActivity.this, 
         settingsbutton.class)); 
      } 

     }); 
    } 
} 

logcat的

08-12 21:29:54.379:ERROR/ProfileVideoFrameDrops(5764):PVMediaOutputNodePort ::早期0幀:: OnTime 0幀::晚0幀::總共3幀 08-12 21:29:54.379:錯誤/ ProfileVideoFrameDrops(5764):PVMediaOutputNodePort :: Ear 0.000000%:: OnTime 19518016.000000%::晚-0.000000% 08-12 21:29:58.652:錯誤/ ProfileVideoFrameDrops(5764):PVMediaOutputNodePort :: Early 0 frames :: OnTime 0 frames :: Late 0 frames :: Total 3幀 08-12 21:29:58.652:錯誤/ ProfileVideoFrameDrops(5764):PVMediaOutputNodePort :: Early 0.000000%:: OnTime 19518016.000000%:: Late -0.000000% 08-12 21:30:01.082:ERROR/ProfileVideoFrameDrops( 5764):PVMediaOutputNodePort :: Early 0 frames :: OnTime 0 frames :: Late 0 frames :: Total 3 frames 08-12 21:30:01.082:ERROR/ProfileVideoFrameDrops(5764):PVMediaOutputNodePort :: Early 0.000000%:: OnTime 19518016.000000%::後期-0.000000%

回答

0

編輯:試試這個:

if(buttonSound.isPlaying()) { 
    buttonSound.stop(); 
} 

try { 
    buttonSound.prepare(); 
} catch (IllegalStateException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

buttonSound.start(); 

check out Google MediaPlayer state diagram

+0

好吧,似乎幾分鐘的工作,但現在它回到它開始。我把我的新代碼。它看起來不錯嗎? – Zach

+0

你可以在按下按鈕時將你在LogCat中獲得的東西放到哪裏?我的圖像有一個堆棧跟蹤,每當聲音不播放時就會發生。 – mopsled

+0

我把它放在上面。 – Zach