10

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
oggy
  • 101
  • 1
  • 1
  • 4

5 Answers5

32

You should also be able to get the this reference from the outer class by using MainActivity.this.

user338519
  • 2,189
  • 1
  • 15
  • 8
14

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 in order to prevent the sound from playing over and over in a small amount of time and overlapping the same sounds with each other.

AndrewKS
  • 3,603
  • 2
  • 24
  • 33
  • 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 Oct 06 '10 at 18:40
  • Thanks for this answer it helped me after about a month of wondering how to do this. Thank you!! – deucalion0 Mar 23 '13 at 09:01
  • You shouldn't have to store a reference to itself within an instance. If you're going to store a reference to the activity it would be more correct to pass it to the runnable, but not store `this` within the activity itself. The `ActivityClass.this` is the correct answer in this case – Yuri Brigance May 04 '17 at 22:01
0

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

shernshiou
  • 518
  • 2
  • 10
0

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
  • 1,707
  • 10
  • 10
  • 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 Oct 06 '10 at 18:42
  • 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 Oct 06 '10 at 18:55
  • 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 Oct 06 '10 at 19:10
  • 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 Oct 06 '10 at 19:13
  • 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 Oct 06 '10 at 19:21
  • 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 Oct 06 '10 at 20:05
0

This works for me:

 void doo {
        handler.post(runnable);
 }

 private final Handler handler = new Handler();
 private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
             foo(getApplicationContext());  // do something with Context
        }
 };
Cor
  • 389
  • 1
  • 2
  • 14