Android: creación de reproductor multimedia
Frecuentes
Visto 38,089 veces
3
Tengo este codigo:
package com.example.pr;
import android.media.MediaPlayer;
public class Audio{
MediaPlayer mp;
public void playClick(){
mp = MediaPlayer.create(Audio.this, R.raw.click);
mp.start();
}
}
I have an error in "create" with this message "The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Audio, int)"
¿por qué?
2 Respuestas
12
MediaPlayer.create() needs a Contexto as first parameter. Pass in the current Actividad y debería funcionar.
tratar:
public void playClick(Context context){
mp = MediaPlayer.create(context, R.raw.click);
mp.start();
}
en tu Actividad:
audio = new Audio();
...
audio.playClick(this);
but don't forget to call release on the MediaPlayer instance once the sound has finished, or you'll get an exception.
However, for playing short clicks using a grupo de sonido podría ser mejor de todos modos.
Respondido 28 ago 12, 09:08
Audio is not an activity, it's a separate class that I use to manage audio, in fact I pass Audio.this - CiclismoEsMejor
Late answer: you need an Android Context to pass. not your Audio class. So either add a Context parameter to the playClick method or have each Activity have their own instance of the Audio class with the Activity Context passed in the constructor. - P. Melch
can anyone tell me how can i add 2 raw files in mediaplayer.create - Vasant Raval
If you want to play two sounds in parallel you have to create two MediaPlayers - P. Melch
1
public class Audio{
MediaPlayer mp;
Context context;
public Audio(Context ct){
this.context = ct;
}
public void playClick(){
mp = MediaPlayer.create(context, R.raw.click);
mp.prepare();
mp.start();
}
From your Activity:
Audio audio = new Audio(YourActivity.getApplicationContext());
audio.playClick();
Respondido 28 ago 12, 08:08
create already does the prepare. MediaPlayer.create(context, R.raw.click).start();
- jt-gilkeson
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android android-mediaplayer or haz tu propia pregunta.
audio is just a class not an activity so that audio.this wont contribute to context - Athul Harikumar