0

I am creating an MP3 Player app. It's mostly working correctly but there is one edge case.

If a user has a file named "Bieber.mp3" but changes the name of the file to "Bieber" without the file extension in the files app (on Android), they will still be able to play the song properly from the files app depending on the device (older devices like Nexus running on marshmallow will do it while newer devices like the pixel on Android 12 will say it's an invalid file type).

However, when the MP3 file is emailed (or uploaded somewhere in the cloud like drive or Dropbox), the file will have an unknown file type when attempting to from there

I want to know, how can MP3 files that are renamed to exclude the .mp3 extension still play in the android files app? Is there any way I can check to see if a file without the .mp3 extension is still an audio/mp3 file in Java for my mp3 player?

If mp3 files without the mp3 extension can play in the android files app, surely there should be some way to play/access them in the MP3 player I'm making, right?

  • Are you querying the `MediaStore` to get the audio content to play? If so, does the renamed piece of media show up in your `MediaStore` query? – CommonsWare Jan 02 '22 at 12:40
  • "Is there any way I can check to see if a file without the .mp3 extension is still an audio/mp3 file" see https://stackoverflow.com/questions/11360286/detect-if-a-file-is-an-mp3-file – Ma3x Jan 02 '22 at 13:04

1 Answers1

0

Well, Android has this library/class called MediaPlayer:

https://developer.android.com/guide/topics/media/mediaplayer

The MediaPlayer is what you use when you play audio,

the MediaPlayer takes the input(the file) as long as it is still encoded as an audio file.

No matter what you do with the file extension, as long as it is still encoded as audio,

it will stay as an audio file.

That's why~

JumperBot_
  • 551
  • 1
  • 6
  • 19
  • Thank you, this was helpful. My follow up question is - how do I check to see if the input is encoded as an audio file? In my Mp3 player app, I'm reading in MP3 files from the download folder. Sure, there could be MP3 files that the user downloaded but also PDF files and photos, etc. How can I ensure that files without extensions are in fact encoded as an audio file and not some other type of file? I don't want to read in a file called "Bieber" that is actually a PDF and not a mp3 and then get errors when running the app. – Yourmomcodes Jan 03 '22 at 00:36
  • @Yourmomcodes What I know is, 1. MediaPlayer cannot identify what's music and what's not, It just plays what it can play 2. You can use an if statement and compare if the file has a ".mp3" extension with it or if it is just blank (which can be an executable file) 3. It's not easy to know which is encoded and which isn't since there are far too many encodings for audio files. – JumperBot_ Jan 03 '22 at 00:54
  • @Yourmomcodes if you want help with option no. 2 above... you can simply get the file name from the directory given from the user and then do something like this: `while(dir.contains("/")){dir=dir.substring(indexOf("/"), dir.length());}` and then use an if statement like this: `if(dir.contains(".mp3")){/*play music*/}` – JumperBot_ Jan 03 '22 at 06:32