Is there any Android mp3 player other than MediaPlayer? - android

I have searched for another mp3 player to use in my android project but i couldn't find. MediaPlayer is not good !
I'm trying to play a song , so when i click the song it plays , but if i click it again it should stop , MediaPlayer is not working good , i tried to make a
ProgressDialog
it also did not work .
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = model.getTitle();
Toast.makeText(MainActivity.this, "you clicked on -> " + name , Toast.LENGTH_SHORT).show();
String url = model.getMusic();
MediaPlayer mp = MediaPlayer.create(getApplicationContext() , Uri.parse(url));
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss();
}
mp.start();
}
});
if(mp.isPlaying()){
mp.stop();
}else {
mp.start();
}
}
});

You are calling the static create() method. The media will be prepared before that method returns. Your OnPreparedListener listener will never be called in your existing code.
The simple solution is to get rid of the OnPreparedListener and call dismiss() after create(). Most likely, you will find that your ProgressDialog does not do much good.
Alternatively:
Replace create() with just calling new MediaPlayer()
Add your OnPreparedListener
Call setDataSource() on the MediaPlayer in a background thread, as this will perform some disk I/O, and you do not want to do that on the main application thread
You may need to then arrange to call dismiss() on the main application thread — I have not tried to dismiss() a dialog from a background thread, and I do not know if that is supported.

Related

How to play/stop/play again using MediaPlayer [duplicate]

This question already has answers here:
Media Player start stop start
(8 answers)
Closed 6 years ago.
I have a button when clicked it plays an audio, and while it is playing I can pause it and replay it again and so forth. I have a shake event, where I want to play the audio on shake and replay it when device is shaken again (by first stopping the audio, calling stopAudio?)
I have the following code:
llBtn = (LinearLayout) findViewById(R.id.button);
llBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this, "LL WAS CLICKED", Toast.LENGTH_SHORT).show();
//IF AUDIO IS NOT PLAYING... PLAY AUDIO
if (tvPS.getText() == "Stop Phrase!") {
StopAudio();
}
else {
PlayAudio();
}
}
});
//the same button is clicked to stop, it is currently setting to Pause.
public void StopAudio() {
mediaPlayer.pause();
Toast.makeText(MainActivity.this, "STOPPED", Toast.LENGTH_SHORT).show();
tvPS.setText("Play Phrase!");
}
public void PlayAudio() {
//stopPlaying(mediaPlayer);
tvPS.setText("Stop Phrase!");
inResId = getResources().getIdentifier("play" , "raw", getPackageName());
mediaPlayer = MediaPlayer.create(getBaseContext(), inResId);
mediaPlayer.seekTo(0);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
tvPS.setText("Play Phrase!");
mediaPlayer.pause();
}
});
}
...
public void onShake() {
// Do stuff!
Toast.makeText(getBaseContext(), "Motion detected",
Toast.LENGTH_SHORT).show();
if (mediaPlayer == null) {
PlayAudio();
}
}
How can I modify the code above to handle Play, and then stop if stopped in the middle and then able to replay it again.
The button click works but I am creating a new instance each time and not recycling. When the device is shaken, it plays the audio twice instead of just once.
First of all create the MediaPlayer in onCreate of the context and not in some listener, and don't forget to release it when done, since it consumes a lot of resources,
mediaPlayer.release();
mediaPlayer = null;
Next thing, stop the audio using the stop() function instead of the pause() on completion of the song.
Steps
If you are using a service/activity to play a video create a MediaPlayer instance before it's usage begins, likely in onCreate.
Later use that instance to play/pause/stop the media.
In the onStop of the Service/Activity release the MediaPlayer instance.
Update your playMusic() function to use the MediaPlayer instance you just created, also in the listener use stop() instead of pause()
To change track of an existing MediaPlayer instance
You can use this:
String path = getExternalFilesDir(null).toString()+"/";
mMediaPlayer.setDataSource(path + mediafile);
Link: Changing data source for audio playback using existing MediaPlayer?

Getting a ringtone to only loop once in mediaPlayer

So basically I use a Spinner widget and pass it the RingtoneManager Picker action, the user then selects their ringtone. Then I call onActivityResult() and get the uri for the ringtone.
Finally I pass the uri to another activity where I have a alarm setup to go off after a specific amount of time.
THE PROBLEM >>> when I get the uri for the ringtone in the 2nd activity and let mediaPlayer play it, It...it doesn't stop. No matter WHAT I try.
This is the 2nd activity and the mediaPlayer that never stops.
Uri ringtone;
ringtone = Uri.parse(musixType);
//mMediaPlayer.setDataSource(DisplayNotification.this, ringtone);
mMediaPlayer = MediaPlayer.create(DisplayNotification.this, ringtone);
mMediaPlayer.start();
mMediaPlayer.setVolume(100, 100);
}
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
while (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
});
How do I get it to stop #.#
Edit:
Could the reason it doesn't stop playing be that it is a ringtone from the RingtoneManager? I don't know why this would matter but I'm grasping at straws at this point.
Edit:
Is there a way to specify a certain length of time for mediaPlayer to run and disregard the data passed to it?
did you use MediaPlayer.setLooping method
use mMediaPlayer.setLooping(false);
Since you're not calling setLooping(true); on your mMediaPlayer referenced object, looping should not be the issue here as default is set false. Make sure you're not actually calling this piece of code multiple times from outside. Put a Log line before mMediaPlayer = MediaPlayer.create(DisplayNotification.this, ringtone); and see how many times it gets logged.
use this
mMediaPlayer.setLooping(true);
I had this problem too and solved it like this:
player.setDataSource(this, ringtone);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
mp.stop();
mp.release();
}
}, mp.getDuration()); <-- Make a postDelayed runnable that has the duration of the file as its cut off. Once the song plays, it will stop the mediaPlayer.
mp.start();
}
});

MediaPlayer.start() dies from too much clicking

In Adnroid, at first i declare the mediaplayer by
MediaPlayer mpl;
next I have this in the onCreate method
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.hit );
mp.setVolume(1, 1);
and a function that's supposed to play a sound when called
public void click()
{
mp.start();
}
yet the problem is that if the user calls this function multiple times, before it has stopped playing the last sound, it will die and stop playing any sounds, before the app is reset.
Any ideas how to fix that?
Thanks!
edit - found a solution:
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpl.release();
}
});
public void click()
{
if( ! mp.isPlaying() ) {
mp.start();
}//if
}//met
you can disable the button.
or you can stop current playing and star
new in onClick()

android MediaPlayer Looping

I am trying to play a looping Ogg file, I tried enabling setLooping(true) but that had no effect so I tried onCompletionListener and that's not working either, could someone clarify what I am doing wrong?
musicPlayer = MediaPlayer.create(mContext, R.raw.overworld);
musicPlayer.setVolume(musicVolume, musicVolume);
// musicPlayer.setLooping(true);
musicPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
musicPlayer.stop();
musicPlayer.seekTo(0);
musicPlayer.start();
Log.d("Sound Manager", "Song Completed");
}
});
Following is my play function,
public void playSong(int id) {
try {
stopSong();
musicPlayer = MediaPlayer.create(mContext, id);
musicPlayer.start();
} catch(Exception e) {
// Ignored
}
}
It's known that MediaPlayer is having problems with ogg files.
You could preferrably switch to another file format.
The other thing is, I would go on trying with setLooping(boolean) as it's most likely using the same scheme and its much more clearly.
Calling seekTo() if the MediaPlayer Object is stopped causes the MediaPlayer to be in an invalid state. You can call pause() instead but I wouldn't call any of these method, why not just seeking? I would guess if you remove the musicPlayer.stop() it will work.
remove ....
musicPlayer.stop();
from
onCompletion(MediaPlayer mp)
onCompletionListener() is not called if your MediaPlayer is set to looping, BUT if you don't have it set to looping, you can always just use a completion listener like so
#Override
public void onCompletion(MediaPlayer mp) {
if(!mp.isPlaying()) {
mp.start();
}
mp.seekTo(0);
}
You also shouldn't call stop() because that stops playback completely, and it doesn't make sense to seek in a video/song that you are not playing.

Mediaplayer stop is not working

I have got a MediaPlayer object as class attribute.
MediaPlayer mp;
Then I use them in my onCreate method -
mp = MediaPlayer.create(getApplicationContext(), R.raw.num1);
mp.start();
Then I try to stop it from one of my buttons clicklistener like this -
btnBack = (Button) findViewById(R.id.btn1);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
stopSound();
startActivity(new Intent(Five.this, Four.class));
finish();
}
});
}
public void stopSound(){
if (mp != null) {
mp.release();
mp.stop();
}
}
but the stop does not work.
What am i doing wrong?
You've already released the MediaPlayer instance. It's gone. You can't call stop() on it after you release it. If you plan to resume the sound, just remove the release() call. If you want to destroy the MediaPlayer (e.g. create a new one later with MediaPlayer.create()), remove the stop() call. You should read over this documentation pretty thoroughly; the MediaPlayer class is pretty complex. You need to understand the different states and when you can and cannot call certain methods.
In some android versions, just some ones, i had a similar issue in one app streamming radio, it worked it with:
try{
if(mp!=null && mp.isPlaying()){
mp.stop();
}
mp.release();
mp=null;
mp = new MediaPlayer(); //I needed this, maybe you dont hac
}catch(Exception e){
e.printStackTrace();
System.out.println("I can't believe you get here !");
}
you can't call release before stop
have a look at the lifecycle here http://developer.android.com/reference/android/media/MediaPlayer.html

Categories

Resources