Search code examples
nativescriptangular2-nativescriptnativescript-plugin

How can I create a MediaSession in Nativescript?


I'm trying to create an Android application with Nativescript. So far I'm doing great, except for one thing (and - considering this is an app for listening podcast - maybe the most important one).

To reproduce mp3 files in my app, I'm using a plugin quite famous, named nativescript-audio. It works great, except that doesn't show any control when phone is in Lock Screen. Which is something I want to give to user.

Reading some android developer documentation, I found out I might need a MediaSession, and then add some icons with actions and write some callback functions to manipulate the player.

Problem is: in Nativescript, how can I do that?

this.session = new android.media.session.MediaSession(
    android.content.Context,
    'My Media Session'
)

This is the snippet I used in my player TypeScript class. But not working, because I receive this error:

Error: Cannot marshal JavaScript argument function () { [native code] } at index 0 to Java type.

Anyone with this problem before willing to help me? Thanks a bunch!


Solution

  • You should pass a valid context, you are passing the context class definition itself.

    import { android as androidApp } from "tns-core-modules/application";
    
    this.session = new android.media.session.MediaSession(
      androidApp.context,
      'My Media Session'
    )