简体   繁体   中英

How to exclude sound clips from music library in Android app

I have had an Android music app in the play store since May of 2016. Currently I ask the user to specify a folder that has all of their music.

I use the following code to filter their folder for music files:

private boolean isAudioFile(String songTitleLowerCase) {
    return songTitleLowerCase.endsWith(".mp3") || songTitleLowerCase.endsWith(".wma") || songTitleLowerCase.endsWith(".wav") || songTitleLowerCase.endsWith(".m4a") || songTitleLowerCase.endsWith("" +
            ".flac");
    }

I have additional code that searches through all the subfolders as well.

This works OK, but has some limitations:

  1. Most users are likely having trouble locating the folder where their music is stored. I have some hints in the app as to common locations for where phones keep their music folders, but between all the phone models, and all the versions of Android, their are quite a few possibilities.

  2. If the user has two or more folders with music, than they either have to consolidate their music, or only have access to part of their music collection with my app. I have not yet implemented code for multiple music folder selection.

I think that most apps automatically find all the music files on the device, and I would like to do this. Previously I implemented this feature, and the problem was that I ended up with all the sound files (ie from Google Maps), as well as podcasts, and audiobooks mixed in.

So how can I exclude sound clips, podcasts, and audiobooks? Do I just do that by the extensions I choose to include in the code snipped above? I really am not sure what file formats are important to music listeners these days, and which file formats are just for sounds and podcasts, or if there is an overlap in uses. IE Are .wav files used for music files or only sound clips?

Thanks for your patience and understanding with my questions. I really appreciate your feedback, as it will help make my app tremendously more user friendly once it can automatically find all the music accurately.

There isn't a definite answer due to the nature of the question: I can record speech for the approximately same duration of a typical song using some high-quality equipment with some background music and there's no way to determine the type of audio file (you sure I wasn't singing?). If you're filtering files based solely on duration or size, do keep in mind that you'll make a lot of Dream Theater fans really angry.

So, first and foremost, extensions are unreliable. Use something like mime-util to extract format using extension and/or magic number (since website is offline and library doesn't seem to be actively developed anymore, refer to this SO question for usage instructions). I'm not sure what's the track record of MediaMetadataRetriever in that aspect, but I guess you could try it. You could also get something like Apache Tika but it seems quite heavy for your needs.

Next, filter out correct files. Anything with MIME audio/* is a safe bet. You might need to whitelist specific application/* types, but the only one I'm aware of is application/ogg and even in that case audio/ogg is preferred. In order to avoid common non-music formats, blacklist audio/audible , audio/vnd.audible.aax , audio/x-pn-audibleaudio (that'd be .aa, .aax and similar audiobook formats), audio/aac , audio/3gp . For more ideas, consult Wikipedia .

Here come the less reliable parts. You could assume that anything below 30s shouldn't be music (that sounds too short even for metalcore). Similar could be assumed for too long files: just make sure you pick a sane value like 90mins or so (no, 15min is not sane. And I don't even listen to Dream Theater).

There's more: you could hope your beloved user was kind enough to set metadata for his/her files. Use MediaMetadataRetriever to check for existence of values such as Album, AlbumArtist, Genre and such. Also check the bitrate - anything 80+ qualifies as music.

And the most unreliable of them all: check the size/duration ratio. Again, MediaMetadataRetriever is your friend. This would require extensive testing, but I can tell you from my collection that an average 4-4.5 minute song is a little less than 4MB when stored as quality 4 ogg file, but can grow up to 15MB when in 320kbps mp3. Be sure to check the type - lossless formats such as flac or wav are much much larger, so 5min flac song takes ~40MB. However, the upper limit would be of no interest to you, since audio recordings are usually of lower quality, so, say, if size (in MB)/duration (in min) < 0.5 (0.7 might work as well), it's definitely not music.

That's all I could think of at the moment. I have quite a collection of different song formats and file durations on my PC (but not audiobooks or recordings), so I guess I could grab you some more specific numbers if that's what you need.

Note: I've assumed you need to filter out songs from non-songs in general case. If you intend to play them using Android's media player, there's no need to bother with each and every obsolete audio format: check which types Android supports and filter according to that.

PS if you're looking into examples which will definitely break whichever sane way you're using to filter, just look into prog genres ( ambient works too ).

You can't know for sure if any audio file is what you expect it to be. An audio book or a recording might as well be an MP3, FLAC, OGG, WAV or any other sound format, depending on what program the publisher/recorder used.

WAV files are (usually) uncompressed audio files and can contain everything. It might be a music file (audiophiles only "listen" to WAV files because they don't like the sound of the compressed MP3s), but could also as well be a recording (my audio recorder for example offers to write output to a WAV file).

The only "recording-only" format I'm aware of is AMR (Adaptive Multi-Rate audio codec, audio compression format optimized for speech coding). As Chetan Joshi already pointed out, you could use some checks for file size. But be aware that those are not always reliable, because (for example) WAV files (= raw audio) are usually very large, even for short durations.

Your logic is more like whitelisting the audioFiles and should be actually the opposite, there are more more audio-music file extensions that audio book guides

so instead of trying to find a valid music extension try to find and audioguide book

those are only: .aa , .aax and .m4b

private boolean isUnwantedAudioFile(String songTitleLowerCase) {
    return songTitleLowerCase.endsWith(".aa") || songTitleLowerCase.endsWith(".aax") || songTitleLowerCase.endsWith(".m4b")  ;
    }

all other files that can pass that proxy are valid!

you can even think about making a list using colletions

here is a quite updated list of all possible files.

Android does all the searching for you and stores results in database accessible using MediaStore API .

Here is a very basic sample that will get you started.

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