Screen lock/unlock detection in Ionic App - plugins

My question, is there a way for a ionic app to detect whether the device screen is locked/unlocked? I do know the app can detect when it becomes 'paused' and is running in the background but this is not what I mean. The reason for this is I want to make a geolocation function that only runs while the device screen is unlocked. Thanks

you can use ionic events pause resume see this snippet
.run(function($ionicPlatform) {
$ionicPlatform.on("resume", function() {
// do something update your interval
}
$ionicPlatform.on("pause", function() {
// do something here to store the timestamp
}
});

Related

How to scan for BLE devices in flutter background

I am using flutter_blue plugin to scan for BLE device i have a pressure monitoring device which broadcasts BLE signal when there is a change in pressure. i want to receive these values in my flutter app then send them to Firestore.
Everything works fine when the app is in foreground but it does not works when app is in background.
Below is my scanner function
void scanner() {
flutterBlue.startScan(timeout: Duration(days: 5));
flutterBlue.scanResults.listen((results) {
if (results != null && results.length > 0) {
for (var i = 0; i < results.length; i++) {
bool checkpoint1 = results[i].device.id.toString() == frontDevice;
bool checkpoint2 = results[i].device.id.toString() == rearDevice;
if (checkpoint1) {
first = results[i].advertisementData.manufacturerData;
}
if (checkpoint2) {
second = results[i].advertisementData.manufacturerData;
}
if (results.length - 1 == i) {
if (first != null && second != null) {
deviceData(first, second);
}
}
}
}
});
}
If you want to run something on background mode[when app is minimized] then you have to drill down to platform specific solutions. At-least for iOS Isolate or any other solution will not work because iOS pauses app after few seconds when user minimized app.
For iOS that you need to enable BackgroundMode to make iOS aware that you are using specific services and you need a privision to running app on background. Only few things can be run on background and Bluetooth is one of the thing. You need to setup few things in iOS project. Here you will find details: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW7
For Android you may use Foreground Services to keep running your Bluetooth related stuff.[Note that I have very basic knowledge about Android. There could be better suggestion]
For Android, there are some limitations like if you try to write Characteristics from thread except Main Thread then you may get exception. Here are few important notes about it: https://github.com/xabre/xamarin-bluetooth-le
Refer to "Caution! Important remarks / API limitations" section specially.
Note for iOS: As far as I understand Isolate can run code snippet on background thread but can not run something when app is minimzed and iOS is pausing execution of source code.
If you start a background service, then please do it cross platform, unless you need this service to run while the app is running (not as a background fetch, meaning the app is in the foreground, but is running not on the main thread).
Please use this package here if you need to run the flutter_blue plugin. If you try to run any other plugins, it will fail:
https://pub.dev/packages/flutter_isolate
Also if you need this to run as a background fetch, then please use this plugin here:
https://pub.dev/packages/background_fetch

How to display the big play button in the Azure media player?

I'm new to the Azure media player.
I would like for the big play button to appear whenever the video is paused. I can see the element in the html, just not sure how to make this happen.
Thanks!
I agree with #vince in the answer, But the Big play Button wont go away when we want to play the paused video
so here you should do the following
myPlayer = amp(id, playerOptions, function() {
console.log('Good to go!');
this.addEventListener('pause', function() {
jQuery(".vjs-big-play-button").show();
});
this.addEventListerner('play', function(){
jQuery(".vjs-big-play-button").hide();
});
});
Add this code on your pause event.
$(".vjs-big-play-button").show();
Let me know if it works.

How to stop video player on samsung Smart TV App without to show the black screen

Hi I'm developing Samsung app with SDK and emulator 4.5. I have a issue..The App shows by videoPlayer some videos loaded on server. My issue is: when the video play and I push button STOP, the video stops and the screen becomes black..I'd like to show the screen with the first frame of the video.
I have tried to do:
sf.service.VideoPlayer.setKeyHandler(tvKey.KEY_STOP, function()
{
if(Popup.getPopup()==Popup.getNPOPUP() && Similars.getOpenS()==false){
//videoPlayer.enterVideo(videoPlayer.url,videoPlayer.title,videoPlayer.from,videoPlayer.axoid,videoPlayer.nid); //riparte l'esecuzione del video
sf.service.VideoPlayer.stop();
videoPlayer.play();//insert function play
sf.service.VideoPlayer.pause();// stop video
videoPlayer.setFullScreen();
sf.service.VideoPlayer.show();
sf.service.VideoPlayer.pause();//stefa
}
});
I have inserted:
videoPlayer.play();//insert function play
sf.service.VideoPlayer.pause();// stop video
The play works but the pausa command not work
How can I do? Have you got a solution?
Thanks
I have inserted below
sf.service.VideoPlayer.stop();
setTimeout(function(){sf.service.VideoPlayer.pause();},2000); //delay function called of 2000 millisecond
I resolved my issue.
if delay time is not enough, you can increase the time to get the finish request.

Prevent iOS mobile safari from going idle / auto-locking / sleeping?

In an iOS app you can set application.idleTimerDisabled = YES to prevent the phone from auto locking.
I need to do this in mobile safari for a game like Doodle Jump where the user may not touch the screen for an extended period of time. Is there any documented method or hack to do this?
(Update)
They seem to be doing it somehow in this site http://www.uncoveryourworld.com. Visit from your iphone and when you get to the buildings/street scene with music playing in the background just leave your phone alone. It never goes to sleep.
(Update 2)
I've spent some time taking a closer look at how they might be keeping the phone from going to sleep. I've done a barebones test and it seems that the way they are looping the audio in the street scene is what keeps it from going to sleep. If you'd like to test this just put a simple audio player that loops on your page and click play:
<audio src="loop.mp3" onended="this.play();" controls="controls" autobuffer></audio>
Everywhere I searched it is being said that this isn't possible, so it is nice to see there is at least some way to do it even if a bit of a hack. Otherwise a browser based game with doodle-jump style play would not be possible. So you could have a loop in your game/app if appropriate or just play a silent loop.
NoSleep.js seems to work in iOS 11 and it reportedly works on Android as well.
Old answer
This is a simple HTML-only method to do that: looping inline autoplaying videos (it might also work in Android Chrome 53+)
<video playsinline muted autoplay loop src="https://rawgit.com/bower-media-samples/big-buck-bunny-480p-30s/master/video.mp4" height=60></video>
See the same demo on CodePen (includes a stopwatch)
Notes
Avoid loading a big video just for this. Perhaps make a short, tiny, black-only video or use
To make it fully work, the videos needs to be always in the viewport or you need to start its playback via JS: video.play()
Edit: This work around no longer works. It is not currently possible to prevent the phone from sleeping in safari.
Yes, you can prevent the phone to sleep using an audio loop. The trick won't start automatically, you will have to play it when the visitor touches the screen.
<audio loop src="http://www.sousound.com/music/healing/healing_01.mp3"></audio>
Test page: tap play and the display will stay on but it will dim on some devices, like an iPhone with iOS 7.
Note: be careful using this trick because it will stop any music that the visitors might be using—and it will annoy them.
No, you can't do this, unfortunately. The only way to achieve this is by making a UIWebView-application and setting the variable you provided there.
https://stackoverflow.com/a/7477438/267892
[edit] random bug behavior, sometimes lockscreen media controls showing, sometimes not
Years later, updated my code
Easy steps :
unlock audio context
create silent sound
loop it and play forever
keep tab active
Working on Safari iOs 15.3.1, tab & browser in background, screen off
// unlock audio context
let ctx = null;
// create silent sound
let bufferSize = 2 * ctx.sampleRate,
emptyBuffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate),
output = emptyBuffer.getChannelData(0);
// fill buffer
for(let i = 0; i < bufferSize; i++)
output[i] = 0;
// create source node
let source = ctx.createBufferSource();
source.buffer = emptyBuffer;
source.loop = true;
// create destination node
let node = ctx.createMediaStreamDestination();
source.connect(node);
// dummy audio element
let audio = document.createElement("audio");
audio.style.display = "none";
document.body.appendChild(audio);
// set source and play
audio.srcObject = node.stream;
audio.play();
// background exec enabled
Even if this approach might not be suitable in every case, you can prevent your phone from locking by reloading the page using Javascript.
// This will trigger a reload after 30 seconds
setTimeout(function(){
self.location = self.location
}, 30000);
Please note that I tested this with iOS7 beta 3
You can stop sleeping and screen dimming in iOS Safari by faking a refresh every 20–30 seconds
var stayAwake = setInterval(function () {
location.href = location.href; //try refreshing
window.setTimeout(window.stop, 0); //stop it soon after
}, 30000);
Please use this code responsibly, don't use it "just because". If it's only needed for a bit, disable it.
clearInterval(stayAwake); //allow device sleep again when not needed
Tested in Safari iOS 7, 7.1.2, and 8.1, but it may not work in UIWebView browsers like Chrome for iOS or the Facebook app.
Demo: http://jsbin.com/kozuzuwaya/1
bfred.it's answer works if you replace the audio-tag with a enter code here -tag - but only if the page is open in iOS10+ Safari AND the user has started the video. You can hide the video with CSS.
Also, I suspect that this feature will also be removed at some point.
This is based on nicopowa's answer, which saves a PWA from being suspended by iOS. (Playing an infinite loop of nothing keeps the app running - even with the screen turned off.)
In order to also make sure that it's triggered by user interaction,
the only thing to change is instead of
let ctx = null
put
let ctx = new AudioContext()

Allow application continue running when iPhone gets locked

I have developed an iPhone app which gets the microphone recorder inputs every second using NSTimer, calculate the amplitude of sound, and take appropriate action if required. I have noticed that when my iPhone gets locked, my application stops running(it doesn't fire the timer callback function). When I unlock iPhone, it start automatically. Could anyone please tell me how can I allow application responding when iPhone gets locked? Is there any other way around such that iPhone shouldn't get locked when my application is running? What should be Apple's recomendation on this?
I found this article but not sure it is correct way to do.
Thanks.
You cannot prevent timer from being stopped when application goes into idle state. Only one possible way is to disable idleTimer of the application.
application.idleTimerDisabled = TRUE;
//or
//Disable screen dimming if no user input occur on device.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];