2017-02-24 53 views
-1

我試圖做一些簡單的應用程序,如點擊按鈕,播放聲音。 在我的最後一個應用程序中,我用開關盒完成了所有工作,但是我意識到如果我有很多代碼,我必須爲每個按鈕編寫很多代碼。 現在我試圖將按鈕和聲音保存到數組中,並將它們循環放入雙精度數組中。陣環Android Studio中

我還做了哪些工作正常一些上下文菜單。但現在我的問題是播放聲音。實際上它會播放兩個按鈕的聲音2。 > Sound1例子和BUTTON2 - - 它應該爲Button1發揮> SOUND2

有人可以在這裏找到我的問題?

public class MainActivity extends AppCompatActivity { 

    MediaPlayer MainMedia; 

    public int [] buttonsas = {R.id.button1, R.id.button2}; 
    public int [] sounds = {R.raw.sound1, R.raw.sound2}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     for (int i=0; i<buttonsas.length; i++) { 
      Button contextMenuButton = (Button) findViewById(buttonsas[i]) ; 
      registerForContextMenu(contextMenuButton); 
     } 

     MainMedia = MediaPlayer.create(this, R.raw.sound1); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     switch (item.getItemId()){ 
      case R.id.item_option1: 
      case R.id.item_option2: 
       Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show(); 
       break; 
      case R.id.item_option3: 
       Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show(); 
       break; 
     } 

     return super.onContextItemSelected(item); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     getMenuInflater().inflate(R.menu.main_menu, menu); 
    } 

    public void MainMedia (View view) { 
     for (int i=0; i<buttonsas.length; i++) { 
      for (int j=0; j<sounds.length; j++) { 
       MainMedia.release(); 
       MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]); 
       MainMedia.start(); 
      } 
     } 
    } 
} 
+0

你爲什麼不創建一個方法,並通過與聽衆的onclick聲音文件路徑,以便您的代碼會更乾淨? – madroid

+0

你好,因爲我對Java有點新鮮。你有一點教程嗎? – ExiizZ

回答

0

如果我正確地理解你的代碼,在方法MainMediaIS實際播放兩種聲音的兩個按鈕,但它只是好像是在打第二個,因爲第一個是幾乎立即釋放似乎。

老實說,我認爲有很多更簡潔和減少處理解決方案,但如果你想做到這一點的兩個嵌套for那麼我認爲一個解決辦法是:

嘗試把一個if語句中的第二for所以你以後可以知道哪個button您按:

public void MainMedia (View view) { 
    for (int i=0; i<buttonsas.length; i++) { 
     for (int j=0; j<sounds.length; j++){ 
      if (view.getId() == buttonsas[i] && i == j) { 
       MainMedia.release(); 
       MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]); 
       MainMedia.start(); 
      } 
     } 
    } 
} 

雖然我覺得這個解決方案應該工作,我建議做這樣的事情,更好:

public void MainMedia (View view) { 
    // Traverse the buttonsas array to get the item the user just pressed. 
    for (int i=0; i<buttonsas.length; i++) { 
     // Check whether the item we got from the array is equal to the item we received as a parameter 
     if (view.getId() == buttonsas[i]) { 
      MainMedia.release(); 

      // Since the sounds and buttons have a relation in the same position in two different arrays, 
      // just get the position we have depending on the selected item. 
      MainMedia = MediaPlayer.create(MainActivity.this, sounds[i]); 
      MainMedia.start(); 
     } 
    } 
} 

這樣你將保存一個for循環。

讓我知道這是否有幫助。

編輯:我加入了代碼中的註釋,以便更容易跟隨。簡而言之,它的工作原理是因爲你有item - sound兩個單獨的數組的關係,所以item1R.id.button1)爲position1的(buttonsas[1]array1buttonsas),這樣一來,當我得到的項目位置我想,我只是使用相同的位置array2sounds[1])。由於您正在使用迭代器(for循環),所以該位置是i

+0

哦謝謝很多:)兩種方法的工作,但我想我必須在雙循環中使用它來獲得每個位置的聲音。現在我想問你推薦的一個。我可以閱讀這個循環他選擇按鈕1,2,3等,但你如何解釋按鈕1 - > sound1(播放)? – ExiizZ

+0

@ExiizZ查看我剛纔對帖子所做的修改。 – skw

+0

哦謝謝,這幫了我很多。我還有一個問題。所以我也建立一個上下文菜單,因爲我想用它與Option1 if(view.getId()== buttonsas [i] && i == j)< - 但這是行不通的,因爲我沒有視圖那裏。有沒有辦法解決這個問題,或更好地留在點擊按鈕的方法? – ExiizZ

0

問題出在你的循環中。你實際上是在同一時間開始所有的聲音。什麼試圖用這個來實現?

+0

你好,正如我在頂部所說。例如,我有3個按鈕和3個聲音文件。我想爲button1設置 - sound1,button2 - sound 2,button3 - sound3。所以 – ExiizZ