I'm developing an application that responds to certain accelerometer
events such as movements to the left or right when a certain delta is
reached. When these deltas are fired I want to play a sound with the
MediaPlayer.

I'm using the Sensor Simulator by Open Intents to simulate sensor
events. I have the code set up, and I also have some basic
android.util.Log.e(...) statements telling me when these events are
fired. When I simulate the sensor, my deltas are met and my log
statements are output to LogCat. Therefore I know this is working.

However, I now want to play an audio resource file (mp3).

I added a method inside of the SensorListener definition called
PlayMedia(). This simply does the following:


                private void PlayMedia()
                {
                        if(mp != null)
                        {
                                android.util.Log.e("test","mp is not null, 
cleaning up!");
                                if(mp.isPlaying())
                                {
                                        android.util.Log.e("test","Stopping the 
mp!");
                                        mp.stop();
                                }
                        }
                        // is not playing.
                        android.util.Log.e("test","creating a new mp");
                        mp = MediaPlayer.create(getApplicationContext
(),com.MyCompany.Apps.MyApp.R.raw.mySound);

                        android.util.Log.e("test","Start the sound");
                        mp.start();
                        mp.reset();
                }

However, when I calll this, no sound ever happens. The log messages
are output into LogCat. Therefore the code is making it into the
method, yet no sound is being output.

The odd thing is, I opened the ApiDemos and rand the Media demos and
they worked perfectly. The resource apidemo worked and played the
sound in the emulator. However, in my testing, I cannot get it to
play.

I've done the following: Moved the player code outside of the
sensorlistener into the activity and then called the PlayMedia() from
within the nested SensorListner, and it still doesnt work.

The only thing different in this code than in the ApiDemo is that my
MediaPlayer is inside of the SensorListener (as shown below). I also
use getApplicationContext() because in the ApiDemo's the example shows
"this" as it is inside of an Activity.

Why would the sound _not_ play? Its a 56kbps file. I've also taken the
same resource from the APiDemo's and tried to use that and still, no
dice.

I'm also noticing that I'm having to fully qualify my resources (R)
which is kind of odd. Anyone know why that would happen? Would that be
the cause? I've deleted, the gen folder and regnerated it a few times
and I still have to fully qualify my Resources. (I'm using Eclipse).

Does anyone know why this would not work?


Thanks,

Donn

--------------- Code below ------------------

private final SensorListener mListener = new SensorListener() {

        private MediaPlayer mp;

        private final float[] mScale = new float[] { 2, 2.5f, 0.5f }; //
accel

        private float[] mPrev = new float[3];

        public void onSensorChanged(int sensor, float[] values) {
                boolean show = false;
                float[] diff = new float[3];

                for (int i = 0; i < 3; i++) {
                        diff[i] = Math
                                        .round(mScale[i] * (values[i] - 
mPrev[i]) * 0.45f);
                        if (Math.abs(diff[i]) > 0) {
                                show = true;
                        }
                        mPrev[i] = values[i];
                }

                if (show) {
                        // only shows if we think the delta is big enough, in 
an attempt
                        // to detect "serious" moves left/right or up/down
                        android.util.Log.e("test", "sensorChanged " + sensor + 
" ("
                                        + values[0] + ", " + values[1] + ", " + 
values[2] + ")"
                                        + " diff(" + diff[0] + " " + diff[1] + 
" " + diff[2]
                                        + ")");
                }

                long now = android.os.SystemClock.uptimeMillis();
                if (now - mLastGestureTime > 1000) {
                        mLastGestureTime = 0;

                        float x = diff[0];
                        float y = diff[1];
                        boolean gestX = Math.abs(x) > 3;
                        boolean gestY = Math.abs(y) > 3;




                        if ((gestX || gestY) && !(gestX && gestY)) {


                                if (gestX) {
                                        if (x < 0) {
                                                PlayMedia();
                                                android.util.Log.e("test",
                                                                "<<<<<<<< LEFT 
<<<<<<<<<<<<");

                                        } else {
                                                PlayMedia();
                                                android.util.Log.e("test",
                                                                ">>>>>>>>> RITE 
>>>>>>>>>>>");

                                        }
                                } else {
                                        if (y < -2) {
                                                PlayMedia();
                                                android.util.Log.e("test",
                                                                "<<<<<<<< UP 
<<<<<<<<<<<<");

                                        } else {
                                                PlayMedia();
                                                android.util.Log.e("test",
                                                                ">>>>>>>>> DOWN 
>>>>>>>>>>>");

                                        }
                                }
                                mLastGestureTime = now;
                        }
                }
        }


        private long mLastGestureTime;

        public void onAccuracyChanged(int sensor, int accuracy) {
                // TODO Auto-generated method stub

        }

        private void OnActivityDestroyListener() {
                // TODO Auto-generated method stub
                if(mp != null)
                {
                        mp.release();
                        mp = null;
                }
        }

        private void PlayMedia()
        {
                if(mp != null)
                {
                        android.util.Log.e("test","mp is not null, cleaning 
up!");
                        if(mp.isPlaying())
                        {
                                android.util.Log.e("test","Stopping the mp!");
                                mp.stop();
                        }
                }
                // is not playing.
                android.util.Log.e("test","creating a new mp");
                mp = MediaPlayer.create(getApplicationContext
(),com.MyCompany.Apps.MyApp.R.raw.mySound);

                android.util.Log.e("test","Start the sound");
                mp.start();
                mp.reset();
        }

};



----------------end code ----------------------

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to