2011-12-29 52 views
1

我只用了幾個星期的Android和Java編程,試圖製作一個吉他調諧器來調整您的吉他。一個簡單的參考調諧器。我已經使用了很多這個網站代碼來構建它。Android停止SoundPool跨多個活動循環。或者在更改頁面時停止所有播放聲音

當我點擊另一個頁面/活動時,如何讓SoundPool停止。

目前我有兩頁,每頁有7個聲音按鈕。當你點擊任何按鈕時,它會無限循環,直到你點擊另一個按鈕,然後循環等等。如果我點擊下一頁的按鈕,然後點擊一個新按鈕,最後一頁的聲音仍在播放!當你試圖用耳朵調整你的吉他時會感到煩惱!

也許只是一個簡單的停止按鈕來停止整個SoundPool將是一個好主意?

這是我的SoundManager代碼 package org.gtdb.guitar_tuners;

import android.content.Context; 
    import android.media.AudioManager; 
    import android.media.SoundPool; 

    public class SoundManager 
    { 
     private Context pContext; 
     private SoundPool sndPool; 
     private float rate = 1.0f; 
     private float masterVolume = 1.0f; 
     private float leftVolume = 1.0f; 
     private float rightVolume = 1.0f; 
     private float balance = 0.5f; 

     // Constructor, setup the audio manager and store the app context 
     public SoundManager(Context appContext) 
     { 
      sndPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100); 
      pContext = appContext; 
     } 

     // Load up a sound and return the id 
     public int load(int sound_id) 
     { 
      return sndPool.load(pContext, sound_id, 1); 
     } 

     // Play a sound 
     public void play(int sound_id) 
     { 
      sndPool.play(sound_id, leftVolume, rightVolume, 100, -1, rate);  
     } 


     // Set volume values based on existing balance value 
     public void setVolume(float vol) 
     { 
      masterVolume = vol; 

      if(balance < 1.0f) 
      { 
       leftVolume = masterVolume; 
       rightVolume = masterVolume * balance; 
      } 
      else 
      { 
       rightVolume = masterVolume; 
       leftVolume = masterVolume * (2.0f - balance); 
      } 

     } 

     public void setSpeed(float speed) 
     { 
      rate = speed; 

      // Speed of zero is invalid 
      if(rate < 0.01f) 
       rate = 0.01f; 

      // Speed has a maximum of 2.0 
      if(rate > 2.0f) 
       rate = 2.0f; 
     } 

     public void setBalance(float balVal) 
     { 
      balance = balVal; 

      // Recalculate volume levels 
      setVolume(masterVolume); 
     } 



     // Free ALL the things! 
     public void unloadAll() 
     { 
      sndPool.release();  
     } 

    } 

這是我的活動代碼Main.java 包org.gtdb.guitar_tuners;

import android.app.Activity; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.Window; 
    import android.view.WindowManager; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.ImageButton; 
    import android.widget.ImageView; 
    import android.widget.Toast;   
    import com.google.ads.AdRequest; 
    import com.google.ads.AdSize; 
    import com.google.ads.AdView; 

    import android.widget.LinearLayout; 

    public class Main extends Activity { 

     SoundManager snd; 
     int string1, string2, string3, string4, string5, string6, string7; 
     OnClickListener buttZonClick; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      setContentView(R.layout.main); 

      LinearLayout layout = (LinearLayout) findViewById(R.id.adMobUnit); 

      // Create the adView 
      // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID 
      AdView adView = new AdView(this, AdSize.IAB_MRECT, "aaaaaaaaaaaaa"); 

      // Add the adView to it 
      layout.addView(adView); 

      // Initiate a generic request to load it with an ad 
      AdRequest request = new AdRequest(); 
      request.setTesting(true); 

      adView.loadAd(request); 

      // Create an instance of our sound manger 
      snd = new SoundManager(getApplicationContext()); 

      // Load the samples from res/raw 
      string1 = snd.load(R.raw.eadgbe1); 
      string2 = snd.load(R.raw.eadgbe2); 
      string3 = snd.load(R.raw.eadgbe3); 
      string4 = snd.load(R.raw.eadgbe4); 
      string5 = snd.load(R.raw.eadgbe5); 
      string6 = snd.load(R.raw.eadgbe6); 
      string7 = snd.load(R.raw.eadgbestrum); 

      /** Tuner Link **/ 
      Button b1 = (Button) findViewById(R.id.tuner1); 
      b1.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        startActivity(new Intent(Main.this, Dadgad.class)); 
       } 
      }); 
      /** Tuner Link **/ 
      Button b2 = (Button) findViewById(R.id.tuner2); 
      b2.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        startActivity(new Intent(Main.this, Daefce.class)); 


       } 
      }); 

      /** gtdb.org Link **/ 
      ImageButton b7 = (ImageButton) findViewById(R.id.imageButton1); 
      b7.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        Intent i = new Intent(Intent.ACTION_VIEW, Uri 
          .parse("http://www.gtdb.org/stats/click.php?id=17")); 
        startActivity(i); 
       } 
      }); 

     } 

     public int count = 0; 
     // Button listener assigned in XML layout 
     public void clickHandler(View v) { 
      int id = v.getId(); // Use the button id to determine which sample 
           // should be played 

      switch (id) { 
      case R.id.button1: 
        snd.play(1); 
       break; 

      case R.id.button2: 
       snd.play(2); 
       break; 

      case R.id.button3: 
       snd.play(3); 
       break; 

      case R.id.button4: 
       snd.play(4); 
       break; 

      case R.id.button5: 
       snd.play(5); 
       break; 

      case R.id.button6: 
       snd.play(6); 
       break; 

      case R.id.button7: 
       snd.play(7); 
       break; 
      } 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      if (item.getItemId() == R.id.item1) { 
       Toast.makeText(Main.this, 
         "Android Guitar Tuner App from Guitar Tunings Database - www.gtdb.org", 
         Toast.LENGTH_LONG).show(); 
      } 
      return super.onOptionsItemSelected(item); 
     } 

    } 

道歉的代碼,我可以想象它是一個爛攤子,因爲我基本上是在Java和Android的新手。我一直在從其他人的代碼和觀看lynda.com視頻拼湊在一起。

我的調諧器的當前版本是在市場上,但它沒有循環的聲音,所以你可以通過網頁和聽。但沒有一個循環,你必須不斷按下按鈕來調整你的吉他!你可以看到here

回答

0

請問Soundpool.stop(int)是否適合你?如果是這樣,爲什麼不重寫Activity.onPause()方法並將其放在那裏?

+0

感謝您的回覆,但是由於我提出了這個問題,我已經重寫了佈局以將所有頁面放置在一個頁面上,使用可見性來隱藏和顯示頁面元素。 我剛剛完成併發布了應用程序的第2版,並且正在考慮重新進行一次全新的重寫。我會在那時嘗試Soundpool.stop(int)。 再次感謝您的時間。 – warmwhisky 2012-01-03 08:56:55