android - How to get a .m4a audio file from a .opus audio file programmatically -
i developing application manages whatsapp voice notes. time ago whatsapp started save voice notes using opus file format, instead of aac previously.
my application gets uri (via implicit intent) points opus voice note stored in whatsapp folder.
since opus audio files impossible play on many devices , in many apps (viber example), create in separate file m4a format version of opus audio file whatsapp.
what tried this: used contentresolver , openinputstream() inputstream on content represented uri via implicit intent. made fileoutputstream on file in application folder. , used path finishes ".m4a" instead of ".opus".
like this:
try { inputstream inputstream = getcontentresolver().openinputstream(urifromexternalintent); string output_directory = environment.getexternalstoragedirectory() + "/audios/recordings/"; boolean exists = (new file(output_directory)).exists(); if (!exists) { new file(output_directory).mkdirs(); } calendar c = calendar.getinstance(); simpledateformat sdf = new simpledateformat("yyyymmdd_hhmmss"); string strdate = sdf.format(c.gettime()); //setting output file complete final path string output_file = output_directory + strdate + ".m4a"; file outputfile = new file(output_file); outputstream out = null; try { out = new fileoutputstream(outputfile); byte[] buf = new byte[1024]; int len; while ((len = inputstream.read(buf)) > 0) { out.write(buf, 0, len); } } catch (exception e) { e.printstacktrace(); } { try { if (out != null) { out.close(); } inputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } by saving file in path ends ".m4a" on api 24 devices able play file inside app , on apps whatsapp, viber or telegram. on api 23 same code gives errors on every app apart whatsapp.
to recap, looking way programmatically .m4a audio file uri points .opus audio file.
there apps able without showing kind of conversion, guess there trick without converting. advice?
copying file different extension not way go.
you have couple of options depending on how robust want app , how work willing put in.
the cheap , easy route find free online file conversion tool can try take advantage of in app. won't bother link particular service because don't recommend approach. app @ mercy of service chose stability. it's doable, ugly.
the more sound approach convert file in application. easiest way find library capable of converting between target , source formats. don't know of android/java audio conversion libraries, know of decent c library called sox has several java wrappers available (e.g., here , here). first of links (the guardian project) made android , includes both sox , ffmpeg. isn't particularly lightweight. second doesn't seem android, may need deal finding appropriate binary or building sox (hint: binaries @ guardian project in "res/raw"). have looked @ sox confirm supports opus format. side note, ffmpeg supports opus, well. once sox or ffmpeg , java wrapper hooked appropriately in project, should able use convert files. depending on use case, should pay attention licenses of libraries choose employ.
the hard way convert file write own codec. potentially in android java using mediacodec class, android mediaextractor doesn't understand opus (as far know) not straightforward endeavor. doubt want go down road.
Comments
Post a Comment