Hi i have again a Problem,
my app plays sound 1 sec. this work fine but if i press less 20 time on any button then plays nothing more what is the error? How i can fix it?
tsc = (Button) findViewById(R.id.button12);
tsc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(R.raw.en_bye);
}
private void playSound(int sFile) {
// set up MediaPlayer
final int medFile = sFile;
Thread thread = new Thread(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), medFile);
mp.start();
}
});
thread.start();
}
Thank you
Related
The audio file (.mp3) gets played when I don't use the onPreparedListener, but I get an error after the file tries to replay after some time (because it's not listening for the state obviously). So basically the code is checking the battery state and if it's 10% or below it plays the alarm sound, I also have a button which I can click to stop the sound. Now with the onPreparedListener the alarm sound doesn't get played anymore. What am I doing wrong?
tv_battery = (TextView) findViewById(R.id.tv_battery);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm);
Button b = (Button) findViewById(R.id.stop_alarm);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
runnable = new Runnable() {
#Override
public void run() {
int level = (int) batteryLevel();
tv_battery.setText("Battery: " + level + "%");
handler.postDelayed(runnable, 5000);
if(level <= 10) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
mp.start();
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
count.start();
}
I think you have place mp.start(); on wrong place please move it before count.start(); as below :
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
mp.start();
count.start();
Also correct below condition if media player already started when battery is under 10 otherwise media player again prepared even after started :
if(level <= 10 && !mp.isPlaying()) {
I have developed app in which there are 4 sound files playing together
First sound :
on next button press
-Second sound :
on previous button press
Third sound :
plays sound of item
Fourth sound :
plays on item (How item speaks)
Everything works well until user play back and fourth quickly, text-to-speech engine crash with
MediaPlayer: error (-19, 0) android
i tried to reset media player , release and stop in next and previous button, but still sound of items stops after few back and fourth.
What i have done :
public class A extends Activity {
int imgArr[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d};
int soundsrc[] = {R.raw.a, R.raw.b, R.raw.c, R.raw.d};
String pro[] = new String[]{"a", "b", "c", "d"};
ImageView iView;
int count = 0;
TextToSpeech txtToSpeech1;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_names);
stopService(new Intent(this, PlayMusic.class));
iView = (ImageView) findViewById(R.id.imageView5);
iView.setImageResource(R.drawable.befora);
txtToSpeech1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
txtToSpeech1.setLanguage(Locale.UK);
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
}
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (txtToSpeech1 != null) {
txtToSpeech1.stop();
txtToSpeech1.shutdown();
}
super.onDestroy();
}
public void onPause(){
if(txtToSpeech1 !=null){
txtToSpeech1.stop();
txtToSpeech1.shutdown();
}
super.onPause();
}
public void aSounds() {
mp = MediaPlayer.create(ANames.this, soundsrc[count]);
mp.start();
}
public void forwardd(View v) {
buttonSounds(R.raw.multimedia_button_click);
if (count < imgArr.length && count >= 0) {
count++;
if (count == imgArr.length)
count = 0;
iView.setImageResource(imgArr[count]);
if (txtToSpeech1.isSpeaking()) {
Log.i("Tag", "Stop speaking");
txtToSpeech1.stop();
}
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
// Execute some code after 2 seconds have passed
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
}
public void backwardd(View v) {
buttonSounds(R.raw.multimedia_button_click);
if (count >= 0 && count < imgArr.length) {
count--;
if (count == -1) {
count = imgArr.length - 1;
}
iView.setImageResource(imgArr[count]);
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
}
public void onImageClick(View v) {
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
public void home(View v) {
buttonSounds(R.raw.multimedia_button_click);
Intent ii = new Intent(ANames.this, PlayMusic.class);
finish();
startService(ii);
}
public void buttonSounds(int src) {
mp = MediaPlayer.create(ANames.this, R.raw.multimedia_button_click);
mp.start();
}
}
-- i have been trying since very very very long
-- tried almost all stack-overflow answers also Google regarding media player working .
-- also how to stop , release , reset, start media player on button click, but nothing works for me..
-- So at last decided to put question in stack-overflow for help
-- any help is heartly welcome and thanks in advance
If the buttons plays a sound effect like 1-4 seconds. Consider using SoundPool class. It should be used for that stuff as I know. Media player errors LINK and LINK. It seems whoever were getting error 19 needed to release the resource which he was playing.
i'm newbie in this world of programming, i want reproduce one sound at time, is a application with long sounds of animals.
when i push some button make sound about some animals (gato make sound of a cat)
i fail to play a sound at time, i try with soundpool too, same results, obviously the problem are my low lvl of programming xD, can somebody help me?
public class MainActivity extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void presionGato(View v) {
mp.stop();
mp.create(this, R.raw.gato);
mp.start();
}
public void presionLeon(View v) {
mp.stop();
mp.create(this, R.raw.leon);
mp.start();
}
public void presionPerro(View v) {
mp.stop();
mp = mp.create (this, R.raw.perro);
mp.start();
}
public void presionTigre(View v) {
mp = mp.create (this, R.raw.tigre);
mp.start();
}
public void presionRata(View v) {
mp = mp.create (this, R.raw.rata);
mp.start();
}
public void presionSapo(View v) {
mp = mp.create (this, R.raw.sapo);
mp.start();
}
public void presionRana(View v) {
mp = mp.create (this, R.raw.rana);
mp.start();
}
}
I suggest to you, learn programming and search about it. You didn't listen anything from UI, how can you know which button was clicked?
first, initialize UI element and media player on onCreate;
Button gatoButton = (Button) findViewById(R.id.gato);
Button leonButton = (Button) findViewById(R.id.leon);
mp = new MediaPlayer();
then set onClickListener for each button.
gatoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionGato();
}});
leonButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionLeon();
}});
You can use
if(mp.isPlaying()){
mp.stop();
mp.release();
}
it will stop the music if it is playing then you can start the your new music again. It prevent to play the music at the same time.
I am trying to play an mp3 file (with an onClickListener) and stop after 2 seconds. I tried the code below but it is not working. Could anyone please help?
final MediaPlayer mpsound = MediaPlayer.create(this, R.raw.media_player_sound);
ImageView sound = (ImageView) findViewById(R.id.button);
sound.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});
Why are you calling stop() on mpfrog if you are playing audio with mpsound? You need to call the stop() function on the mpsound MediaPlayer. Also, you might want to add the #Override annotation to your onClick() method.
for the override...
sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});
for a timer.....
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
mpsound.stop();
}
};
//Task for timer to execute when time expires
class SleepTask extends TimerTask {
#Override
public void run(){
handler.sendEmptyMessage(0);
}
}
//then in some other function...
Timer timer = new Timer("timer",true);
timer.schedule(new SleepTask(),2000);
You have to wait 2 seconds in different thread, for this case use your own created thread and wait there and stop player, or you can use CountDownTimer - very simple solution.
You can find same question aggregated here and here
In eclipse you can use autocomplete (ctrl + space by default) to fill automatically all inmplemented methods and #Overrides will already be there.
I really hope you can help me here. Below is a section of my code that successfully runs two count down timers side by side...all I want to do is play a short mp3 file when count down has finished....I have tried many different bits of code but I am struggling to make anything work...an quick wins would be good..
So to round up two timers each need to play sound when finished..
//Declare Start/Stop button
Button btnstart = (Button)findViewById(R.id.btnstart);
Button Button1 = (Button)findViewById(R.id.Button01);
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField=(TextView)findViewById(R.id.counter2);
//Counter 1
final CountDownTimer Counter1 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText(" " + formatTime(millisUntilFinished));
}
public void onFinish() {
start();
}
};
//Counter 2
final CountDownTimer counter2 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter2TextField.setText(" " + formatTime(millisUntilFinished));
}
public void onFinish() {
start();
}
};
//Start Button1
btnstart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Counter1.start();
}
});
//Start Button2
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
counter2.start();
Thanks in advance
Dj
I assume start() is the function you call to play the sound, right ?
so inside the definition of start(), put the following code :
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound
mp.start();
Hope this helps!
Edit: trying to be super clear :)
Somewhere in your code, it is written :
public void onFinish() {
start();
}
This method/function is called when the counter finishes.
Inside this function it is written 'start()'
I don't know what this start() does.
In both cases, I suggest you keep it (if it doesn't create an error), and after start(), add playSound() inside the two onFinish() methods.
and then write OUTSIDE of this function, the following:
public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound
mp.start();
}