Can Android MediaPlayer play audio in an archived file?

CODE EDITED:

I am developing a dictionary for Android. I succeeded in having the app pronounce every word viewed. Here is the code:

btnPronounce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Log.i(MAIN_TAG,"Start pronounciation ..."); btnPronounce.setEnabled(false); String currentWord = edWord.getText().toString().toLowerCase(); try { ZipFile zip = new ZipFile("/sdcard/app_folder/sound/zip_test.zip"); ZipEntry entry = zip.getEntry(currentWord); if (entry != null) { InputStream in = zip.getInputStream(entry); // see Note #3. File tempFile = File.createTempFile("_AUDIO_", ".wav"); FileOutputStream out = new FileOutputStream(tempFile); IOUtils.copy(in, out); // do something with tempFile (like play it) File f = tempFile; try { if (f.exists()) { Log.i(MAIN_TAG,"Audio file found!"); MediaPlayer mp = new MediaPlayer(); mp.prepare(); mp.setLooping(false); mp.start(); while (mp.getCurrentPosition() < mp.getDuration()); mp.stop(); mp.release(); Log.i(MAIN_TAG,"Pronounciation finished!"); } else { Log.i(MAIN_TAG,"File doesn't exist!!"); } } catch (IOException e) { Log.i(MAIN_TAG,e.toString()); } btnPronounce.setEnabled(true); } else { // no such entry in the zip } } catch (IOException e) { // handle your exception cases... e.printStackTrace(); } } }); 

But the problem is that there are too many WAV files in one folder, and all these files are treated as music files on Android devices. As a result, it takes age to index such files. In addition, indexing and viewing users sometimes cause the application to crash.

So I'm just wondering if you can programmatically do the following:

  • Can Android MediaPlayer play WAV / MP3 files archived or packaged into a single file? I mean, I want to pin or wrap the audio files (or do something similar) so that they appear as one single file on Android devices, but MediaPlayer can play every single WAV file inside.
  • If this is not possible, can you suggest a solution to the problem?

EDIT: Are there other ways / solutions that allow audio files to just fit into one large file (image, zip or the like ...) and then let MediaPlayer read the individual files in it?

Thank you very much.

+4
source share
2 answers

You can use a combination of ZipFile or ZipInputStream and java.io to read the necessary data from zip, create temporary files and play them using MediaPlayer .

Alternatively, you can simply use the TTS engine and not skip the APK with 50 million bytes.

Edit - Example on request:

 try { ZipFile zip = new ZipFile("someZipFile.zip"); ZipEntry entry = zip.getEntry(fileName); if (entry != null) { InputStream in = zip.getInputStream(entry); // see Note #3. File tempFile = File.createTempFile("_AUDIO_", ".wav"); FileOutputStream out = new FileOutputStream(tempFile); IOUtils.copy(in, out); // do something with tempFile (like play it) } else { // no such entry in the zip } } catch (IOException e) { // handle your exception cases... e.printStackTrace(); } 

Notes:

  • I have not used any safe file processing methods here. It is for you.

  • This is not a way to do this, only a way . There are probably 100 more ways, some of which may be better suited to what you need. I did not use ZipInputStream simply because there was a bit more logic, and I was going for brevity. You have to check each entry to see what it is looking for with ZipInputStream , while ZipFile allows you to simply query what you want by name. I'm not sure if (if any) performance implications using any other.

  • It is by no means required to use temporary files (or files in general, actually), but Android MediaPlayer does not really like streams, so this is probably the easiest solution.

+6
source

An alternative that you should consider is downloading individual audio files when the user wants to listen to the pronunciation. This should reduce the file size, although this means that you cannot listen to the pronunciation when there is no Internet.

0
source

Source: https://habr.com/ru/post/1391522/


All Articles