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.
Related
I already asked about it before, but my question even after edits about my progress of fix this noone answered to it anymore. This is original question:
Android media player stop playing while in background
So in short, I'm making music player app, but mediaPlayer stop sometimes when loading next song. After many tests I find out that it stops at mediaPlayer.prepare() and it won't continue as long as I don't trigger any action on phone like turning on display, volume up/down, click headset button. I'm out of ideas what can be a reason of it.
maybe you need to call prepareasync() instead of prepare().
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
I have an activity listening for volume button events using onKeyDown(), however I am not able to see these events when the screen is off.
Is there any way I can still listen for volume button presses when the screen is off?
No, This is not possible usually because broadcast receiver not work when screen light off because after screen off your volume button get lock and volume level not change.
Alternative Way to do this:
But you can achieve using run Media Player with zero sound in background infinite times that keep your volume button active always or you can use Wake Lock to achieve this.
But I don't suggest to do this method of long time because it totally drain the battery very fast. You can use these alternative method to do this for short period of time otherwise mobile battery get down fastly.
If you wish to control the volume of audio played through your app, make use of setVolumeControlStream() method. It directs volume key presses to the audio stream you specify.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
However, if you wish to make the volume button a shortcut key for your app, I'm afraid there is no straightforward solution.
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?
I'm trying to use JMF to play a sound every time I hit a button.
This is what I have to contruct the media player:
MediaPlayer mp = new javax.media.bean.playerbean.MediaPlayer();
mp.setMediaLocation("file:///D:/fall.wav");
mp.setPlaybackLoop(false);
And, to play the sound once, I do
mp.start();
mp.setMediaTime(new Time(0));
mp.stop();
The first time I play the sound, i get a
javax.media.NotRealizedError: Cannot set media time on a unrealized controller
and an
Exception in thread AWT-EventQueue-0" javax.media.NotRealizedError:
Cannot set media time on a unrealized controller
on the setMediaTime line. This doesn't happen any subsequent time I hit the button that makes the sound, it only happens the first time. If i change the constructor and add
mp.realize();
at the end, then it doesn't throw those errors, but it also doesn't play the sound the first time.
How do I go about fixing this?
mp.start() is asynchronous. So when you call mp.setMediaTime(new Time(0));
immediately after calling start(), the player is not yet realized.
You could add a ControllerListener and listen to its events. Once you get the RealizedEvent, then set the media time. It will be a good idea to initially call mp.realize() and on getting the event, set the media time and then call start()