Nativescript中的RAW文件(音频)文件夹

时间:2019-01-24 01:15:06

标签: 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 = 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();

在此先感谢愿意帮助❤️❤️

的人

2 个答案:

答案 0 :(得分:1)

没有直接方法可以访问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.
        }
    }
}

要进一步阅读,请参阅here

答案 1 :(得分:1)

尝试此操作以获得声音文件的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();