views:

59

answers:

3

Hi,

I try to play a sound from R.raw. inside a Thread/Runnable But I can't get this to work.

new Runnable(){ 
  public void run() {  

    //this is giving me a NullPointerException, because getBaseContext is null  
    MediaPlayer mp = MediaPlayer.create( getBaseContext(), R.raw.soundfile);  

    while (true) {  
      if (something)  
          play something  
    }  
  }

How can I get the real Context inside the run method? It is null no matter what I try. Or is there a better way to do this?

A: 

I guess you need to create a Thread and call Thread.start().

shernshiou
A: 

You need to declare a Handler object in your UI thread.

Then in your Thread use

YourHandler.post(new Runnable() {
    public void run() {
        //do something to the UI (i.e. play something)
    }});
Miguel Morales
I have a handler and that extra Runnable. But if I want to access that extra Runnable I need to declare it as a class variable and so there is no Context and getBaseContext is null
oggy
Why is there no context? You can save it in your onCreate, or right before your start your thread: final Context myContext = ...; or extend your handler intializer like YourHandler(Context c) { mGlobalContext = c } ...
Miguel Morales
that's what I am trying to figure out. If I declare a class variable Context c; and do c = getBaseContext(); in the onCreate Method I can print it out in the onCreate Method and it gives me something. If I print context in the Runnable it gives me null no matter what
oggy
Hmm, have you tried saving the 'this' object, like on your onCreate() { final Context MyContext = this; } .... There should be no reason MyContext would be null unless that runnable code gets reached before the variable is initialized.
Miguel Morales
if I do: final Context c = this; in the onCreate Method then the Runnable Method can't see the context - or did I get something wrong?
oggy
Yes, you have several options on passing the context to the runnable. Pass the context to the Runnable when it is being initialized. Seems that it's just a coding error.
Miguel Morales
A: 

You should use getBaseContext. Instead, if this runnable is within an activity, you should store the context in a class variable like this:

public class MainActivity extends Activity {
    private Context context;

    public void onCreate( Bundle icicle ) {
        context = this;


        // More Code
    }

    // More code

    new Runnable(){ 
        public void run() {  
            MediaPlayer mp = MediaPlayer.create(context, R.raw.soundfile);  

            while (true) {  
                if (something)  
                    play something  
            }  
        }
    }
}

Also you shouldn't have an infinite loop like that playing a sound over and over. There should be a sleep in there so you don't get ear-raped by billions of the same sound playing one after another.

AndrewKS
I tried that. But the context inside the run() of the Runnable of the Thread is null. Even with the class variable. The class extends Activity.
oggy