When a sound that the user previously selected is playing, how can I stop that sound and immediately play the new selected sound? The code I have so far only stops the new sound, but the user has to select the sound again in order to play it.
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.secondsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
I am making a soundboard with over 30 sounds, if that helps at all. What (slight I assume) changes would I need to make to my code? Any help would be greatly appreciated!
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
public Boolean playing=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
}
}
Related
I'm struggling on designing a little audio player.
Just a list of 4 songs - each has it's own textView and when a user clicks on a textView a song starts playing but i have no idea how to make this. I though about making a onClick method for each textView but really dont like this idea.
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
/* MediaPlayer hell = MediaPlayer.create(this, R.raw.Hell);
MediaPlayer bells = MediaPlayer.create(this, R.raw.HellsBells);
MediaPlayer shoot = MediaPlayer.create(this, R.raw.Shoot);
MediaPlayer thunder = MediaPlayer.create(this, R.raw.Thunderstruck);*/
public void playMusic (View view) {
varMediaPlayer = MediaPlayer.create(this, R.raw.Hell);
varMediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the following variables:
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
will make your app crash as your assigning the variables when the MainActivity object is created.
move those to onCreate(Bundle) and assign a click listener to each of those TextViews
private static final int RAW_MUSIC_1 = R.raw.Hell;
private static final int RAW_MUSIC_2 = R.raw.HellsBells;
private static final int RAW_MUSIC_3 = R.raw.Shoot;
private static final int RAW_MUSIC_4 = R.raw.Thunderstruck;
private MediaPlayer varMediaPlayer;
private TextView song1, song2, song3, song4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign after setContentView(...) as findViewById scan the content view layout
song1 = (TextView) findViewById(R.id.song1);
song2 = (TextView) findViewById(R.id.song2);
song3 = (TextView) findViewById(R.id.song3);
song4 = (TextView) findViewById(R.id.song4);
// add click listeners
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_1);
}
};
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_2);
}
};
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_3);
}
};
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_4);
}
};
}
and change playMusic(View) method to playMusic(int) and add release and reset methods to release object from memory
private void playMusic(final int musicId) {
try {
if (mediaPlayer.isPlaying()) mediaPlayer.reset();
} catch (final Exception e) {}
try {
mediaPlayer.release();
} catch (final Exception e) {}
mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
}
Replace your code with this one and try running this...
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
MediaPlayer mediaPlayer = new MediaPlayer();
public void playMusic (Integer id) {
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(this , id);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.hell);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.HellsBells);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Shoot);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Thunderstruck);
}
});
}
}
I have an activity when i click on play button and it plays mp3 sound. When I click back the audio still plays in the background and can also run alongside other audio sounds which is annoying. I have tried to implement other methods used on here but to no avail.
Code:
public class InstructionsManualActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instructions_manual);
Button one = (Button) this.findViewById(R.id.instructionsManual_but);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.instructions_manual);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
}
I have tried this code but app crashes on back press
#Override
public void onBackPressed() {
super.onBackPressed();
if (mp.isPlaying()) {
mp.stop(); // or mp.pause();
mp.release();
First of all declare the MediaPlayer object Globally
MediaPlayer mp = null; //Declaring globally so can access anywhere in activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instructions_manual);
Button one = (Button) this.findViewById(R.id.instructionsManual_but);
mp = MediaPlayer.create(this, R.raw.instructions_manual);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
When you are pressing back button in any activity onBackPressed()
will be called , there you can stop you media player if playing.
#Override
public void onBackPressed() {
super.onBackPressed();
//Here you can stop your MediaPlayer
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
}
}
Try this it's working for me
if (mp != null) {
mp.stop();
mp.release();
mp = null;
finish();
}
Your code is fine just check it before stop for null player
super.onBackPressed();
if (mp!=null && mp.isPlaying()) {
mp.stop(); // or mp.pause();
mp.release();
After I set oncompletetionlistener for MediaPlayer.
Using the same button again after the mediaplayer is release, it crashes the app. How do I call a event to play audio after the media player is release?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1 = findViewById(R.id.b1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.release();
}
});
}
});
}}
After you release the media player you have to reintialiaze the MediaPlayer object.
if (mp1 == null)
mp1 = new Media player();
I have problem with my android app. It's soundbar application.
After run, everything works perfectly. After some time and clicking on buttons, I have error:
E/MediaPlayer: start called in state 0, mPlayer(0x7d176f4300)
E/MediaPlayer: error (-38, 0)
I run this on my Samsung S7, I would ask for understanding becouse I am beginner. I saw similar topics but I can't find answer which helps me. Here is my code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer AaaCoJeszcze = MediaPlayer.create(this, R.raw.aaa_co_jeszcze);
Button But1 = this.findViewById(R.id.Button1);
But1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AaaCoJeszcze.start();
}
});
final MediaPlayer CoNieMoznaMP = MediaPlayer.create(this, R.raw.co_nie_mozna);
Button But2 = this.findViewById(R.id.Button2);
But2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoNieMoznaMP.start();
}
});
final MediaPlayer CoToJaWinnyMP = MediaPlayer.create(this, R.raw.co_to_ja_winny);
Button But3 = this.findViewById(R.id.Button3);
But3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoToJaWinnyMP.start();
}
});
final MediaPlayer debilMP = MediaPlayer.create(this, R.raw.debil);
Button But4 = this.findViewById(R.id.Button4);
But4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
debilMP.start();
}
});
final MediaPlayer iTenMP = MediaPlayer.create(this, R.raw.i_ten_o_co_chodzi);
Button But5 = this.findViewById(R.id.Button5);
But5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iTenMP.start();
}
});
final MediaPlayer JedziemyMP = MediaPlayer.create(this, R.raw.jedziemy_z_kurwami);
Button But6 = this.findViewById(R.id.Button6);
But6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JedziemyMP.start();
}
});
final MediaPlayer JegoMP = MediaPlayer.create(this, R.raw.jego_robota_moze_poczekac);
Button But7 = this.findViewById(R.id.Button7);
But7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JegoMP.start();
}
});
final MediaPlayer ZanimMP = MediaPlayer.create(this, R.raw.zanim_podeszlem);
Button But8 = this.findViewById(R.id.Button8);
But8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ZanimMP.start();
}
});
final MediaPlayer MozePanOdejscMP = MediaPlayer.create(this, R.raw.moze_pan_odejsc);
Button But9 = this.findViewById(R.id.Button9);
But9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozePanOdejscMP.start();
}
});
final MediaPlayer MozeZrobilPomylkeMP = MediaPlayer.create(this, R.raw.moze_zrobil_pomylke);
Button But10 = this.findViewById(R.id.Button10);
But10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeZrobilPomylkeMP.start();
}
});
final MediaPlayer MowiszTakTakMP = MediaPlayer.create(this, R.raw.mowisz_tak_tak);
Button But11 = this.findViewById(R.id.Button11);
But11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MowiszTakTakMP.start();
}
});
final MediaPlayer PaniMnieMP = MediaPlayer.create(this, R.raw.pani_mnie_rozprasza);
Button But12 = this.findViewById(R.id.Button12);
But12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PaniMnieMP.start();
}
});
final MediaPlayer PrawdopodobnieMP = MediaPlayer.create(this, R.raw.prawdopodobnie_polak);
Button But13 = this.findViewById(R.id.Button13);
But13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrawdopodobnieMP.start();
}
});
final MediaPlayer ProszeNiePrzeszkadzacMP = MediaPlayer.create(this, R.raw.prosze_nie_przeszkadzac_dobrze);
Button But14 = this.findViewById(R.id.Button14);
But14.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProszeNiePrzeszkadzacMP.start();
}
});
final MediaPlayer PrzepraszamPaniaMP = MediaPlayer.create(this, R.raw.przepraszam_pania_spoczko);
Button But15 = this.findViewById(R.id.Button15);
But15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrzepraszamPaniaMP.start();
}
});
final MediaPlayer RozpraszaMP = MediaPlayer.create(this, R.raw.rozprasza_mnie);
Button But16 = this.findViewById(R.id.Button16);
But16.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozpraszaMP.start();
}
});
final MediaPlayer RozumiszMP = MediaPlayer.create(this, R.raw.rozumisz);
Button But17 = this.findViewById(R.id.Button17);
But17.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozumiszMP.start();
}
});
final MediaPlayer spoczkoMP = MediaPlayer.create(this, R.raw.spoczko);
Button But18 = this.findViewById(R.id.Button18);
But18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spoczkoMP.start();
}
});
final MediaPlayer SzefiemozeszMP = MediaPlayer.create(this, R.raw.szefie_mozesz_odejsc);
Button But19 = this.findViewById(R.id.Button19);
But19.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SzefiemozeszMP.start();
}
});
final MediaPlayer AleodejdzMP = MediaPlayer.create(this, R.raw.szefie_odejdz_ale_odejdz);
Button But20 = this.findViewById(R.id.Button20);
But20.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AleodejdzMP.start();
}
});
final MediaPlayer MozeMP = MediaPlayer.create(this, R.raw.moze_moze_moze);
Button But21 = this.findViewById(R.id.Button21);
But21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeMP.start();
}
});
final MediaPlayer RekoczynyMP = MediaPlayer.create(this, R.raw.zadnych_rekoczynow);
Button But22 = this.findViewById(R.id.Button22);
But22.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RekoczynyMP.start();
}
});
final MediaPlayer PowinnoDojscMP = MediaPlayer.create(this, R.raw.juz_powinno_dojsc);
Button But23 = this.findViewById(R.id.Button23);
But23.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PowinnoDojscMP.start();
}
});
}}
I have song which duration is 15min. But I would like to play this song for 30 minutes. Also when the user will download PRO version of the application, the song should play for 2 hours. How can I set the time?
I am new to Java and Android.
Thanks for your help.
public class MainActivity extends Activity {
Button start, stop;
// Button btn_play, btn_stop, btn_pause;
MediaPlayer mp;
TextView display, comment;
int length;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
length = 0;
start = (Button) findViewById(R.id.bStart);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, R.raw.splahsound);
mp.seekTo(length);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
mp.release(); // free up memory
mp = null;
length = 0;
}
});
}
});
stop = (Button) findViewById(R.id.bDur);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null) {
try {
mp.stop();
mp.reset();
mp.release();
mp = null;
length = 0;
} catch (Exception e) {
Log.d("error", e.toString());
}
}
}
});
}
}
Use TimerTask. A similar question has already been answered here, so pointing to that.