Is it possible to calculate total time spent watching with media.playVideo?

Hello, I have created an app intended for only Android that lists a number of movies and, when the movie poster is clicked, media.playVideo plays the movie. Everything works great for playing movies, but the challenge is to detect how long user’s spend watching each movie. My goal is to be able to log the total number of seconds that users spent watching each movie.

On the device, I haven’t been able to run any background processes while the movies play with media.playVideo, and I’ve only been able to trap some of the scenarios where the user exits movies launched with media.playVideo.

With Corona (on Android), when media.playVideo is triggered, users can potentially exit the video by:

  1. tapping the Android back button
  2. tapping the Android home button
  3. tapping the Android switch app button
  4. pressing the device power button
  5. or an event can be triggered to occur when the movie ends

My first thought was to record the time right before the movie launches, and calculate duration by trapping all of the exit events listed above. Corona triggers a scene onExit event when the media.playVideo is initially triggered, so control goes to the video player, and it appears as if exiting media.playVideo only triggers a trappable Corona event if the user leaves the movie via #1 or #5 above. If the user exits the video via #2, #3 and #4, I haven’t been able to figure out any way of detecting that event.

My second thought was to start a timer loop and log the name of the movie and a duration while the video is onscreen. I tried creating a 15 seconds loop in both the scene that calls media.playVideo and in main.lua. This loop method works on the emulator (on OSX), but not on my Android device. On the device, all of my loops stop running as soon as media.playVideo is triggered.

Does anyone have any thoughts or suggestions that might work?

At this point, I’m willing to try just about anything.

This might help:

https://coronalabs.com/blog/2012/05/15/handling-corona-system-events/

Thanks JonPM. I hadn’t come across that link. The page helped me come up with a few new events to trap, but my tests were discouraging.

When media.playVideo is triggered in the code, the “applicationSuspend” event is triggered as the movie starts to play, which is just the same as what happens when someone hits the Android home button or when there is an incoming call. If someone hits the Android home button during the movie, powers down the device during the movie, or changes applications during the movie, Corona doesn’t appear to wake up, so there is no “applicationResume” event, which is what I had my fingers crossed for.

This might help:

https://coronalabs.com/blog/2012/05/15/handling-corona-system-events/

Thanks JonPM. I hadn’t come across that link. The page helped me come up with a few new events to trap, but my tests were discouraging.

When media.playVideo is triggered in the code, the “applicationSuspend” event is triggered as the movie starts to play, which is just the same as what happens when someone hits the Android home button or when there is an incoming call. If someone hits the Android home button during the movie, powers down the device during the movie, or changes applications during the movie, Corona doesn’t appear to wake up, so there is no “applicationResume” event, which is what I had my fingers crossed for.