(Android) Initial lag in playing sounds - SoundPool and MediaPlayer both - java

So I've been learning Android dev for a short while now, and I'm writing a metronome app, with tap tempo. What I'm doing is keeping a play button, which starts the metronome when you hit it. In the button's onClick(), I've used a ScheduledExecutorService to repeat the Runnable, which has a SoundPool with the metronome's click loaded.
Here's the thing. The delay between the first two ticks of the metronome is noticeably less than it should be; then it evens out. So if I want it to tap at 120 bpm, it'll speed up to 150 bpm for the first two ticks and then go back to normal. I can only assume the initial tick has been delayed, for some reason.
I've tried using a MediaPlayer instead of a SoundPool, a TimerTask, separate Handlers and Runnables instead of the ScheduledExecutorService, but no luck. What could the problem be?

Related

Best Method of Displaying Time in Android Timer

I was curious and could use a bit of help with a Timer app I'm working on for Android. Is it a good idea to just use one single TextView that displays the time, or would it be better to use three individual TextViews (one for hours, minutes, and seconds)? I'll be using a Number Picker Dialog to set the values, but I am a bit confused how that will work if I just use one TextView.
Thanks for any repsonses.
Also, I can't use Timer, Chronometer, CountdownTimer, or Digital Clock View for this timer. Restricted to Asynch Tasks, Handlers, Threads. Professors orders.
Use Chronometer class of android API. It has methods for start and stop Chronometer. The XML View also available. also you will be able to pause and resume the Chronometer.
Three fields available (H:MM:SS) for timer in Chronometer.

Java Android: Mediaplayer fades automatically

I've figured out that the MediaPlayer on Android 4.4.2 seems to fade-in the Audiofile automatically.
I am using the MediaPlayer for playing a Sound that mustn't be faded in.
It worked well with a Smarthpone with 4.2.1, but on another Phone with 4.4.2 the Fading occurs.
I've also had a look at the SoundPool, but it misses the Feature of letting me know if the File is still playing.
Am I able to switch off the automatic fade-in or do I need to use the Soundpool and keep an eye on the length of the Track on my own?
Thanks,
VanDahken
You can try to setAudioAttributes(AudioAttributes attributes) before start playing.
atrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
Try to play with CONTENT_TYPE.
I have the same issue. The good thing is that it worked on a couple other Android versions, so hopefully only 4.4.2 has the problem. My first solution I discovered was to use an uncompressed music file... (.wav with data format LEI16) but the music file was way too big.
I also found that the fade-in doesn't seem to happen if you set the audio stream type to STREAM_RING but that's probably not a good idea since it's meant for phone rings.
For my app I only have one critical spot (when the app first starts) where the music has to play without the fade-in, and I was able to get a fix for that. The intro takes a few seconds before the music starts, so what I am doing is:
-prepareAsync() at the start of the intro
-then when prepared, setVolume() to 0f and start()
-then, after a small delay (for my case that ends up being a few seconds: the time it takes for the app's intro to complete), add a seek listener, and seekTo(0)
-when the seek completes, set the volume to the desired value

Implementing CountDownTimer properly

I am currently developing a game for the Android OS. In it, the player can carry out activities. Each activity costs the player "energy". The players current energy value is stored in an sqlite table and displayed on the home screen(home.class). When the player carries out activities, the energy value will decrease. When the energy reaches zero, i want a countdown timer to reset the energy value to 10.
The way I have implemented it so far is as follows. At the top of the main screen that displays the users health/energy ratings I have displayed the CountDownTimer.
The timer works. However, when I click a button and go to a different activity..and then return to the Home screen, the timer has restarted. I know this is because the following code runs when I want to return to the home screen:
Intent i = new Intent(Crime.this, Home.class);
startActivity(i);
It starts a new Home.class because i want to update the values displayed in the Home screen after some activities have been carried out.
What is a better way to implement the CountDownTimer? Any help would be appreciated...thanks :)
store the time at which you want to reset the energy in the database (at least once you hit onPause) and update the remaining time with a ticking thread thing. That way you don't need to rely on timers to finish / restart since you can always recalculate the times based on e.g. the current system time

Make sound cut itself off rather than play over itself

My 1st app is an audio sampler. A button starts an audio loop and other buttons play different sounds over the top.
If I hit a button multiple times, the sound will play over itself.
I want the last click to cut off any audio still playing from the same button. But I don't want it to interfere with any audio started from a different button.
(For a button that plays audio of someone rapping "my name is", 3 quick clicks of the same button should result in "my-my-my name is". But a loop should remain playing in the background).
I'm using SoundPool and OnTouchListeners.
Thanks.
I've never used SoundPool, but with MediaPlayer, if you use release() it should stop it. If you put it before the start(), it should stop it (if it's already playing) then start it again.
EDIT
From http://developer.android.com/reference/android/media/SoundPool.html#stop%28int%29
public final void stop (int streamID)
Since: API Level 1
Stop a playback stream. Stop the stream specified by the streamID. This is the value returned by the play() function. If the stream is playing, it will be stopped. It also releases any native resources associated with this stream. If the stream is not playing, it will have no effect.
Parameters
streamID a streamID returned by the play() function
Have you tried calling stop(streamID)?
If you look at the documentation for SoundPool, the constructor has a parameter for maxStreams. Also in the overview:
When the SoundPool object is constructed, the maxStreams parameter sets the maximum number of streams that can be played at a time from this single SoundPool. SoundPool tracks the number of active streams. If the maximum number of streams is exceeded, SoundPool will automatically stop a previously playing stream based first on priority and then by age within that priority.
You could create a separate SoundPool (separate from the loop) for this one sound and set maxStreams to 1.

How to make a protection of many times pressing the button?

In my app I use a camera, and I want to take a pictures. In my app is button (Photo). If I press it one times - all work perfect, but if I press button many times until camera take picture, my app hangs. How can I fix it?
In your onClickListener call Button.setEnabled() and set it to false.
Then set it to true when you've finished taking the photo.
Use a listener that disables the button on press (setEnalbed(false)), than start a countdown thread that re-enables it after some time, 200ms maybe, or whatever fits the best.
After second thoughts this might not be a really good idea.
There is a chance that the thread will not be scheduled to run, so if you exactly know the point when you can re-enable the button in your code, don't use threads.

Categories