Checking the Media player - android

If i am playing song from a song list and if the other song is already playing then both songs plays simultaneously in android studio. If i just use next or previous buttons then they are working fine. The problem is through song list. I am not getting how to check if media player is playing already or not in this condion. I am doing this code in kotlin and i am new user of android studio.

you can check this way
You can take a global variable of MediaPlayer
public MediaPlayer mediaPlayer;
and then create in onCreate method:
mediaPlayer= MediaPlayer.create(this, R.raw.xxxx);
and thus your play method will be:
public void mediaPlay(View view) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
else{
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}

Related

How to stop mediaplayer while playing on background?

I am developing a music player on android and the app has two activities (MainActivity and PlayActivity). In the MainActivity I have a listview with a list of songs and in the PlayActivity I have a button to listen and pause the music. The problem is that when go back to MainActivity and I select a new song from the listview, the first one keeps playing on background while the second one also starts playing. How can I stop the first song when I select a new one?
(I don't want to stop mediaplayer onBackPressed, I just want to stop the music when another song it's selected from listview and play a fresh song in PlayActivity)
EDIT: I'm using AsyncTask to start mediaplayer on PlayActivity: mp.prepareAsync();
You could try to stop MediaPlayer when onBackPressed()
MediaPlayer mp ; // mp is your MediaPlayer
#Override
public void onBackPressed() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onBackPressed();
}
private void playMusic() {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
MediaPlayer copyMp = new MediaPlayer();
try {
copyMp.setDataSource("/path");
mp = copyMp;
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
From here
MediaPlayer is not thread-safe. Creation of and all access to player
instances should be on the same thread.
That means you have to maintain one media player instance throughout your whole application(As per your requirement). I don't know how you implemented the media player(Service/AsyncTask etc.). When you select a new song, you need to access that instance and replace the existing song with the selected one.

How to stop multiple songs playing at same time?

I have a list of songs i put manually inside my app and it currently have 3 functions: start, pause and stop (using ImageViews).
The problem is that i can play multiple songs at the same time that is not supposed to be like that. I really cant find the issue here and hope someone can help.
This is a demonstration on my problem
I want it to STOP currently playing song when another song is being clicked on.
Here is my code for my play button:
viewholder.playB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentSong == null || song != currentSong) {
currentSong = song;
mediaPlayer = MediaPlayer.create(context, song.getSong());
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
viewholder.playB.setImageResource(R.drawable.play_black);
} else {
mediaPlayer.start();
viewholder.playB.setImageResource(R.drawable.pause_black);
}
}
});
Instead of always creating a new instance of MediaPlayer every time the event onClick is called, you should create a single instance and reused it.
To play a new song don't forget to reset your player first. Here is an example:
mPlayer.reset();
mPlayer.setDataSource(context, song.getSong());

MediaPlayer creates new MediaPlayer instead of stopping

I have created a list adapter which is for a list of sounds. The user presses one and it plays the sound on a loop until they press to stop it. It shouldn't let more than one sound play at a time.
MediaPlayer mp;
public void playSound(int position){
if(position!=-1){
mp = MediaPlayer.create(mContext, ints.get(position));
if(!mp.isPlaying()) {
mp.setLooping(true);
mp.start();
}else{
mp.stop();
mp.release();
}
This does the exact opposite of what I would like. It doesn't stop playing the sound and instead plays multiple instances of sounds at the same time.

Android : stop and play sound on button click

I want to play a sound on button click in game.
I can easily do this using following code
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
Issue is, my clicking rate is faster then playing duration of my sound clip.
How do I stop it and play again on my second or third click and so on.
I want to match sound play to match my clicking rate.
Thanks
Abhinav Tyagi
Firstly, you should use MediaPlayer only for large files, like music. For playing sound effects, SoundPool is the way to go.
An excellent tutorial on playing back media is available HERE.
On the click of your button, call soundPool.stop() and soundPool.play() immediately in sequence to stop and start your audio playback.
You can call MediaPlayer.Stop to stop it playing sounds. You can also call MediaPlayer.Reset to return it to the Idle state.
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer;
public void startPlay(View view) {
mplayer.start();
}
public void stopPlay(View view) {
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this, R.raw.laugh);
}
}

How can stop mediaPlayer which is already running ,and create new mediaPlayer?

I am working in android. I am creating a mediaPlayer which is running audio files. i have 10 buttons. i have assigned different url to each button. So when i press button1 then song of url with respect to button 1 is playing. and then i click on 2nd button then song of button 2 is also playing with song 1. but i want to stop song of button 1 when i press button 2.
this is the code i am using for this functionality:-
public void onClick(View v)
{
MediaPlayer mediaPlayer=new MediaPlayer();
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
int i = Integer.parseInt((v.getTag()).toString());
String str=urls[i];
try {
mediaPlayer.setDataSource(str);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
please check my code and let me know what is mistake done by me.
you have construct a new MediaPlayer object each time the user click you view
how could it be in the running state !!!
calling release() method on a MediaPlayer object it is in thre End state
Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
but in case you want to reuse a MediaPlayer object you should call the
call the following method in the same order
reset()
make the mediaPlayer enter the Idle state
setDataSource()
set your data source note : the mediaplayer shoud be in the idle state
prepare()
start()

Categories

Resources