music loading/playing glitch - java

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.

Related

vlcj: player.mute() has no effect when player is not playing

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.

Android media player stop playing in service

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().

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

Prevent LibGDX game from completely stopping when paused

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)

How do i start activity when screen goes off?

I have an application which is doing its work only when devices screen goes off. I have set up broadcast receiver (Intent.ACTION_SCREEN_OFF) and it works fine. I got record in my LOGCAT always when devices screen goes off. So far so good.
My onReceive code from ACTION_SCREEN_OFF uses some code calculating stuff, and all executes fine (When screen goes off). So far so good.
At the end of my onReceive code i'm starting new activity, but the onCreate of targeting activity is NOT always executing. (For example on my HTC Desire 2.3.7 works fine. On HTC Wildfire S 2.3.5, Xperia Arc S 2.3.4 it does not execute, and on Samsung Galaxy ACE 2.2.1 , it depends. Sometimes is executing sometimes isn't). My LOGCAT shows that onReceive executed till the end but Activity was not started. I'm using following code to starting this activity:
Intent startH_Activity = new Intent(getApplicationContext(), HandleActivity.class);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startH_Activity);
The important things to note:
I said that on some phones mentioned above it doesn't work. Well its not working immediately, like it should (when SCREEN_OFF fires ). Always activity starts like.. 10-15 minutes after screen went off (which is not acceptable)
When screen goes off and activity does not start , if I press POWER button on device it immediatelyfires off my target activity and app is working like normal. (again this is not acceptable. It should fire automatically).
When ANY device is connected to the PC it works like it should. Activity starts immediatelyafter screen goes off.
After reading a lot of Stack Overflow I realized that this is because MAYBE device goes to sleep. That's why it works like normal if i press POWER button, or Wakes up automatically after 10-15min because it synchronizes or something. So I used WAKELOCK.
//OnCreate Service
powMan = (PowerManager) getSystemService(POWER_SERVICE);
wake = powMan.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
//OnReceive (screen goes off)
wake.acquire();
But still without a success. (Am i doing wrong?) Even wake lock is acquired my activity won't show up on these devices.
Basically what I'm asking. How do i open usual activity when screen goes off? Or at least how to turn screen ON if activity won't start (remember, pressing on power button shows up my activity
I would really need some help now, because I'm getting really frustrated about this. Thank you for help.
You should be starting Service instead of Activity when screen goes off.
I would register yet another broadcast receiver and start the Activity when screen goes on (Intent.ACTION_SCREEN_ON).
Two issues:
1) How do you pass data from one receiver to another? What comes to my mind is either starting a Service or saving the information to a file (or shared preferences).
2) What happens if you have two events that must start an Activity? (E.g. "something started" and "something finished".) You must resolve this, either showing both events or just the latest one.
Note that the user can turn the screen on and off without unlocking the screen (Intent.ACTION_USER_PRESENT), and that if the phone is configured not to lock the screen, Intent.ACTION_USER_PRESENT does not happen.

Categories