My android application has two buttons. Each of them will play a sound of size 1.5 seconds if clicked. I have used MediaPlayer to achieve this. The problem is when i click one button and press another within less the 1.5 sec, the second sound wont play . My requirement is when i press the second button I want the first sound to be stopped and the second sound should start playing. How do i achieve this.
When you press the 2nd button and the music is still playing, you need to stop that music and then start playing the 2nd button's song.
so add this to your button
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(this, YourMusicHere);
mPlayer.start();
Related
Unable to switch between songs
Totally new to this thing.
I have two buttons in my android app.
I'd like to play 2 songs using MediaPlayer.
When I play one button, everything is fine, but when I start the 2nd
button, the first song is still playing. How do I stop that?
You need to end the 1st song and then start the 2nd song. So for each button, do something like this. You need to check first to see if the music is playing, and if it is, end it. Then start your new song
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, Song1); //then song2
mPlayer.start();
I'm creating a simple app that plays and stops music on button click. For the first time, when I click start music button, the music plays and stops when clicking it again. The problem is that when I click it again, it starts playing and doesn't stop on the next click anymore. Instead, it plays same music again in parallel (e.g. however many times I click, that many tracks are played in parallel).
The code for the button XML:
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_toRightOf="#+id/imageView1"
android:background="#7FFF00"
android:text="Start Music"
android:onClick="music" />
and MainActivity.java
public void music(View v)
{
Button bu= (Button)findViewById(R.id.button3);
String ans=bu.getText().toString();
MediaPlayer mp = MediaPlayer.create(this, R.raw.my);
switch(ans)
{
case "Stop Music":
mp.setLooping(false);
mp.stop();
bu.setText("Start Music");
break;
case "Start Music":
mp.setLooping(true);
mp.start();
bu.setText("Stop Music");
break;
default:
break;
}
}
You are creating a new MediaPlayer instance on every button click. Just create it once in your activity's onCreate() and refer to that single instance in your music() method.
Remember to call mp.release() as well, when you exit your activity.
The above Image shows various states of MediaPlayer. The Key thing you need to notice here is that After the Starting the music (Start State) when you stop the music it goes to Prepared State. Now if You want to play your song again, You need to prepare() it first.
Hence before playing the music:
mp.reset();
mp.prepare();
mp.start();
Hence When you hit the play button after the stop button! It will prepare() then play(). reset() is simply resetting the song to start from beginning.
I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?
Please someone help me. Thanks...
Use MediaPlayer from Android. See: Android Example
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
I have a requirement where upon clicking the launcher icon of my app, it should read out the app name.
I know it's a kind of funny, but is that possible? any directions?
try this in the onCreate/onResume method of your activity: MediaPlayer
Just start the player
mp = new MediaPlayer();
mp.reset();
mp.setDataSource(fileNamePath);
mp.prepare();
mp.start();
I have a buttons called play and stop
if I press play its starting playing. after I press stop its going to home page. But I need to go home page automatically once playing is done ie. Without clicking stop button. How can I do it for android?
thanks in advance
hi shekhar you can set an oncomplete listener to the player object. The code follows:
mPlayer.setOnCompletionListener(new OnCompletionListener(){#Override public void onCompletion(MediaPlayer mp) { "your code comes here" }});
here the mPlayer is the object of the MediaPlayer which is currently running..