问题
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 ❤️❤️
回答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();
回答2:
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.
来源:https://stackoverflow.com/questions/54338035/raw-files-audio-folder-in-nativescript