1

Is the raw folder automatically considered by NativeScript when packaging the app?

I could not find any reference to this. So what I'm trying to do is to store a .mp3 file in that folder and use it then as a custom notification sound.

The problem is: How can I then reference that file? What should I write as a path? Here's the concerned code:

// how should my path be like here?
const sound = android.net.Uri.fromFile(new java.io.File('path????'));

const notification = notificationBuilder
    .setSmallIcon(ad.resources.getDrawableId('icon'))
    .setContentTitle('Some test notification')
    .setContentText('Done with native builder')
    .setPriority(2)
    .setSound() 
    .setVibrate([0, 550, 200, 500])
    .setLights(200, 500, 1000)
    .build();

Thanks in advance to anyone willing to help ❤️❤️

2 Answers 2

1

Try this for URI of sound file

    import * as application from 'tns-core-modules/application';

    const uri = new android.net.Uri.Builder()
        .scheme(android.content.ContentResolver.SCHEME_ANDROID_RESOURCE)
        .authority(application.android.nativeApp.getPackageName())
        .appendPath("raw")
        .appendPath("filename")
        .build();
1
  • Thank you so much! This worked perfectly! It was exactly what I was trying to do!
    – Caius
    Jan 25, 2019 at 0:48
1

There is no direct way to access the content of raw folder. Meanwhile you can access in following way.

export function createSound(){
    var context = app.android.context;
    var resourcestmp = context.getResources();
    var ident = resourcestmp.getIdentifier("test", "raw", context.getPackageName());
    readFile(ident);
}

function getFDForResource(context, resId) {
    var afd = context.getResources().openRawResourceFd(resId);
    if (afd != null) {
       return afd.getFileDescriptor();
    }
    return null;
}

function readFile(resId) {
    var context = app.android.context;
    var fd = getFDForResource(context, resId);
    if(fd != null) {
        var inputStream = new java.io.FileInputStream(fd);
        var nextByte;
        console.log(inputStream);
        while((nextByte = inputStream.read()) != -1) {
            // read here.
        }
    }
}

For Further read, you can refer here.

1
  • Hey! Thank you! Although I didn't mark it as correct one, because a bit more "complex" than the other one, it still works. Especially for the MediaPlayer, which accepts a context and a FileDescriptor. Thank you!
    – Caius
    Jan 25, 2019 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.