2

Can i play a tone which is present in assets using Android Ring tone manger? I know i can use the tone copied in Res folder in following manner:

Uri inAppsoundUri = Uri.parse("android.resource://"
            + mAppContext.getPackageName() + "/raw/" + tone);**
ringTone = RingtoneManager.getRingtone(mAppContext,
                    inAppsoundUri);**
ringTone.play();

But when i tried replacing /raw/ with /assets/ in inAppsoundUri, it resulted in error. Actually i want to copy the tones to Sdcard also for some app specific usage for which i have to keep these tones files in assets, and also some times i have to play it through Ringtone manager for which i have to copy the same files in Res/raw folder. So there is unnecessary 2 copies of same tone files are present in the apk which results in larger apk size. So could you please suggest any way to do this both the functions(i.e. copy to sdcard and play using Android Ringtone mnager) from same place?

jjlema
  • 850
  • 5
  • 8
ssk
  • 151
  • 1
  • 9
  • can you please post error log ? – Haresh Chhelana Nov 25 '14 at 10:39
  • Even if the files are in res/raw you can copy them to external memory. Open an InputStream. Further code like copy from assets to sdcard. – greenapps Nov 25 '14 at 10:40
  • possible duplicate of [Play audio file from the assets directory](http://stackoverflow.com/questions/3289038/play-audio-file-from-the-assets-directory) – BSKANIA Nov 25 '14 at 11:05
  • @haresh: I get this exception after replacing /raw/ with assets Failed to open file 'android.resource://package_name/assets/Tone/tonee_file'. (No such file or directory). But i have verified that same file is there in mentioned folder. – ssk Nov 25 '14 at 12:28
  • I get "Failed to open file " error. @greenapps: That looks like a good option where i can avoid extra copy, thanks. In the link you mentioned, audio is getting played through Media player not Ringtone Manager. But i cant use media player, because if headphones are connected then tone is routed through headphones not through speaker, but i always want to play the tone through speaker. – ssk Nov 25 '14 at 12:34
  • `/Tone/tonee_file'`. You use file names without extension? – greenapps Nov 25 '14 at 12:41
  • Yes, that's how it was working for /raw/, but in assets i tried both with an without extension. No change... – ssk Nov 25 '14 at 12:44

1 Answers1

-1

I am check it but i think it help you.

Uri inAppsoundUri = Uri.parse("file:///android_asset/tone.mp3"); 

and place tone.mp3 in assert folder.

Thanks

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37