Big T Larrity
Big T Larrity

Reputation: 221

Getting a List of all filepaths for Audio Files in a certain directory

I am trying to build my own Music Player android app.

I want it to be very simple, just find all valid music files (perhaps I set file types such as mp3, m4a, wma etc for it to look for?) and play them in order of how they are found.

So far I have it so that I can find the default music folder path using

string musicDirectoryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).ToString();

What I now need is to check every file (and folder) inside that folder to see if it is mp3/m4a etc and then add them to the list. I've been messing and trying some foreach loops but cannot get anything useful.

I am very confused and quite new to Android development.

As you will see, if I specify a song that I know is on my phone in the default directory (*/NewPipe/Intergalatic - Beastie.m4a), then the music is playing.

So, how can I get a List of all those strings such as the "NewPipe/Beastie.." etc (along with the other 500+ folders inside my default music directory?

Here is my entire class so far (it's the only class in the project):

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Android.Media;
using Xamarin.Android;
using System.IO;
using System.Collections.Generic;
using Android.Util;

namespace Music_Player_v001
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    protected MediaPlayer mediaPlayer;
    List<string> filepaths_audio = new List<string>();

    public void StartMediaPlayer(String filePath)
    {
        mediaPlayer.Reset();
        mediaPlayer.SetDataSource(filePath);
        mediaPlayer.Prepare();
        mediaPlayer.Start();
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);

        FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
        fab.Click += FabOnClick;

        string musicDirectoryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).ToString();
        Log.Info("FILEPATHS: ", "music directory path is: " + musicDirectoryPath);

        mediaPlayer = new MediaPlayer();
        StartMediaPlayer(musicDirectoryPath + "/NewPipe/Intergalactic - Beastie Boys (HD).m4a");

        //foreach(AudioTrack track in Android.OS.Environment.DirectoryMusic)
        //{
        //    Log.Info("BULLSHIT TAG","track count =======================" + tracks);
        //    tracks++;

        //}
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.menu_main, menu);
        return true;
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        int id = item.ItemId;
        if (id == Resource.Id.action_settings)
        {
            return true;
        }

        return base.OnOptionsItemSelected(item);
    }

    private void FabOnClick(object sender, EventArgs eventArgs)
    {
        View view = (View) sender;
        Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
            .SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
    }


}
}

Additional

I also tried this:

 foreach (File f in Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic))
        {


        }

Upvotes: 0

Views: 1692

Answers (1)

nbura
nbura

Reputation: 388

It sounds like your main issue is recursively iterating through all the files in the directory and its subdirectories. Here are a few relevant links:

How to recursively list all the files in a directory in C#?

Best way to iterate folders and subfolders

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree

The easiest approach is probably Directory.EnumerateFiles, used like this: Directory.EnumerateFiles(musicDirectoryPath, "*", SearchOption.AllDirectories) to ensure that it also enumerates through the sub-directories.

foreach (string musicFilePath in Directory.EnumerateFiles(musicDirectoryPath, "*", SearchOption.AllDirectories)) {
        Log.Info("Music File", "Music file path is " + musicFilePath);
        // do whatever you want to with the file here
}

Note that this may fail if there is an access-restricted directory (or some other error) somewhere in the tree. If you'd like your solution to work around this, see the links I posted above, particularly the bottom one.

Also note, you can change that "*" (wildcard) in the method call to "*.mp3" or "*.wav" or so on, to only match files with those extensions. However, as the method doesn't support regex, I don't think there's any way to match multiple file extensions (like BOTH mp3 and wav). I think you'd have to use the "*" and then just check within the foreach if the file is a valid music file.

Upvotes: 1

Related Questions