开发者

Media Player on Android stops sounds all together

I have seen alot of posts about it stopping sound early. This is not my problem. My problem is this... I have these 25 sounds up and working. When the phone is shaked it speaks, displays the text, and has an animation so the person looks like he is talking. It works just fine at first. The only way I was able to reproduce the problem that my client has found is by shaking it numerous times, then clicking back. Shaking back into it and shaking numerous times, then clicking back again. When I shake back into it again for a few times it works then suddenly all sound cuts out. Occasionally one will work but if back again and back in it usually stops making sound all together. Everything else functions as it should. Anyone encountered this before? Any help would be appreciated.

public class Ask extends Activity{
private SensorManager mSensorManager;
 private ShakeEventListener mSensorListener;
 String[] answer;
 int possibleAnswers, randomAnswer, talkRun=0, last=-1;
 int shake0;
 long lastClick;
 private MediaPlayer mp, sound0;
Context context;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask);
        Intent type = getIntent();
        lastClick = type.getLongExtra("lastClick", 0);
        context=this;

        final Random generator = new Random();

        possibleAnswers = 1;
        answer = new String[possibleAnswers];
        answer[0]="***Coughing***";



        //Sounds
        setVolumeControlStream(AudioManager.STREAM_MUSIC); 
        mp = MediaPlayer.create(this, R.raw.askbud);
        mp.start();


        mSensorListener = new ShakeEventListener();
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);

      final TextView tv = (TextView)findViewById(R.id.answer);


        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          public void onShake() {
              if (System.currentTimeMillis() - lastClick > 900) {
                  lastClick = System.currentTimeMillis();



                      randomAnswer = generator.nextInt(possibleAnswers);
                      if(last==randomAnswer)
                          randomAnswer = generator.nextInt(possibleAnswers);

                      animation();

                      if(randomAnswer==0){
                        if(shake0>=1){
                            sound0.reset();
                        }
                        shake0++;
                        sound0 = MediaPlayer.create(context, R.raw.coughing);
                        try {
                            sound0.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        sound0.start();
                      }

        });
    }





 @Override
  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_UI);
  }

  @Override
  protected void onStop() {
    mSensorManager.unregisterListener(m开发者_运维问答SensorListener);
    super.onStop();
  }

}

Just a slight addition to the problem. After I get the no sound to occur it continues to occur even after the app is closed and reopened.


Okay, this is a bit much to post in a comment, so here's what you need to do with setDataSource(). If you check the docs for setDataSource(), you'll see that it requires a FileDescriptor. You can acquire one for a Raw resource like so:

Resources res = getResources();
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.coughing);
FileDescriptor fd = afd.getFileDescriptor();

//assuming existing MediaPlayer mp
mp.reset();
mp.setDataSource(fd);
mp.prepare();
mp.start();


From your code, it looks like you are creating MediaPlayer on click. But I dont see any code to stop / release it. So your application may be running out of resources, finally stopping additional media player content. That is why after couple of shake events, this issue is propping up.

Shash

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜