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)? :)
Related
//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 am playing some audio via a button is pressed. The audio plays fine, but i want to be able to spam the button and let the audio play play play play. Even stop where its playing and play again if i press the button. Now when i press the button, the audio just plays all the way through and nothing happens if i press the button while the audio is playing. Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
mp.setLooping(true);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
Replace this code with your existing code. if you want to repeat your audio then you have to set the looping property to true.
OK - I hope I got this right:
You want to play the audio clip for every time you press the button and the clip should be played completely, before the next clip starts.
Here's what I'd do:
Create a new integer attribute.private int i = 0;
Increment it every time you press the button.
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IUP();
}
});
public synchronized void IUP(){
i++;
}
public synchronized void IDOWN(){
i--;
}
public synchronized int getI(){ return i; }
public void startAudio(){ mp.start(); }
public boolean isPlaying(){ return mp.isPlaying(); }
Use a seperate thread for the player.
Thread player_thread = new Thread(new Runnable(){
#Override
public void run(){
while(true){
if(getI() > 0 && !isPlaying()){
IDOWN();
startAudio();
}
}
}
}).start();
Or use an OnCompletionListener on the MediaPlayer.
Tell me if this is what you were looking for.
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(play_button.getText().toString().equals("Stop")
{
mp.stop();
play_button.setText("Play");
}
else
{
mp.start();
play_button.setText("Stop");
}
}
});
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);
}
}
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();
}
});
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.lastmohican);
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mp.isPlaying()) {
mp.stop();
} else {
mp.start();
}
}
}
I can play this music by one click and with another click music is stopped, but when I click together music does not start. Any idea why?
I'm not sure I understand your problem, but if you are wondering why the audio will not start back up after you press the button a third time, it is because you are calling stop() on the media player when you should be calling pause(). Stop requires you to prepare the media again before you can begin playback. Have a look at the MediaPlayer Documentation. If that is not your problem you need to provide a better description.
Its solved you should change code for this type
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}