2011-08-13 114 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