java – Why does NetBeans not recognize getApplicationContext ()?

Question:

something very strange that happens to me, is that I try to place getApplicationContext () and it tells me that

"cannot find symbol"

I do the corresponding import and it does not give an error

import android.content.ContextWrapper;

So you understand, the mediaplayer works for me, I can create an android mediaplayer, but I can't make that work, why? I hope someone knows thank you

The source code where this problem occurs is the following package bfhsoftware.sonidoambiental;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.widget.Toast;

/**
 *
 * @author bfhsoftware
 */
public class ReproductorAndroid extends reproductor{
    private static MediaPlayer reproductor = null;
    OnCompletionListener escuchar = sedetienelamusica();
    public ReproductorAndroid() {
        super();
    }

    @Override
    public void empezar(String proximotema) {        
        super.empezar(proximotema);
        //
        if ((reproductor == null)){
            try {
                if (proximotema.equals("")){                    
                    System.out.println(proximotema);
                    //Environment.getExternalStorageDirectory().getPath()+
                    //Uri myUri = Uri.parse(proximotema);
                    reproductor = new MediaPlayer();                 
                    reproductor.setDataSource(getApplicationContext(),Uri.parse(proximotema)); 

                    reproductor.prepare(); // might take long! (for buffering, etc)                                        
                    reproductor.setOnCompletionListener(escuchar);
                    reproductor.start();
                    System.out.println("deberia estar reproduciendo");
                }
            } catch(Exception e ) {
                e.printStackTrace();
                System.out.println("error al reproducir");
                System.out.println(e.getMessage());        
            }
        }
        reproduciendo = (reproductor != null);
    }

    @Override
    public void parar() {
        /* Llamamos a la implementación común de parada */
        super.parar();
        /* Implementamos aquí el método en android */
    }

    @Override
    public void pausa() {
        /* Llamamos a la implementación común de pausa */
        super.pausa();
        /* Implementamos aquí el método en android */
    }

    private OnCompletionListener sedetienelamusica() {
        reiniciar();
        reproducir();
        return null;
    }
    private void reiniciar(){
        reproduciendo = false;
        if (reproductor != null)
            reproductor.stop();
        reproductor = null;
    }
    public static void displayExceptionMessage(Context context, String msg)
{
    Toast.makeText(context, msg , Toast.LENGTH_LONG).show();
}
}

in the line player.setDataSource (getApplicationContext (), Uri.parse (next item)); the compiler tells me that getApplicationContext is not recognized as a symbol, I have tried in netbeans and in intellij idea comunity version. For me the code is well entered, for me the problem is the platform, it is not Android where I run it, how to put this syntax and compile it, and that it only runs if the operating system is not Android?

Answer:

Since you are not inheriting from ActionBarActivity you cannot access the getActivityContext () method.

One option you have is to pass this class (through the constructor) the context of the activity the moment you create it. To do this, you will have to indicate that, in the constructor, you receive a Context type object.

In addition to this, you have to declare a global Context type object to your class to be able to match it in the constructor and thus be able to use it in other methods of your class.

It would be done as follows:

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.widget.Toast;

/**
 *
 * @author bfhsoftware
 */
public class ReproductorAndroid extends reproductor{
private static MediaPlayer reproductor = null;
private static Context contexto;  //Declaramos un objeto tipo Context para almacenar el que nos van a pasar por el constructor
OnCompletionListener escuchar = sedetienelamusica();
public ReproductorAndroid(Context cont) {  //Indicamos que recibimos contexto
    super();
    contexto = cont;  //Ya tenemos el contexto para utilizarlo en esta clase
}

@Override
public void empezar(String proximotema) {        
    super.empezar(proximotema);
    //
    if ((reproductor == null)){
        try {
            if (proximotema.equals("")){    
...

When creating an object you will have to do it as follows:

ReproductorAndroid repro = new ReproductorAndroid(getActivityContext());
Scroll to Top