How can I make it so that music (mp3 files) can be played from the external SD card, whether directly or indirectly?

Hi,

My name is Clement Caillat, and I'm currently developing an app that plays a random song when shaked. That part works fine, however I've add trouble trying to play music from the external SD card on my phone. After spending an entire night on it, I've concluded that the best way is to make a temporary copy of the song selected from the external SD card to the internal storage, because the "Player" function seems unable to play music files from the external SD card. I tried using the "Taifun File" extension to do so, but although it allows me to list mp3 files from my external SD card, there doesn't seem to be a block that will allow me to copy files from said external SD card. Or maybe I programmed it wrong. Below here is a screenshot of the relevant part of my code. By the way, my phone is a LG33L with Android 5.0.1.

Hello Clement.

The Player can play music files directly from an external SD card (the card some have a slot for) when using an Android 8.1. Are you using an Android 10 or 11; oh you have Android 5 which should work ok? . Developers have issues when handling files on Android 10 and 11. In my instance, Taifun's File extension works fine.

Have you tried reading the files on the external card without all the fancy code you use for the Player and shaking? Get your app to read the file (just print out a list of the files in your Music folder), then add the stuff that makes the app fun once you can determine that you can read the external card.

Thanks for the suggestion. I've tried what you told me, and it seems like my app is unable to read anything whatsoever on the external SD card. It does seems to find the external SD card when I do "call:name.AvailableStorageDirectories", but it systematically tries to get the list of files from the internal storage when I do "call:name.FileListAsync". So unless there's something I can do about that, I think I'm just going to fetch the songs from my Google Drive (since they're stored on there as well) even though it's not a great solution/remedy.

Try this APK on your device:

Does it work? I think so.

See also here:

Sure the Player (using Taifun's File extension) can read from both the external removable sdCard AND internal memory. You are not providing the appropriate code. Experiment and try again. :slight_smile:

Perhaps this graphic will help. The red dots are the internal, the green dots the removable sdCard on my Android 8.1 Tablet. Did this also on my 4.2 Android where the internal directory is /storage/emulated/0/Music/ and the external is /storage/external_SD/Music/ (Different manufacturer's hardware have different names for the external).

. You have to specifically tell your app which to choose. In these instances the removable card was the first item in the List provided by AvailableStorageDirectories.
You capture a List of your available music files using AfterFileListAsync

OK, you have a GoogleDrive solution. Be happy with it or learn how to use Taifun's File extension; the graphic may help.

@SteveJG This doesn't work:

@Clement_Caillat On Android devices with API > 28 (Android 10 and above) see here:

So use the FileTools extension from @vknow360 until @Taifun has fixed this issue.

1 Like

The example posted will work on Clement's Android 5 @Anke :slight_smile: Both methods can work. Let Clement decide what he wants to do for his Android 5.

No, there's no need to split anything here. In addition, TaifunFile.DirectoryList must begin with file:/// in order to be able to read / list from the removable SD card.

Is this what is bothering you Anke?

filestorage

Yes, Clement needs that Block too eventually. :slight_smile: If he already knows the path, correct...he does not need to 'split'. He does need to know what directories are available. He was confused about what information is returned using the AvailableStorageDirectories. I show him how to parse it.

to scan the complete internal sdcard for available jpg files please use the FileListAsync method, see also the documentation

FileList
Return a list of filenames of a given directory.
Use this method for specific directories to get back a result immediately. To get a list for a complete device or sdcard, which may take a few seconds, use the FileListAsync method instead.
The complete path to these files is provided. You can filter by extension. To get a list of all files, use * as extension. If a directory starts with file:/// you can specify a complete path to the directory. To include files of all subdirectories, set includeSubdirectories to true, else to false.

I just tested the FileList block on an Android 10 device (Samsung Galaxy A51) in the current App Inventor system, which builds against API 29 (Android 10) and did not get the runtime error. @Anke what did you modify to elicit that error? Or are you already using an Android 11 device for your tests?


btw. the AvailableStorageDirectories method already returns a list... just use the select list item block to get an item from the list...

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

@Clement_Caillat Here are the correct blocks:

If there are a lot of MP3s stored in the (internal or removable) SD card, you should use the .FileListAsync method, as it can take several seconds until the list is created.

On devices with API ≤ 28 (here: Galaxy Note8, Android 9 / API 28:

On devices with API ≥ 29 (here: Pixel 2XL, Android 11 / API 30):

includeSubdirectories = false

includeSubdirectories = false

It seems quite weird for me as I never saw/found that method returning an empty list.(I have used your extension for a long time)
Maybe it was my mistake.

It's not an empty list, but a
NullPointerException - Attempt to get length of null array
→ crash

What do you mean by that?

Sorry, that was for @Taifun

my tests with an Android 10 device have been fine, so only on Android 11 beta there is an issue... I will look into it... but currently this is not something to worry about... Android 11 still is not released...

it returns an empty list, if there is no such file found, for example a jpg file directly in the root directory without scanning the complete sdcard...

yes, for Android 11 beta
thank you for your tests!

Taifun

1 Like

Correction:

On devices with API ≥ 30 (Android 11) the TaifunFile extension doesn't list the files from the root directory of the external storage:

thank you...
as soon as Genmotion offers an Android 11 emulator I will continue to test and hopefully fix this issue
Taifun

You can try android studio emulator.
I want to use it but I don't have enough ram.
However, I have installed Oreo and it works fine with 1.5 GB ram.

1 Like