Why is MediaPlayer playing before TextView setText takes effect?

by Breezy » Tue, 19 Jan 2010 10:41:12 GMT


Sponsored Links
 When a button is clicked the text of a textview changes and a sound is
played, but the sound is played first then the text changes.  I would
like it to happen simultaneously if not then the text to take effect
first.  Here's my code...



        public void playSound(int playingFile)
        {
                        MediaPlayer mp = MediaPlayer.create(getBaseContext(), 
playingFile);
                            mp.start();
                            while(mp.isPlaying())
                            {
                                //i put this in so the mp isn't released in 
middle of the
sound
                            }
                        mp.release();
        }

        public void onClick(View v)
        {
                if(v==button1)
                {

                                maintext.setText("2");
                                maintext.setTextColor(Color.RED);
                                playSound(R.raw.s2);
                }
         }


Can anyone see the problem?  It works, just not as I hoped.  Is there
any way this could be more efficient as well?



Other Threads

1. Debugging App Widgets

I am trying to debug an App Widget and I find that if I set a
breakpoint in the onReceive message, that I can only leave it stopped
in the de{*filter*} for 30 seconds or so, before my process is killed.  If
I look in logcat, I see line about a 'Timeout of broadcast
BroadcastRecord...' and then my process is killed.  Is this because
the Home page is waiting for a response and gives up after 30
seconds?  Is there a way to increase or eliminate this timeout so I
can trace through my code?

Thanks

-- 

2. bindService() instantiating a second Service instance

When memory profiling my app, I've noticed that multiple instances of
the same local service class are getting instantiated.  This is not my
expectation from my interpretation of the APIs and service example
code in the SDKs.

My app is designed such that the service represents a data layer,
handling all databases, networking, and some potential background
operations when the Activity is not in the foreground.  The Activity
is responsible for starting the service by calling bindService(..)
during onResume(..).
   context.bindService( new Intent( context,  MyService.class),
service_connection, Context.BIND_AUTO_CREATE );

To allow the memory allocations of the UI/Activity while the Service
continues (for a limited amount of time), the Activity call
unBindService(..):
   context.unbindService( service_connection );
To be clear, the Service does not call selfStop() right away.  Instead
it notes the time of the last unbind and effectively sets a timer.
This part of the code is working just fine.

Now, when I go in and out of the application, I find I'm binding to a
new instance every time I resume the activity.  I assumed I would bind
to the same Service object each time as long the Service did not stop
itself or the process was shutdown for memory reasons.  However, my
logs and the memory profiler do not show this to be the case.

How can I guarantee my process only has one instance of my Service
class instanciated, and that the Activity will rebind to existing
Service upon Resume?

-- 

3. ACTION_DOCK_EVENT can no longer be sent in 2.2?

4. Slow AES Decryption

5. MediaPlayer StreamVideo question.

6. Custom Keyboard for an App

7. Questions about Custom (Compound) control