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;
For you to understand, the mediaplayer works for me, I can create an android mediaplayer, but I can't make that work, why? hope someone knows thanks
The source code where this problem occurs is the following package bfhsoftware.soundambiental;
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(nexttopic)); the compiler tells me that getApplicationContext is not recognized as a symbol, I have tried in netbeans and intellij idea community version. For me the code is correctly entered, for me the problem is the platform, it is not Android where I execute it, how to place this syntax and compile it, and that it only be executed if the operating system is not android?
Since you're not inheriting from ActionBarActivity you can't access the getActivityContext() method.
One option you have is to pass to this class (via the constructor) the context of the activity at the time you create it. To do this, you will have to indicate that, in the constructor, you receive an object of type Context.
In addition to this, you have to declare a Context object global to your class so that you can match it in the constructor so that you can use it in other methods of your class.
It would be done as follows:
When creating an object you will have to do it as follows:
Actually getApplicationContext() is an Android SDK method that returns the context of the single global Application object of the current process.
This method could be used if Netbeans had the Android Plugin installed (as it was done in Eclipse), however these plugins are already obsolete and the official recommendation for Android development is Android Studio .
What you really need is the context,
In this case the option is to send the context to instantiate the class, for this the constructor is modified:
this way you can use the context using the variable
context
, now to instantiate the AndroidPlayer class (usinggetApplicationContext()
is more "lightweight"):