Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按下主页按钮并返回活动后没有声音_Android_Soundpool - Fatal编程技术网

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

Android 按下主页按钮并返回活动后没有声音,android,soundpool,Android,Soundpool,我创建了一个简单的应用程序,用户可以点击按钮播放声音。一切正常,但当我点击HOME然后返回应用程序时,按钮不再工作,没有声音。当我返回到应用程序时,onResume方法被调用,mSoundPool不为null,但我不知道为什么声音没有被激发,直到我用后退按钮退出应用程序,然后返回 public class MainActivity extends Activity { ImageButton btn_word, btn_play; TextView tv_text; T

我创建了一个简单的应用程序,用户可以点击按钮播放声音。一切正常,但当我点击HOME然后返回应用程序时,按钮不再工作,没有声音。当我返回到应用程序时,onResume方法被调用,mSoundPool不为null,但我不知道为什么声音没有被激发,直到我用后退按钮退出应用程序,然后返回

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;
    }


}

在onResume中()不起作用。

可能是因为您在onPause()和onStop()中释放了()您的声音池,这两个选项在按下home按钮时都会被调用。试试soundpool.pause()

  mSoundPool.resume(mStream);
  mSoundPool.resume(mStream2);
  mSoundPool.resume(mStream3);