Android Nativescript中的原始文件(音频)文件夹

Android Nativescript中的原始文件(音频)文件夹,android,notifications,nativescript,Android,Notifications,Nativescript,打包应用程序时,NativeScript是否自动考虑raw文件夹 我找不到这方面的任何参考资料。所以我要做的是将一个.mp3文件存储在那个文件夹中,然后将它用作自定义通知声音 问题是: 然后我如何引用该文件?我应该写什么作为路径?以下是相关代码: // how should my path be like here? const sound = android.net.Uri.fromFile(new java.io.File('path????')); const notification

打包应用程序时,NativeScript是否自动考虑
raw
文件夹

我找不到这方面的任何参考资料。所以我要做的是将一个.mp3文件存储在那个文件夹中,然后将它用作自定义通知声音

问题是: 然后我如何引用该文件?我应该写什么作为路径?以下是相关代码:

// 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();

提前感谢任何愿意帮忙的人❤️❤️

无法直接访问
raw
文件夹的内容。同时,您可以通过以下方式访问

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.
        }
    }
}

要进一步阅读,您可以参考。

试试这个声音文件的URI

    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();

非常感谢你!这工作做得很好!这正是我想做的!嘿非常感谢。虽然我没有将它标记为正确的一个,因为它比另一个更“复杂”,但它仍然有效。特别是对于MediaPlayer,它接受上下文和文件描述符。非常感谢。