按下主页按钮并返回活动后没有声音

时间:2014-12-01 17:39:15

标签: android soundpool

我创建了一个简单的应用程序,用户可以点击按钮播放声音。一切顺利,除非我单击HOME然后返回应用程序按钮不再工作,没有声音被触发。当我返回应用程序时,调用onResume方法并且mSoundPool不为null,但我不知道为什么在我使用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 个答案:

答案 0 :(得分:1)

这可能是因为你在onPause()和onStop()中释放()你的soundpool,它们在按下home按钮时都被调用。试试soundpool.pause();