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.