when the sound playback is repeated for a long time. when the example is clicked 10 times He does not play. what is the reason why it plays when you restart the application
mediaPlayer = MediaPlayer.create(mContext, R.raw.click);
mediaPlayer.setVolume(7f, 7f);
mediaPlayer.start();
mediaPlayer.reset() will help you!
Ex:
public static void startSound(Context context, int soundResId) {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundResId);
mMediaPlayer.start();
}
Related
I tried the below code to play the mp3 file but it plays all the ringtones.
Can anyone help me why the ringtone is being played?
MediaPlayer mMediaPlayer1 = new MediaPlayer();
AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.raw.dialtone);
FileInputStream in = new FileInputStream(fd.getFileDescriptor());
mMediaPlayer1.setDataSource(in.getFD());
mMediaPlayer1.setLooping(true);
mMediaPlayer1.prepare();
mMediaPlayer1.start();`
Try this:
public static void startSound(Context context, int soundResId) {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundResId);
mMediaPlayer.start();
}
then
startSound(this, R.raw.dialtone);
I set a music file to loop on MediaPlayer for my game, but it causes a 2 sec delay when it loops.
My code:
boolean activateSounds = getIntent().getBooleanExtra("Activate sounds", true);
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(StartGame.this, R.raw.music1);
mp.setVolume(8f, 8f);
mp.setLooping(true); // This is causing delays
if (activateSounds) mp.start();
For a game, this is not interesting. Is there a way to make MediaPlayer run out of loop delays?
I was not able to make setLooping work without a gap.
Only solution that worked for me was to use setNextMediaPlayer call (which is able to start next loop without a gap) and two MediaPlayers.
'pseudo' code:
class Abc implements MediaPlayer.OnCompletionListener {
private final MediaPlayer[] mps = new MediaPlayer[2];
public Abc() {
mps[0] = new MediaPlayer();
mps[1] = new MediaPlayer();
mps[0].setOnCompletionListener(this);
mps[1].setOnCompletionListener(this);
}
public void start()
initMediaPlayer(mps[0]);
initMediaPlayer(mps[1]);
mps[0].setNextMediaPlayer(mps[1]);
mps[0].start();
}
private void initMediaPlayer(MediaPlayer mp)
{
if (mp.isPlaying()){
mp.stop();
}
mp.reset();
final float volume = 0.07f;
mp.setDataSource(MY_SOURCE);
mp.setVolume(volume, volume);
mp.setLooping(false);
try {
mp.prepare();
}catch(Exception error){
Log.d("BackgroundMusic", error.toString());
}
}
#Override
public void onCompletion(MediaPlayer mp)
{
MediaPlayer cur = mps[0] == mp ? mps[1] : mps[0];
initMediaPlayer(mp);
cur.setNextMediaPlayer(mp);
}
}
I'd just like to stop my MediaPlayer file and I really don't understand why is that so complicated. I have this method to start to music:
public void startMusic() {
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
mediaPlayer1.start();
mediaPlayer1.setVolume(0.2f, 0.2f);
mediaPlayer1.setLooping(true);
play = true;
}
And I'd like to stop music with this method:
public void stopMusic(){
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
if(play){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
When I call the startMusic method the music starts but when I call the stopMusic method then nothing happens.If anyone knows how to do that please response.
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
On this line you are creating a new MediaPlayer object and you lose the old object reference (it will continue playing until gc). Use same object, as listed below
public void stopMusic(){
if (mediaPlayer1 != null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
}
}
You are again creating MediaPlayer instance and stopping the new instance. But what you need to do is to stop the existing MediaPlayer instance which is running and playing audio. Change your stopMusic() -
public void stopMusic(){
if(play && mediaPlayer1 != null){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
I have a very simple mediaplayer that play background. It calls file from the apk, but I want it to play from any directory like as music or sdcard.
Here is my code:
private MediaPlayer mpintro;
.
.
mpintro = MediaPlayer.create(this, R.raw.intro);
mpintro.setLooping(true);
mpintro.start();
It works like this:
mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();
It did not work properly as string filepath...
String filePath = Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
and this play from raw folder.
int resID = myContext.getResources().getIdentifier(playSoundName,"raw",myContext.getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(myContext,resID);
mediaPlayer.prepare();
mediaPlayer.start();
mycontext=application.this. use.
Here is the code to set up a MediaPlayer to play off of the SD card:
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
mediaPlayer.start()
You can see the full example here. Let me know if you have any problems.
Use the code below it worked for me.
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.mp3");
mp.prepare();
mp.start();
I use this class for Audio play. If your audio location is raw
folder.
Call method for play:
new AudioPlayer().play(mContext, getResources().getIdentifier(alphabetItemList.get(mPosition)
.getDetail().get(0).getAudio(),"raw", getPackageName()));
AudioPlayer.java class:
public class AudioPlayer {
private MediaPlayer mMediaPlayer;
public void stop() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
// mothod for raw folder (R.raw.fileName)
public void play(Context context, int rid){
stop();
mMediaPlayer = MediaPlayer.create(context, rid);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}
// mothod for other folder
public void play(Context context, String name) {
stop();
//mMediaPlayer = MediaPlayer.create(c, rid);
mMediaPlayer = MediaPlayer.create(context, Uri.parse("android.resource://"+ context.getPackageName()+"/your_file/"+name+".mp3"));
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}
}
2020 - NOV
This worked for me:
final File file = new File(getFilesDir(), "test.wav");//OR path to existing file
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(file));
mediaPlayer.start();
In Kotlin:
1) The file is in a resource folder :
var nowLesson =resources.getIdentifier("test.mp3", "raw", packageName)
mediaPlayer = MediaPlayer.create(applicationContext, nowLesson)
mediaPlayer.start()
2) The file is in a Path (file path)
val file = File(
this.getDir("Music", MODE_PRIVATE),
"/t.mp3"
)
mediaPlayer = MediaPlayer.create(this,Uri.fromFile(file ))
mediaPlayer.start()
E/MediaPlayer(20473): error (-19, 0)
I/MyApp (20473): Decoding lala.mp3
I/StagefrightPlayer( 68): setDataSource('mypath')
E/AudioFlinger( 68): no more track names available
E/AudioTrack( 68): AudioFlinger could not create track, status: -12
E/AudioSink( 68): Unable to create audio track
Does any1 know why i'm getting this? This usually happens after playing about 100+ of so audio files using mediaPLayer. I'm playing it like this
public RenderResultFormat DoIt() {
if(mp!=null){
mp.release();
mp = null;
}
AudioRenderer mr = new AudioRenderer(); mp = mr.AudioRenderer(filePath);}
private class AudioRenderer extends Activity {
private MediaPlayer AudioRenderer(String filePath) {
//delcare mediaplayer variables, path etc
mp= MediaPlayer.create(this, path);
if(mp != null){
int duration = mp.getDuration();
mp.start();
try {
Thread.sleep(duration);
} catch (InterruptedException e2) {
e2.printStackTrace();
System.out.println("I've been interrupted >:(");
}
}
}return mp;}
Am i missing something? Quite new to android development. Thank you
This solution works well for me (playing a ressource). Just implement an OnCompletionListener
private void playbeep(int id) {
MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(context, id);
mPlayer.start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}