2

I have an activity that looks like,

public class TestActivity extends Activity {

    private SensorManager      mSensorManager;
        private ShakeEventListener mSensorListener;
    private MediaPlayer        mPlayer;

    private boolean            mIsPlaying; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.testsound);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorListener = new ShakeEventListener();   
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          @Override 
          public void onShake() {
            toggleSound();
          }
        });  
    }

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

    @Override
    protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(mSensorListener);
      stopSound();
    }    

    protected void toggleSound() {
        if (!mIsPlaying) {
          startSound();
        } else {
          stopSound();  
        }
    }


    protected void startSound() {

        if (mPlayer == null)
          {
            mPlayer = MediaPlayer.create(TestActivity.this, R.raw.test);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

            mPlayer.setLooping(true);
            mPlayer.start();  

            mPlayer.setOnCompletionListener(new OnCompletionListener() {

                 @Override
                public void onCompletion(MediaPlayer mp) {
                  stopSound();
                }
            }); 

            mIsPlaying = true;
          }
    }


    protected void stopSound() {
        if (mPlayer != null) {
              if (mPlayer.isPlaying())
              {
                  mPlayer.stop();
              }
              mPlayer.release();
              mPlayer = null;
        }

        mIsPlaying = false;
    }

}

I know this may not be the best way to do it, but I'm just starting out with some random tests. I haven't included the ShakeEventListener, but that pretty much just detects a shake event, upon which a sample sound is played in loop (yes, it's quite annoying!) until the next shake.

This does in fact work really well, except I get LogCat entries with tag MediaPlayer that say start() mUri is null or stop() mUri is null when the start and stop commands are executed. This makes me believe I do something wrong, but I can't find out what it is, nor is this apparently a popular error, when I google around a bit.

Would anyone have an idea what it means here? Sorry if I'm being thick - it's all still a bit confusing to me.

UPDATE (from comments): One thing I did was give it an actual Uri (built from path), and then it comes with a similar warning, now saying start() mUri is android.resource://com.joris.soundtest/2130968576 (level D), preceded this time by an E-level Uri is android.resource//... (dots give same uri). Is it possible that those LogCat entries are just 'informative', for debugging? The E-level one wouldn't sound like it ..

Thanks! -Joris.

  • Try passing context to the startSound method as shown [here](http://stackoverflow.com/questions/12154951/android-mediaplayer-create) – Peshal May 01 '13 at 00:57
  • That didn't change anything to the LogCat outputs, unfortunately. One thing I did was give it an actual Uri (built from path), and then it comes with a similar warning, now saying `start() mUri is android.resource://com.joris.soundtest/2130968576` (level D), preceded this time by an E-level `Uri is android.resource//...` (dots give same uri). Is it possible that those LogCat entries are just 'informative', for debugging? – Joris Peeters May 01 '13 at 08:41

1 Answers1

0

change following thing , because mediaplayer.release() method doesn't make mediaplayer object as null ...

package com.pranav.listviewdemo;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity {

    private SensorManager      mSensorManager;
    private ShakeEventListener mSensorListener;
    private MediaPlayer        mPlayer;
    static int i=1;

    private boolean            mIsPlaying=false; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorListener = new ShakeEventListener(this,null);   
        mPlayer = MediaPlayer.create(this, R.raw.test);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

        mPlayer.setLooping(true);

        mPlayer.setOnCompletionListener(new OnCompletionListener() {

             @Override
            public void onCompletion(MediaPlayer mp) {
              stopSound();
            }
        }); 
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          @Override 
          public void onShake() {
            toggleSound();
            Toast.makeText(getBaseContext(), "DETECTED"+i, Toast.LENGTH_SHORT).show();
            i++;
          }
        });  
    }

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

    @Override
    protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(mSensorListener);
      stopSound();
    }    

    protected void toggleSound() {
        if (!mIsPlaying) {
          startSound();
        } else {
          stopSound();  
        }
    }


    protected void startSound() {

        if (mPlayer != null)
          {
            mPlayer.seekTo(0);
            mPlayer.start();  
            mIsPlaying = true;
          }
    }


    protected void stopSound() {
        if (mPlayer != null) {
              if (mPlayer.isPlaying())
              {
                  mPlayer.pause();
                  mIsPlaying = false;
              }

        }


    }

}
Pranav Jadav
  • 763
  • 5
  • 11
  • I was setting it to NULL myself, in the stopSound(), after release(). That actually seemed to work. I've gone off on some attempts to do the mPlayer.create(..) thing in my OnCreate method and then only starting and stopping in startSound and stopSound, using release() in OnPause. Would that be the more elegant thing to do in the longer run? At present it is giving me all kinds of Fatal Exceptions in any case (Called in Illegal State, etc), but I could attempt to work through them, if it is an improvement over the continuous use of create() and release() + NULL. – Joris Peeters May 01 '13 at 08:47
  • Oh, brilliant, thanks for the effort - I'll give that a go this evening. From a quick inspection, though, it looks like mPlayer is never going to be NULL (which may cause it never to start the sound, in startSound()?), and that there isn't any release (and from what I read online, it's quite mandatory that this be included, for resource purposes). I might well be missing something .. I usually only properly understand code once I tried it out. :) – Joris Peeters May 01 '13 at 16:37
  • http://stackoverflow.com/questions/16280751/how-can-i-make-android-mediaplayer-play-sound/16281261#16281261 – Pranav Jadav May 01 '13 at 19:48