I am seeing the following behavior when toggling mute in vlcj:
before first playback of mediaPlayer: mediaPlayer.audio().mute() works fine
when playing: mediaPlayer.audio().mute() works fine
after first playback of mediaPlayer (when stopped): mediaPlayer.audio().mute() has
no effect
Problem: I have a mute button in my UI. When pressed, the button icon changes. But when started, the player is not muted, which makes the button show the wrong icon. It seems that you have to wait for the player to start playing before being able to mute it. I could get around this by calling setMute() on the first time-changed event, but it has the effect that even if muted through the UI beforehand, you will hear a fraction of the audio before setMute() takes effect. I have read this post: https://github.com/caprica/vlcj/issues/395 but it's quite old. I was wondering if there are better options now? In native VLC I don't see this issue, but I learned that it is not using libvlc. Is there anything one could do to get around this problem?
The vlcj sample application shows the same behavior btw: play some mp3 file, stop the player, toggle mute and start the player again -> the player is not muted.
setVolume() seems to have the same issue.
Unfortunately, this is just how the native media player in LibVLC 3.x works.
This does work as expected with LibVLC 4.x, and therefore vlcj-5.x, but VLC 4.x is likely still some way off from a full release, it's only available in unsupported pre-release nightly builds at the moment.
There may be some workarounds, but they will not be ideal - e.g. you could manage the mute/volume state yourself and apply it immediately when the media starts playing (listen for a "media player ready" event) - but obviously here you may get a short blip of unwanted (or missing) audio.
Instead of listening for "media player ready", you could maybe try applying the mute/volume on receipt of an "elementary stream selected" event with type audio, but I'm not sure this will work or be any better to be honest.
It may also be beneficial to listen to the various audio-related media player events, you have "muted" and "volume changed", and more recently "corked". At least then you might be able to keep your UI controls in sync with the actual state of the media player, even though it is not quite what you want.
So in short, I'm afraid with LibVLC 3.x there is not a good solution here.
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().
edit
It think all this stuff is now well out of date, there have been many changes to this as android has developed. I'll post an answer when I have worked it out (if noone else has).
I really just want a background app to get first crack at bluetooth messages.
end of edit
I have an android app that needs input from the user - I want to use bluetooth headset buttons (actually bluetooth watch/band buttons - but technically the same!).
I have been looking at media button examples, there are many here and elsewhere which all basically say:-
1) Put receiver/intent in manifest
2) Register receiver in main activity
3) Do processing in the receiver class
This isn't working for me (even cutting and pasting sample code), with no errors, but no events being triggered.
However I also found this:
http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html
Which is a dedicated bluetooth headset api... has it superceded the media button approach? or is it just higher in the food chain? Should this be used instead of the Media Button Event stuff - or are they just different perspectives?
If the media button stuff is the way, I can post my non-working code, but don't want to waste anyones time if its an out of date approach!
Thanks!
For examples of using bluetooth buttons - this is an excellent example...
https://code.google.com/p/media-button-router/
It detects a hit on the headset 'play' button then pops a dialog asking which app to direct the 'play' to - it plays the choices via the headset so one can be selected without viewing the screen.
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
In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?
The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.
I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.
You should use Asset Manager to prevent it, here a good tutorial :
http://code.google.com/p/libgdx/wiki/AssetManager
the official doc :
http://code.google.com/p/libgdx/wiki/AssetManager
Load all of your textures with assets manager
I have the same problem, but I found the solution by:
Disable option in Developer options called
- Don't keep activities (Destroy every activity as soon as the user leaves it)
I have a quick and dirty solution that might not work on android versions 2 and up.
The problem involved music preference (true or false) and loading the music. Basically, if the music preference was off, and then the user turns it on, the music loads in another thread but the function also calls play_music() right away. But the music doesn't play since it takes a relatively long while to load the music and the play_music() function called and ended. However, if then user turns off the music then turns it back on right away, the music plays since it's already loaded.
To get around this, I continually called this piece of code in the update method:
if (scr_get_pref_music() == true && Assets.msc_song != null)
{
Assets.msc_song.play();
}
It works on my android phone (version 4.04), but I wanna know if continually calling the play() function will cause any glitches or stuttering music in other versions of android (2 and up).
thanks
This might help you,
int status = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0);
If it is enabled, status== 1, otherwise status== 0.
Atleast it works for me.