Windows Phone timer under locked screen - timer

I try to make a Windows Phone app which plays sounds to help you fall asleep. I use SoundEffect class so that I can mix multiple sound files. The certification requirements says
"An app can play media in the background while it is running even when
its primary function is not related to music or video. An app that
plays music, audio, or sound effects must meet the following
requirements:" "The SoundEffect class must not be used to play a
continuous background music track in an app."
So if the SoundEffect class plays continuos music under locked screen is ok, right?
I made a timer with DispatcherTimer class, so that the user can set the time when sounds stop, so the battery won't go dead. But the certification requirements says
"All apps that run under a locked screen must stop any UI updates,
active timers, and other non-critical processing when notified that
the screen is locked."
So I can't use that, because my app has to be able to run under locked screen. What can I do to stop the music playing after a time interval or at a set time?

and other non-critical processing
Turning off the music is a key feature of your app, and therefore is a critical processing.
That said, since the DispatcherTimer has a strong dependance on the UI, I don't know if it will run properly under the lock screen. You should use a classical timer instead, or even better, just a thread with a Thread.Sleep instruction.
For instance, if you must stop the music two hours after the screen is locked, start a thread in the Obscured event and make it sleep for two hours:
private void TurnOffMusic()
{
Thread.Sleep(1000 * 60 * 60 * 2); // Sleep for two hours
// Turn off the music
}

Related

Background playback of videos in Codename One

I have the following code:
MediaPlayer media = new MediaPlayer(MediaManager.createMedia(thePath, true));
media.setAutoplay(true);
video.add(BorderLayout.CENTER, media);
that works. I want that the users can continue to "listen to" the video if they want to put the smartphone in their pocket. It can make sense when the video doesn't requires to be watched (like for a talking, a conference or other similar talking videos). Usually an user presses the hardware button to power off the screen before putting him/her smartphone in the pocket, that means to put the app in the background (I'm not sure if it's exactly as putting the app in the background, please correct me if I'm wrong).
Moreover, I want that the users can continue to "watch to" the video if they put the app from background to foreground.
How can I implement this use case with Codename One?
In this blog post https://www.codenameone.com/blog/material-icons-background-music-geofencing-gradle.html, there is a section about "Background Music", but this use case is different. I need "Background video".
For iOS the build hint ios.background_modes=music build hint should work. Android might be more problematic as it requires a different media API call and right now that media API call is designed for audio.
It's possible that you can detect suspend (stop call) and stop the video playback then switch to MediaManager.createBackgroundMedia() and continue from the same offset. Then detect the start() call to resume the video. I haven't tried this though and I'm not sure if it's the right way to do this.

ThreadPoolTimer not run when app is suspended

I create a ThreadPoolTimer to refresh my token(which will expire in 12 hours), and the timer is expected to be ticked in 12 hours. However, I put my UWP app in background over 12 hours, and then resume the app, the timer is not ticked even though the token is expired. Seems that when the app is background, the ThreadPoolTimer is also suspended. I tried DispatcherTimer, which is not suspended when the app is in background, however it's only available in UI thread. Is there any replacement Timer in UWP can meet my requirement?
This is normal behavior - when the app is suspended, all it's processes are being stopped - take a look at App's Lifecycle.
Your scenario - to run code in 12h interval, fits BackgroundTask with TimeTrigger. Take a look at MSDN and there is also a sample.
Once I've also written a blog post about running such task - maybe will help. Also take a look at this answer at SO.

App freezing after several restarts

I built this app that takes pictures, displays them for acceptance and uploads them to a webserver via post.
It is very simple in concept and execution. But then the app is freezing in the Android handset (I have an Xperia Z3+ which i has a fairly good amount of resources, also tried in a Moto X).
To try and reproduce this I take a picture (it will automatically tried the upload)... push the power button for the screen to shutdown... then when I light up the screen it takes a while for the app to start working again (I can see because I have a background animation). After some retries of these... the app will freeze and I'll have to close it to retry.
Any suggestions on how to troubleshoot this problem?
My thinking is that I can aliviate the problem a little by removing the animation elements from the screen... at least the perceived performance will be far better is the app does not appear frozen on any app switch... therefore I want to be able to use the onPause, onStop from the activity lifecycle events to clear the animations. I guess those objects are serialized, so I will save serialization and deserialization time and also the perceived performance will improve as no freezed app will be seen while the onResume, onRestart events.
Is this posible?
Thank you very much
Chuck
This isn't caused by the animation, it may be caused by an uncaught Exception or poor app performance.
To review your app performance, move any long process that requires feedback to the UI (AsyncTask) to postShow() method of your forms and not beforeShow(). If it doesn't require feedback to UI in real time (IntentService), consider using Display.getInstance().scheduleBackgroundTask() which runs your task on a low priority thread while serializing it and this can be done in the beforeShow() method. If your forms are handcoded (Not GUI), do heavy long process in addShowListener().
Also cut down the amount of images you use in your app as this could also hinder your app performance when loading heavy images.
Avoid unnecessary use of revalidate(), usually by not calling it in a loop, it's a bit expensive, use repaint() instead.
You can also use Android ddms to check if your app is running into some errors.
Locking your screen or minimizing shouldn't affect your app in anyway other than when the app is starting up and the splash screen is shown (This usually freezes the app if you minimize your app when splash is shown). I believe this is a known issue though.
Another option could be the "suspend-resume" behavior. When an app is suspended (power button, incoming phone call etc.) the stop() method is invoked followed by a call to the start() method when it returns.
If you have a progress indicator during the stop() method then the restore call will reshow the progress indicator with the previous form as its "before form". That way when the progress indicator is dismissed it shows the "previous form". You can test this behavior in the simulator using the "suspend/resume" menu.
To workaround it just dispose the progress indicator in the stop() method as such:
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}

Apprpriate Timer object to run audio in WPF

I have large WPF application which also uses MEF.
I want to trigger an Audio alert on certain condition and repeat it for the times specified by user. audio file can be .wav or .mp3.
I am making use of SoundPlayer to play the audio.
I am not sure which timer to use for repeat intervals.
I dont want to block UI thread when audio is playing and also want it to be threadsafe.
thanks in advance.
Why not consider using Event Aggregation, which is an implementation of publisher subscriber pattern. Whenever your application encounters a condition where it wants to play audio, it would publish its intensions via the event aggregator.
There would be a listener who listens for these events and would play the correct audio based on the type of the event. If there are multiple requests, the listener may play them in parallel or in a sequence. You can implement the desired threading model within the listener and standard thread-sleep
This way, you get to keep all you audio configuration and logic tucked behind a single module. All the other modules will just ‘tell’ this module what they want to play (and optionally for how long)
On the other hand, unless your application is large enough the above approach would be an overkill
A good write-up on event aggregation
http://codebetter.com/glennblock/2009/02/23/event-aggregation-with-mef-with-and-without-eventaggregator/

How to check whether an app is Idle?

I have a requirement where I need to show a Lock screen , when the app remains idle for certain period of time. In WP7 is there any way to get the Idle time(Any OS APIs). I have gone through MSDN documentation of PhoneApplicationService.UserIdleDetectionMode and PhoneApplicationService.ApplicationIdleDetectionMode but it is just for enabling and disabling.I need some method by which I can get the Idle time. Thanks in Advance
To my knowledge, there isn't a way of doing that in the current API version.
You could implement a timer in your application and reset it every time there is an user interaction (with your controls) within your application.
The bigger question is how you should handle the OS lock screen? I mean, if your application has an in-app custom lock screen that has kicked in, and then the OS lock screen kicks in, then the user has to unlock 2 screens. Not especially user friendly in that case.

Resources