2012-02-27 88 views
0

在我的android音板中,所有按钮都可以正常工作,声音可以正常播放,但存在问题。问题是,当你按下应用程序中的按钮时,你可以按其他按钮。例如,如果我按下的按钮播放噪音声,我可以按另一个按钮,同时播放两个声音。这不是我想要的。有没有我可以添加的代码,以便我可以一次只点击一个按钮,或者一些代码可以让以前的声音停止,并且它会播放新选择的声音?Eclipse:Android Soundboard按钮可以“过度点击”

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.soundboard" 
    android:versionCode="1" 
    android:versionName="1.2"> 
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="false" android:allowClearUserData="true"> 
    <activity android:label="Vegeta Soundboard" android:screenOrientation="portrait" android:name="Soundboard" android:icon="@drawable/icon"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


</application> 

<uses-sdk android:minSdkVersion="3" /> 


</manifest> 

回答

1

最简单的事情是只是有一个soundPlaying布尔值,是全球性的,你当用户点击一个按钮的声音,这是由声中结束未设置设置。

public class Main extends Activity{ 




    //global variable, notice how it's not in any method or inner class 
    //so it's accessible to all the members of this class 
    private boolean soundPlaying = false; 




    @Override 
     public void onCreate(Bundle b){ 
      super.onCreate(b); 
     } 


    //Somewhere in here you have your onclick function that you have called from your xml button 
    public void playSound(View v){ 
    if(soundPlaying) 
     return; 
    soundPlaying = true; 
//myMedia is your media player object 

    myMedia.start();//this will only work if you have a media player set up with media 

    myMedia.setOnCompletionListener(new OnCompletionListener() {    
      @Override 
      public void onCompletion(MediaPlayer mp) { 
       soundPlaying = false;//here you set it to false cause the sound is done 

      } 
     }); 
    } 





    } 
+0

非常感谢你!!!! – Hugthug 2012-02-27 01:07:23

+0

好吧,所以我遇到了另一个问题。如何将全局变量合并到Android应用程序中,以便在我的手机上工作? – Hugthug 2012-02-27 02:26:30

+0

我不确定我明白你的意思。当你使用手机时,你想看到什么?假设你在你的应用程序中,你希望将来电的效果是什么? – 2012-02-27 05:39:07

-1
public class newBoard extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 

     Toast.makeText(this, "Thank you for using this App.", Toast.LENGTH_LONG).show(); 

     // ads - load request to display 
     AdView layout = (AdView)this.findViewById(R.id.adView); 

     // ads - load display with an ad 
     AdRequest adRequest = new AdRequest(); 
     adRequest.setTesting(true); 

     layout.loadAd(adRequest); 

     // import sound files 
     final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01); 
     final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02); 
     final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03); 
     final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04); 
     final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05); 
     final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06); 
     final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07); 
     final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08); 
     final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09); 
     final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10); 
     final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11); 
     final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12); 
     final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13); 
     final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14); 
     final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15); 
     final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16); 
     final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17); 
     final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18); 
     final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19); 
     final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20); 
     final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21); 
     final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22); 
     final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23); 
     final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24); 
     final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25); 

     // play sound files on clicks 
     Button s01 = (Button) findViewById(R.id.button01); 
     s01.setText(this.getString(R.string.quote01)); 
     s01.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound01.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound01.start();     
       } 

     }); 
     registerForContextMenu(s01); 

     Button s02 = (Button) findViewById(R.id.button02); 
     s02.setText(this.getString(R.string.quote02)); 
     s02.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound02.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound02.start(); 
      } 
     }); 
     registerForContextMenu(s02); 

     Button s03 = (Button) findViewById(R.id.button03); 
     s03.setText(this.getString(R.string.quote03)); 
     s03.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound03.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound03.start(); 
      } 
     }); 
     registerForContextMenu(s03); 

     Button s04 = (Button) findViewById(R.id.button04); 
     s04.setText(this.getString(R.string.quote04)); 
     s04.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound04.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound04.start(); 
      } 
     }); 
     registerForContextMenu(s04); 

     Button s05 = (Button) findViewById(R.id.button05); 
     s05.setText(this.getString(R.string.quote05)); 
     s05.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound05.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound05.start(); 
      } 
     }); 
     registerForContextMenu(s05); 

     Button s06 = (Button) findViewById(R.id.button06); 
     s06.setText(this.getString(R.string.quote06)); 
     s06.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound06.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound06.start(); 
      } 
     }); 
     registerForContextMenu(s06); 

     Button s07 = (Button) findViewById(R.id.button07); 
     s07.setText(this.getString(R.string.quote07)); 
     s07.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound07.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound07.start(); 
      } 
     }); 
     registerForContextMenu(s07); 

     Button s08 = (Button) findViewById(R.id.button08); 
     s08.setText(this.getString(R.string.quote08)); 
     s08.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound08.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound08.start(); 
      } 
     }); 
     registerForContextMenu(s08); 

     Button s09 = (Button) findViewById(R.id.button09); 
     s09.setText(this.getString(R.string.quote09)); 
     s09.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound09.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound09.start(); 
      } 
     }); 
     registerForContextMenu(s09); 

     Button s10 = (Button) findViewById(R.id.button10); 
     s10.setText(this.getString(R.string.quote10)); 
     s10.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        sound10.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sound10.start(); 
      } 
     }); 
     registerForContextMenu(s10); 
+0

上面的代码会在这里进入(你的方向似乎令人困惑)我试图让同样的事情发生(对不起,我是一个noob – 2012-07-30 23:05:23