2012-02-27 89 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