My custom audio application starts to play when the Main activity begins, but I'd like it to start playing when the user hits the "Play" button. Just for the record I'm not using the Android MediaPlayer, but my own player.
When I simply use startPlayer() it runs fine, when the Activity starts, but when I put the startPlayer() invokation inside the setOnClickListener() it doesn't work.
The method startPlayer() in being called (it's logging fine), but the audio player doesn't fire up. Why?
Here is the code:
play = (Button) findViewById(R.id.playbutton);
play.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});
}
private void startPlayer() {
try {
player.playAudio();
} catch (Exception e) {
e.printStackTrace();
finish();
}
}
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});
Related
buttonMusic = findViewById(R.id.buttonMus);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick();
}
private Button buttonMusic;
private MediaPlayer musicSound;
public void buttonClick() {
buttonMusic.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
}
);
}
public void soundPlay(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.stop();
}else {
sound.start();
sound.setLooping(true);
} }
Hello.
The code launches the music, is able to stop it, but it wont play again after pressing the play button, after pausing the song that is.
you aren't pausing song, you are stop()ing it. use sound.pause() for pausing :)
after stop() MediaPlayer you have to prepare it again, use prepare() or prepareAsync(). MediaPlayer.create( makes that for you at the beginning, also if you would create MediaPlayer using its constructor you would also want to prepare()/preapreAsync() before calling start() in onClick
check out state diagram of MediaPlayer
//this is inside onCreate function
//layoutRost is declared as private, inside onCreate is initialized, also speak_rost...
layoutRost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speak_rost.start();
speak_rost.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
speak_rost.release();
}
});
}
});
First time I press it, the button & audio works well, second time I get the error: Open app again
It's been a while since I last touched Android, I created my own method for playing sounds and it worked fine considering I published an app, so maybe it would help
//method that plays a sound effects
public void playMedia(int resid){
mediaPlayer = MediaPlayer.create(this,resid);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
Every time there was something to play I would pass the id of the resource to this method
//just initialize inside ClickListener, speak_rost, and after .setOnCompletionListener, I put speak_rost().start; that was ALL, very nice problem, now all my 50+ buttons/layouts are "in the right form" to play 100+ time, every time I needed. Thank you.
layoutRost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speak_rost = MediaPlayer.create(hundred.this, R.raw.asa);
speak_rost .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mysound) {
mysound.release();
}
});
speak_rost .start();
}
});
I want to play music when a button is clicked, but I want to play different music with different button then the first music stopped and the second music would play.
code:
charge = MediaPlayer.create(this, R.raw.charge);
santai1 = MediaPlayer.create(this, R.raw.santai1);
}
public void charge(View view) {
charge.start();
}
public void santai1(View view) {
santai1.start();
}
Say, I clicked charge button and then played the charge.mp3. Then I want to play santai1.mp3 but when I clicked the santai1 button the charge.mp3 will stop playing and the santai1.mp3 will play.
I want this to be able the opposite and it can be done continuously.
When I clicked santai1 button then santai1.mp3 will be played, while the santai.mp3 still playing I clicked the charge button. I want the santai1.mp3 stopped and then play the chage.mp3.
When I clicked on charge button, then I clicked on santai1 button, both music started together.
if you need it for many
create a mediaplayer object and a method.
private MediaPlayer media;
private void stopMusic() {
if (media != null) {
media.stop();
media.release();
media= null;
}
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMusic();
media= MediaPlayer.create("you acivityname".this, "mp3file directory");
media.start();
}
});
try this:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
and do the same for other button
well it's simple you need 2 if statments for the buttons to check if one of them is playing.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(santail.isPlaying()){
santail.stop();
}
charge.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button
when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong
but i need the song to be paused so whereever i used
mp.start();
function which will be start to play the music from the url i added the code as
if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}
but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously
please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function
the full code of the project is
public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {
MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
//mp.start();
/*if(mp.isPlaying()== true)
{
mp.start();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.pause();
mp.release();
iv.setImageResource(R.drawable.play);
}*/
}
#Override
public void onClick(View v) {
try
{
pd = new ProgressDialog(this);
pd.setMessage("Buffering.....");
//pd.show();
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
mp.prepareAsync();
mp.setOnCompletionListener(this);
}
catch(Exception e)
{
Log.e("StreamAudioDemo", e.getMessage());
}
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();
}}
thanks for your help in advance
i have commented some code above i have to re correct it
you can have a try at your onclickListener,when this player is playing,if you want to pause it ,you should to call MediaPlayer.pause() ,if you want to release it data,you can call MediaPlayer.release(),then ,you can play next music.
use this
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
// mp.start(); - this should be removed
if(mp.isPlaying()== true)
{
mp.pause();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.start();
mp.release();
iv.setImageResource(R.drawable.play);
}
}
I have the following code:
if (someId.matches("A") || someId.matches("a")) {
tvLetCap.setText("A");
tvLetLow.setText("a");
ivLetterIcon.setImageResource(R.drawable.apple);
btnDisplayWord.setText("A is for APPLE");
mpSound = MediaPlayer.create(this, R.raw.sound);
mpSound.setLooping(false);
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*try {
Uri uri = Uri.parse("android.resource://com.mypack.testing/" + R.raw.sound);
mpSound.setDataSource(getApplicationContext(),uri);
mpSound.prepare();
*/
mpSound.start();
btnPlay.setVisibility(View.GONE);
btnStop.setVisibility(View.VISIBLE);
btnStop.setOnClickListener(stopSound);
/*}
catch (IOException e){
Log.e("REPLAYING", "prepare() failed");
}*/
}
});
mpSound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpSound.release();
btnPlay.setVisibility(View.VISIBLE);
btnStop.setVisibility(View.GONE);
}
});
}
It plays fine with the codes commented out but whenever I try to replay the file my app FC. The sound file is located in res/raw/sound.mp3
How can I modify the code so it plays as many times as the btnPlay is pressed.
You've released the media player. If you want to reuse it, don't do that.
There are some methods for mediaplayer...
http://developer.android.com/reference/android/media/MediaPlayer.html
Maybe you need to use mpSound.reset() in onClick method for button (some time ago i had such mistake :p)? :)