Reputation: 3
I'm making my first app in Android Studio. I got this activity, where it just plays a sound with MediaPlayer, and then it is supposed to return to the main activity, either by a button (works fine) or when the sound stops(what i'm missing). Is there an easy way to check if the sound has stopped playing, and then let that trigger an action?
Here is my code so far:
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
public class image_button_2 extends ActionBarActivity implements View.OnClickListener {
ImageButton imageButton6;
MediaPlayer mySound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_button_2);
imageButton6 = (ImageButton) findViewById(R.id.imageButton6);
imageButton6.setOnClickListener(this);
mySound = MediaPlayer.create(this, R.raw.hb);
mySound.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_image_button_2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void imageButton6Click(){
mySound.stop();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.imageButton6:
imageButton6Click();
break;
}
}
}
Upvotes: 0
Views: 225
Reputation: 10569
Try this out , This will take you into MainActivity when music is finished
mySound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
imageButton6Click();
}
});
Upvotes: 1