I use a toggle button for playing an audio file. When i checked the toggle button, app played music. When the music finish, i want it automatically uncheck the toggle button. How can i do ?
This is my playing music method :
MediaPlayer mp = null;
protected void playmusic(String theText) {
if (mp != null) {
mp.reset();
mp.release();
}
if (theText.length() > 2) {
mp = MediaPlayer.create(this, Uri.parse(theText));
} else { mp = MediaPlayer.create(this, R.drawable.afart11);
}
mp.start();
}
And this is my toggle button :
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(this);
...
...
case R.id.toggleButton1:
if (tg.isChecked()) {
playmusic(Setting.set);
else {
mp.stop();
}
set an onCompletionListener to the MediaPlayer
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp){
tg.setChecked(false);
}
});
Implement OnCompletionListener and call mp.setOnCompletionListener(this). Callback method onCompletion(MediaPlayer mp) gets called when song is finished.
public class YourActivity extends Activity implements OnCompletionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//media player code
mp.setOnCompletionListener(this);
}
#Override
public void onCompletion(MediaPlayer mp) {
//your code when song completes
}
}
Related
I'm trying to play music when the user clicks in the MUSIC ON toggle button, and music will pause when he clicks in MUSIC OFF. I also need to play music when opening the app. This is my code but doesn't work:
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vapor);
mediaPlayer.start();
.....
}
MusicButton = (ToggleButton)findViewById(R.id.toggleButton);
MusicButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MusicButton.isChecked()){
mediaPlayer.start();
}
else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
#Override
public void onPause() {
mediaPlayer.stop();
mediaPlayer.release();
super.onPause();
}
#Override
public void onResume() {
mediaPlayer = MediaPlayer.create(this, R.raw.vapor);
mediaPlayer.setLooping(false);
mediaPlayer.start();
super.onResume();
}
Error log:
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1384)
at com.myapp$MainActivity$2.onClick(MainActivity.java:80)
first,you should not use setOnClickListener() on ToggleButton,ToggleButton has function setOnCheckedChangeWidgetListener to listene the status of compent
second,about you error,maybe your ToggleButton is unchecked,when you clicked it, ToggleButton become checked and invoke you code mediaPlayer.start();,but you start mediaPlayer at onCreate() already,you can check it.
I am trying to develop a simple test project that plays sound when I tap the button and stop automatically after a few minutes after playing the sound.
Here is a code snippet:
Code for playing:
if (mPlayer != null) mPlayer = null;
mPlayer = MediaPlayer.create(this, R.raw.shush_v2);
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Code for stopping:
if(mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
But sometimes I can still hear two sounds playing after I've stopped the sound.
Have so ever seen this behaviour before?
You should clear your media player.
//example usage of clearing media player when its over.
ourSong.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
clearMediaPlayer(mp);
}
});
//You can use this to stop media player.
private void clearMediaPlayer(MediaPlayer mp){
if(mp!=null){
mp.stop();
mp.release();// this will clear memory
mp = null;
}
}
public class PlayerApp extends Activity {
Button btnStart;
MediaPlayer mediaPlayer = null;
// Use the handler to stop the Player, after specific time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_app);
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
// Initialize Player and start it.
// Call the Handler same time.
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.test);
mediaPlayer.start();
startHandler();
}
});
}
private void startHandler()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// if Player is not null, then Stop it and Reset Null.
if(mediaPlayer!=null)
{
mediaPlayer.stop();
mediaPlayer = null;
}
}
}, 2500);
}
}
For example, I want to play 3 songs. When the 1st song ends, the 2nd song begins, and when the 2nd song ends, the 3rd song begins, and when the 3rd song ends, the 1st song begins again and so on. Is the mp.setOnCompletionListener used here?
You are right. You can do something simple like this:
public class MainActivity extends Activity {
MediaPlayer mp1, mp2, mp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp1 = MediaPlayer.create(this, R.raw.music_1);
mp2 = MediaPlayer.create(this, R.raw.music_2);
mp3 = MediaPlayer.create(this, R.raw.music_3);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});
}
}
This will play mp1 and onCompletion of it, it will play mp2, and onCompletion of mp2, it will play mp3, and onCompletion of mp3, it will play mp1 again and so on...
First declare the 3 MediaPlayer files:
MediaPlayer song1;
MediaPlayer song2;
MediaPlayer song3;
Then initialize the MediaPlayer objects:
song1 = MediaPlayer.create(this, R.raw.exampleSong1);
song2 = MediaPlayer.create(this, R.raw.exampleSong2);
song3 = MediaPlayer.create(this, R.raw.exampleSong3);
Now start the first Media file with
song1.start();
Now with every instance created you should add to every MediaPlayer object a setOnCompletionListener like this:
song1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song2.start();
}
});
Do the same for the Second and Third MediaPlayer files:
song2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song3.start();
}
});
song3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song1.start();
}
});
First I am using soundpool But with soundpool its difficult to do this task. So I am now using media player. In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish so that user able to play sound again when its finish.
Code-
private LinearLayout layout1;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
});
}
private LinearLayout layout1;
MediaPlayer mp;
boolean flag=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(!flag){
mp.start();
layout1.setEnabled(false);
} else{flag =true ;}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
flag=false;
layout1.setEnabled(true);
}
}); }
EDIT
You can do like this in onClickListener:
boolean flag;
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if (flag == false){
flage=true;
layout1.setEnabled(false);
mp.start();
}
});
The value of flag is dependant on state of sound file.
The callback public void onCompletion(MediaPlayer mp) gives you a reference to the MediaPlayer.
public void onCompletion(MediaPlayer mp) {
flag = false;
layout1.setEnabled(true);
}
guys scenario is that suppose i click a button, the sound plays and within the duration of that track i again click the button and want to play it from the beginning. i tried with the following code, but no success.
code is :
public class SoundtestActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.gun_shot);
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
mp.reset();
mp.start();
}
}));
}
}
Try this code
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp.start();
}
});
Or replace mp.start(); by mp.reset(); in this code
Best way and the easiest way is =
mp.setLooping(true);
And yes it does work, just simply add this line after mp.start() and nothinfg else is required
You just need to comment the following lines and shift your Media player instance in the button's listener method, bingo your done with multiple media players playing parallel to each other
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(this, R.raw.gun_shot); // ADD THIS LINE HERE
if(mp.isPlaying()){
mp.stop(); //ADDED TO STOP FIRST
mp.release(); //ADDED TO RELEASE RESOURCES
}
mp.start();
}
}));