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.
Related
In this application I have 2 buttons one is for start the media player and another one is for stop the media player ,But in my case stat and stop is working fine .
I can't start the media player once the stop button is clicked and also I got
start called in state 0, mPlayer(0x9f5d8300)
error (-38, 0)
I tried some solutions from stackoverflow but nothing is helped .
Coding :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
can anyone help me to fix it .
If I read the doc they say you need to call prepare() method to reset your player.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
player.prepare();
}
});
}
you need to do
player.release();
after stopping media player
How to stop play sound by press button back or button home
Please Help me :
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button_play);
final MediaPlayer mP = MediaPlayer.create(MainActivity.this,R.raw.music);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mP.isPlaying()) {
mP.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
}
);
}}
Pause media player in onPause(); Method
#Override
protected void onPause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
super.onPause();
}
Hope It will work for you.
For more information Refer MediaPlayer LifeCycle
to stop music player
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
to stop play sound by press button back or button home
in Activity you need to stop music on onDestroy and onBackPressed
#Override
protected void onDestroy() {
super.onDestroy();
stopPlaying();
}
#Override
public void onBackPressed() {
super.onBackPressed();
stopPlaying();
}
I'm trying to play music in all activities in my app but when I switch between mainactivity and another activity music stopped please someone help me without using Service
MainAcivity :
ToggleButton MusicButton;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//code
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
if(MusicButton.isChecked()){
mediaPlayer.pause();
}
else
mediaPlayer.start();
}
}
I don't know what I can do in the other activity.
You can pass the current seek position using extras, but the Music may stop while switching between activities.
I suggest that you use service then in each activity onPause event you check if you are switching to another activity in your application or not, if not send some broadcast to the service to stop playing music.
I am creating a game, I want to play a background music for one activity only(For main menu of game), my code shown below, The problem is that the music plays more than one time, I want to play the same music also when activity Resumes.
public class Menu extends Activity {
MediaPlayer mp
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
public void play(View ButtonClicked) {
mp.stop();
mp.release();
//mp = MediaPlayer.create(Menu.this, R.raw.l);
//mp.start();
goToActivity(Game.class);
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
//coins
coin.setText(data.getString("coin"));
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
//mps.release();
}
In your onResume don't initialise MediaPlayer again and again. It creates new instance every time when you come to onResume. So add a check in onResume like this :
#Override
protected void onResume() {
super.onResume();
if (mp==null)
mp=MediaPlayer.create(MainActivity.this,R.raw.adalante);
if (!mp.isPlaying())
mp.start();
}
and additionally add this for prevention to play when activity goes to onPause
#Override
protected void onPause() {
super.onPause();
mp.pause();
}
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
}
}