How to generate buzzer at timer countdown? - java

I'm looking to play a sound when a CountDownTimer expires. I can't seem to get it working as I want. I've looked at some sample code using the ToneGenerator but it is not working as I'm expecting it to and because I'm new to android and java, not really sure how to get it working better.
Here is what I have:
- Countdown timer works. It starts and stops when I click on it and resumes when I click on it again. Everything is perfect here.
I have managed to play some sounds out of my device but it will actually repeat for some reason. Not sure why.
Here is what I want:
- onFinish() of the CountDownTimer, I would like to generate a tone around 2800Hz for a period of 2 or maybe 2.5 seconds.
- after I get it working, I would like to play with the idea of making it an alternating tone at say 2800Hz and 2000Hz for 50ms each or something and for a period of 2 to 2.5 seconds.
** EDIT **
public void soundBuzzer() {
ToneGenerator toneGen;
int type1, type2;
toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
type1 = ToneGenerator.TONE_DTMF_6;
type2 = ToneGenerator.TONE_DTMF_8;
toneGen.startTone(type2, 2000);
}

The ToneGenerator will generate a tone that sounds like the tone on a telephone when you hold down a number. I was unable to get the buzzer to sound using that class and making it sound good. As a result I ended up playing an MP3 for sound.
Here's the code:
// function to generate a tone for 3 seconds
public void soundBuzzer( boolean shortBuzzer ) {
MediaPlayer mp;
if( !shortBuzzer )
mp = MediaPlayer.create(appContext, R.raw.buzzer_2s);
else
mp = MediaPlayer.create(appContext, R.raw.buzzer_short);
mp.start();
}

Related

CountDownTimer doesn't count down by the specified intervals

I'm trying to build a count-down timer function in my app, however, it doesn't run as expected.
timer = new CountDownTimer(100, 10) {
#Override
public void onTick(long l) {
Log.d(TAG, "onTick: " + l);
}
#Override
public void onFinish() {
}
}.start();
My expectations are that it would count down gradually from 100,90,80...till 0
Although the output timer is showing quite random times (34 then 18 then 8..etc).
How can I achieve what I want?
It's difficult to explain the exact idea I'm trying to achieve but basically, I'm using touch listeners to hover over an item which in turn loads a few other items in the view. For some reason, there should be an intentional delay of around 100ms before these items could be displayed. I'm trying to build a progress bar that tells the user that something is loading rather than giving the impression that the screen is just stuck. I hope that helps to give a better idea of what I'm trying to achieve

how to know video start time is smaller than video duration in java-vlcj player

I am trying to develop a video player application with java - vlcj player. I need to give random start time for bunch of videos. However some of videos are shorter than my random start time. I need to skip these videos and play next ones.
Here is my code:
for(int i=0;i<videoCount;i++){
int delay = playTime*1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
videoPath = randomvVideoPath(directory);
//*** times[0] is start time, times[1] is stop time***//
String[] times = startStop();
/*** I DONT KNOW IF THE START TIME IS SMALLER THAN THE VIDEO DURATION******/
mediaPlayerComponent.getMediaPlayer().playMedia(videoPath,":start-time="+times[0], ":stop-time="+times[1]);
}
};
new Timer(delay, taskPerformer).start();
}
How can I check this? getLength() and getTime() of mediaPlayerComponent.getMediaPlayer() does not give the correct time.
Thanks in advance,,
getLength() and getTime() do return the correct time, but sometimes this information is not available until after the media is playing. That's just how VLC works.
I would suggest instead looking at Java bindings for the native MediaInfo command-line utility and using that to get your video length.

How to use `addMP3PlayerListener` with JACo MP3 player library

I am trying to create an automatic radio station using the JACo MP3 library. I have code to generate a random hour long playlist, and I can get JACo MP3 to play it no problem. What I need to do is somehow get JACo to notify me when the playlist has completed playing so that I can then generate a new hour long playlist and start playing the new list.
I think I need to use the method "addMP3PlayerListener" method, but don't know how to.
Right now my playlist will play, then the program just sits there.
Here is the code I have:
private void startStationActionPerformed(java.awt.event.ActionEvent evt)
{
boolean isStopped = true;
while(isStopped)
{
int djID = getCurrentDjID();
DiskJockey dj = new DiskJockey();
songListing = SongOpenedController.getSongList();
ArrayList<Song> theCurrentDJsSongs = dj.getDjPlayList(songListing, djID);
clearTable();
updateTable(theCurrentDJsSongs);
MP3Player myPlayer = new MP3Player();
for (Song song : theCurrentDJsSongs)
{
myPlayer.addToPlayList(new File(song.getSongPathName()));
}
myPlayer.play();
isStopped = myPlayer.isStopped();
}// end while
}
You don't actually need addMP3PlayerListener(). It's surprisingly simple, actually.
All you need to do is check if the music is stopped (myPlayer.isStopped()) and do it then.
Ryan

Trying to animate a die roll in Java

I'm attempting to "animate" a die roll in Java. I currently have an icon (called "diceImage") set up that, when a button is clicked (called "diceRoll"), updates to a new random image of a die face. What I would like to do is have it change image (to a random die face) numerous times over a couple of seconds before stopping on a final image.
The problem I have is not with generating a random number, or rolling it numerous times, it's with the updating the image numerous times within a loop. The code below, which rolls the die 10 times is what I have so far:
private void diceRollActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 1; i <= 10; i++) {
rollDice();
pause(100);
}
}
This links to the following two methods (the first of which generates the random number, and sets the icon image):
private void rollDice() {
Random r = new Random();
int randomNumber = r.nextInt(6) + 1;
diceImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Game/Images/Dice " + randomNumber + ".png")));
}
The method below is "supposed" to pause the programme briefly between updating the image (this was taken from a programming course I was on where we had to animate an image of a car moving across the screen):
private void pause(int sleepTime) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.exit(-1);
}
}
All this programme seems to do is pause and then print the final dice roll. It doesn't show any of the "intermediate" faces. Does anyone have any ideas on why this isn't working?
Any help is greatly appreciated.
This question is asked several times a day. If you sleep in the event dispatch thread, you're preventing it from doing its job: react to events and repaint the screen.
Your animation should be done in another thread. Read the tutorial about concurrency in Swing, and use a Swing Timer.
The pause() you are using is when u need to animate moving stuff like a car for example , but just changing the icon of a JLabel which is the dice here doesn't need pause ( unless you just want it to be delayed a bit) ...
but anyways , to solve your issue , you need to call repaint() on that JLabel or updateGraphics(). Setting the Icon for the JLabel doesn't make the new Icon get displayed , you just need to repaint() it .
of course like JB Nizet said , for the app not to hang you need to call repaint on a new Thread .Thought you have to learn how to use Thread well cuz it can be very tricky at times .
good luck

change image of imagebutton

I'm begining on android and java.
I try to make a simon game but have some problems.
I wrote this to show the simon buttons sequence or the button pushed by the player:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
for (int x = 1; x < 10000000; x++) { };
ib1.setImageResource(R.drawable.bullet_ball_green);
} else if (but_num == 2) {
It should change the image of each imagebutton, play a sound, wait some time (for {}) and then
change the image again....
But it doesn't work well... it plays the sound and really changes the image by bullet_square_xxx, but the eye can't see the image change, the change is only visible if the image is not changed back again by the bullet_ball_xxx :-(
I think this is my fault because I wrote the code different than java really works... I'm a
beginner and don't think in java... I have the visual basic program structure on my mind yet.
Thank You and sorry for my English !
This is probably caused by a delay on the event dispatch thread and the fact that the empty loop might be even ignored by the compiler since it is static, it is easily predicted to have no effect on the program. My suggestion is first force a repaint/update on the GUI and use Thread.sleep. Something like this:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
updateUI(); // if you are somewhere in a class extending any Frame/Panel
//If you are in other class use mainFrame.repaint();
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
try{
Trhead.sleep(3000);
} catch (InterruptedException e) {}
ib1.setImageResource(R.drawable.bullet_ball_green);
updateUI(); //only if this effect is delayed too
} else if (but_num == 2) {
ok....I think delay is the problem in your code. Since nowadays there are highspeed processors available that can count to 10000000 in a few ms, mine does. So instead of using the old-school for loop to introduce a delay use
Thread.sleep(5000);
this causes a delay of 5 sec, the argument is the time in milliseconds.
there is another thread which talks about introducing a delay:
How to pause / sleep thread or process in Android?
you could try this [i have copied pasted from that thread]:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ib1.setImageResource(R.drawable.bullet_ball_green);
}
}, 2000);
} else if (but_num == 2) {

Categories