简体   繁体   中英

Cordova/PhoneGap Get File Path To Resource (Outside of Res Folder)

We have a Cordova/PhoneGap project and we are trying to play an audio file.

We have tried dozens of attempts to get the absolute file path and failed.

eg

    File file = new File("android.resource://www/audio/0.m4a");
               //also tried "www/audio/0.m4a"
               //also tried "file:///assets/www/audio/0.m4a"
           if(file.exists())
           {
               Log.d("1","FILE EXISTS");
           }else
           {
               Log.d("2","FILE DOES NOT EXIST");
           }

The file exists and is in the folder

在此处输入图片说明

And the file will be found (and play) if I move it to the /res/raw folder and use the R.res.0 to play it.

Do you know how I can get the absolute path to this audio file so I can create a File object from it?

Looks like you can't create file object from Assets.

Few Questions :

Why you are keeping file in assets folder, when you have res->raw folder ?

If you want to play from Assets, you need to do this

final MediaPlayer mediaPlayer=new MediaPlayer();  
final AssetFileDescriptor assetFileDescritor=pContext.getAssets().openFd(MusicFactory.sAssetBasePath
                    + pAssetPath); mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(),assetFileDescritor.getStartOffset(),assetFileDescritor.getLength()); 
mediaPlayer.prepare();
final Music music=new Music(pMusicManager,mediaPlayer);
pMusicManager.add(music);
return music;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM