2014-12-01 91 views
0

我創建了一個簡單的應用程序,用戶可以點擊按鈕並播放聲音。一切運作良好,除非我點擊主頁然後返回到應用程序按鈕不工作了,沒有聲音被解僱。當我回到應用程序時,onResume方法被調用,並且mSoundPool不爲空,但我不知道爲什麼聲音沒有被觸發,直到我用BACK按鈕退出應用程序然後返回。按下主頁按鈕並返回到活動狀態後沒有聲音

public class MainActivity extends Activity { 

    ImageButton btn_word, btn_play; 
    TextView tv_text; 
    TextView tv_time, tv_title; 
    String[] words; 

    private MyCountDownTimer mycountDownTimer; 
    private boolean timerHasStarted = false; 

    private int mStream = 1, mStream2 = 2, mStream3 = 3; 
    float streamVolume; 
    AudioManager mAudioManager; 
    int maxStreamNumber=5; //the maximum number of simultaneous streams for this SoundPool object 
    private SoundPool mSoundPool; 
    private HashMap<Integer, Integer> mSoundPoolMap; 

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

     words = getResources().getStringArray(R.array.szavak); 

     btn_play = (ImageButton)findViewById(R.id.btn_2); 
     btn_word = (ImageButton)findViewById(R.id.btn_1); 
     tv_text = (TextView)findViewById(R.id.tv_text); 
     tv_time = (TextView)findViewById(R.id.tv_time); 
     tv_title = (TextView)findViewById(R.id.tv_title); 

     tv_title.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/colonnamt.otf")); 

     tv_text.setText("Új szó"); 
     tv_time.setText("0:59"); 
     btn_play.setImageResource(R.drawable.btn_play); 

     mycountDownTimer = new MyCountDownTimer(59100, 1000); 

     mSoundPool = new SoundPool(maxStreamNumber, AudioManager.STREAM_MUSIC, 0); 
     mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mSoundPoolMap.put(mStream, mSoundPool.load(this, R.raw.szovaltas, 1)); 
     mSoundPoolMap.put(mStream2, mSoundPool.load(this, R.raw.rovid, 2)); 
     mSoundPoolMap.put(mStream3, mSoundPool.load(this, R.raw.hosszu, 3)); 
     streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     streamVolume = streamVolume/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 2.5f; 


     btn_play.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (!timerHasStarted) { 
        mycountDownTimer.start(); 
        timerHasStarted = true; 
        btn_play.setImageResource(R.drawable.btn_stop); 
        mSoundPool.play(mSoundPoolMap.get(mStream2), streamVolume, streamVolume, 1, 0, 1f); 
       } 
       else { 
        mycountDownTimer.cancel(); 
        timerHasStarted = false; 
        btn_play.setImageResource(R.drawable.btn_play); 
        tv_time.setText("0:59"); 
       } 

      } 

     }); 

     btn_word.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String randomStr = words[new Random().nextInt(words.length)]; 
       tv_text.setText(randomStr); 
       mSoundPool.play(mSoundPoolMap.get(mStream), streamVolume, streamVolume, 1, 0, 1f); 
      } 
     }); 



    } 

    @Override 
    public void onResume() { 

     if (mSoundPool != null) { 
      Log.i("RESUME", "not null"); //this is logged out after returning to app 
      mSoundPool.resume(mStream); 
     } else { 
      Log.i("RESUME", "null"); 
     } 
     super.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     Log.i("STATUS", "onPause"); 
     if (mSoundPool != null) mSoundPool.release(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Log.i("STATUS", "onStop"); 
     if (mSoundPool != null) mSoundPool.release(); 
    } 

    @Override 
    public void onDestroy() { 
     Log.i("STATUS", "onDestroy"); 
     super.onDestroy(); 
     if (mSoundPool != null) mSoundPool.release(); 
    } 

    public class MyCountDownTimer extends CountDownTimer { 

     public MyCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     public void onFinish() { 
      tv_time.setText("0:59"); 
      btn_play.setImageResource(R.drawable.btn_play); 
      mSoundPool.play(mSoundPoolMap.get(mStream3 ), streamVolume, streamVolume, 1, 0, 1f); 
     } 

     public void onTick(long millisUntilFinished) { 
      tv_time.setText("0:" + placeZero(millisUntilFinished/1000)); 
     } 
    } 

    public String placeZero(long s) { 
     String c = ""; 
     if (s < 10) { 
      c = "0" + String.valueOf(s); 
     } else { 
      c = String.valueOf(s); 
     } 
     return c; 
    } 


} 

調用

mSoundPool.resume(mStream); 
    mSoundPool.resume(mStream2); 
    mSoundPool.resume(mStream3); 
中的onResume

()不工作。

回答

1

這可能是因爲你在onPause()和onStop()中釋放()你的聲音池時,它們在按下home鍵時都被調用。嘗試soundpool.pause();

相關問題