How to read&write m3u file format for an audio player application? (Juce Framework)

293 Views Asked by At

I am developing an audio player application using Juce Framework. The music file paths are stored in a string array and when playlist is saved as file, a txt file includes these paths is created.

I want to read & write m3u files and display mp3 tags such as song name, artist name, cover etc. So what do you suggest? Is there any example or library you would suggest to create and use m3u playlist files?

I examined an m3u file created by VLC Player. It seems like all the process can be done using string processing functions. The m3u file I have examined is below.

#EXTM3U
#EXTINF:244,RihannaVEVO - Rihanna - Diamonds
music.mp3

Is the constant and only format like the example above? What do you suggest to done this work?

1

There are 1 best solutions below

0
Theo On

here is an example. You will have to implement your own SAF read/write mechanisms but hopefully this gets you on your way

             String path = null;
        String line = null;
        OutputStream out = null;
        OutputStreamWriter myOutWriter = null;
        ContentResolver m3uresolver = context.getContentResolver();
        String projection[] = null;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
            projection = new String[]{
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.DURATION
            };
        } else {
            projection = new String[]{
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DATA
            };

        }

                 DocumentFile  playlist = pickedDir.findFile(m3uplaylist);
        boolean file_check=false;
        try {
            if (playlist!= null) {
                // append if exists
                file_check=true;
                out = context.getContentResolver().openOutputStream(playlist.getUri(),"wa");
            } else {
                playlist = pickedDir.createFile("text/" + extension, m3uplaylist);
                out = context.getContentResolver().openOutputStream(playlist.getUri());
            }
            myOutWriter = new OutputStreamWriter((out), Charset.forName("UTF-8").newEncoder());

        //   UTF-8
                //   The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text
                //  as ISO-8859-1 or CP1252 will display the characters  for this.
                if (utf_setting && !file_check) {
                    // write Byte Order Mark BOM
                    out.write(0xef);
                    out.write(0xbb);
                    out.write(0xbf);
                }


                int loop = mArray.size();
                int i = 0;
                boolean  prefsetting  = sharedPreferences.getBoolean(context.getString(R.string.m3usetting), false);
                boolean  ossettings  = sharedPreferences.getBoolean(context.getString(R.string.ossetting), false);
                String prefix = sharedPreferences.getString(context.getString(R.string.m3u_relative_setting),"./");
                do {
                    // for each of the audio_ids, get a cursor
                    Uri newuri = uris.get_audio_media_uri_with_appended_id(mArray.get(i));
                    Cursor crsor = m3uresolver.query(newuri, projection, null, null, null);
                    if (crsor != null && crsor.moveToFirst()) {
                        crsor.moveToFirst();
                        if (i == 0) {
                            // only set once
                            albumColumn = crsor
                                    .getColumnIndex(MediaStore.Audio.Playlists.Members.ALBUM);
                            trackColumn = crsor
                                    .getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE);
                            artistColumn = crsor
                                    .getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST);
                            durationColumn = crsor
                                    .getColumnIndex(MediaStore.Audio.Playlists.Members.DURATION);
                            if (Build.VERSION.SDK_INT != Build.VERSION_CODES.Q) {
                                pathColumn = crsor
                                        .getColumnIndex(MediaStore.Audio.Playlists.Members.DATA);
                            }
                            line = "#EXTM3U - " + m3uplaylist + "\r\n"; // first line
                            if(!file_check) {
                                // only apend for new
                                myOutWriter.append(line);
                            }
                        }


                        Uri nuri = null;
                        String part1 = null;



                        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
                            // no _DATA column so create our own
                            try{
                                nuri = MediaStore.getDocumentUri(context, newuri);
                                path = nuri.getPath();
                                part1 = path.substring(0, path.indexOf("/document/") + 10);
                                String replace_string = null;
                                if (part1.startsWith("/tree/primary:")) {
                                    ///tree/primary:/document/primary:Download/music1/Chicago XI/Chicago - Chicago XI - 11 - Little one.mp3
                                    part1 = path.substring(0, path.lastIndexOf("/primary:") + 9);
                                    replace_string = StorageUtils.getinternalSdCardPath() + "/";
                                } else {
                                    // /tree/1318-2F10:/document/1318-2F10:Music/music1/1977 Out Of The Blue/07-Electric Light Orchestra - Jungle.mp3
                                    replace_string = "/storage/";
                                }

                                if (prefsetting) {

                                    prefix = prefix + "/";
                                    path = path.replace(part1, prefix);
                                } else {
                                    path = path.replace(part1, replace_string);
                                    path = path.replace(":", "/");
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                i++;
                               return context.getString(R.string.give_access);
                            }
                        } else {
                            path = crsor.getString(pathColumn);
                            if (prefsetting)  {
                                path =  general.check_prefix(context, path, prefix);
                            }
                        }




                        line = "#EXTINF:" + crsor.getString(durationColumn) + ","
                                + crsor.getString(artistColumn) + " - "
                                + crsor.getString(albumColumn) + " - "
                                + crsor.getString(trackColumn) + "\r\n";

                        myOutWriter.append(line);
                        crsor.close(); // new cursor for each record  no longer needed




                        if (ossettings) {
                            path = path.replace("/", "\\");
                            //     Log.i(TAG,"Line = "+ line);
                        }

                        if (check_pa.checkifinstalled(context)) {
                            int rating = 0;
                            rating = pamp.getPowerampRatings(context, path);
                            line = "#EXT-X-RATING:" + rating;
                            myOutWriter.append(line + "\r\n");
                        }
                        myOutWriter.append(path + "\r\n");
                    }
                    i++;
                } while (i <= loop - 1);

                myOutWriter.close();
                out.close();
    } catch (FileNotFoundException e) {
        // catch when creating new file
            error = context.getString(R.string.error_create_file);
        e.printStackTrace();
    }


}
    return error;