2

I have the code for playing a mp3 file from a path. I although don't know what the value of my filePath should be when I call StartPlayer(String filePath).

My audio files stored are in the assets and raw folders. But I don't know what the best place for storing the audio files are? Also, I am not sure how to access the path from either of the folders.

protected MediaPlayer player;

public void StartPlayer(String  filePath)
{
  if (player == null) {
    player = new MediaPlayer();
  } else {
    player.Reset();
    player.SetDataSource(filePath);
    player.Prepare();
    player.Start();
  }
}

Any help is appreciated.

Demitrian
  • 3,200
  • 1
  • 24
  • 41
Anunaki
  • 83
  • 1
  • 1
  • 9
  • Is it required for you to use a `string` as the parameter? Could your solution possibly work using the name of the file instead? – Demitrian Jan 04 '17 at 22:15

2 Answers2

1

If you add your media file as an asset, you need to use the AssetManager to get an AssetFileDescriptor. An instance of the AssetManager is available by accessing the Assets property on an Android.App.Context, such as an Activity. So in an Activity sub-class, you can do the following in your else clause:

 AssetFileDescriptor afd = Assets.OpenFd("filenameinAssetsfolder.mp3");
 player = new MediaPlayer();
 player.Reset();
 player.SetDataSource(afd.FileDescriptor);
 player.Prepare();
 player.Start();

However I do have to say that your audio will not play the first time due to your else clause, which will not run when the MediaPlayer is null when that method is called. Seems you should just do this:

 if (player == null)
 {
      player = new MediaPlayer();
 }
 AssetFileDescriptor afd = Assets.OpenFd("filenameinAssetsfolder.mp3");
 player.Reset();
 player.SetDataSource(afd.FileDescriptor);
 player.Prepare();
 player.Start();

For more info on the AssetManager: https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_6_-_using_android_assets/

jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44
  • This plays the last song in the Asset folder, I didn't have mentioned that I have multiple songs in the Asset folder. – Anunaki Jan 05 '17 at 07:49
  • I found the solution player.SetDataSource(afd.FileDescriptor, afd.StartOffSet, afd.Length); – Anunaki Jan 05 '17 at 08:01
  • Please accept an answer even if it helped you find the solution. In this case the answer did show you how to find files in the assets folder so you can work with them. – jgoldberger - MSFT Jan 06 '17 at 00:11
1

You can use both the assets and raw folder to store audio files which will be compiled with your .APK.

But you should re-consider your strategy in regards to using a filePath as your parameter. Instead, consider using a string fileName or an int resource.

To retrieve a file from the assets or raw folder cannot be done using a filePath in Android. Instead, this is done by either using the AsssetManager or through a Resource as mentioned here.

I have also optimised your code a little as the else clause isn't needed.

Assets folder

When trying to access a file from the assets folder, you need to use the static method OpenFd from this.Assets (where this is the Context of your Activity) with the name of the file. This will return an AssetFileDescriptor that you can use as a DataSource as follows:

protected MediaPlayer player;

public void StartPlayer(string fileName)
{
    if (player == null) 
        player = new MediaPlayer();

    var fileDescriptor = Assets.OpenFd(filename);

    player.Reset();
    player.SetDataSource(fileDescriptor.FileDescriptor);
    player.Prepare();
    player.Start();
}

Raw folder

You can also use the raw folder which although requires that you point to the auto-generated id of the given Resource. This is done using the static Create method of the MediaPlayer:

protected MediaPlayer player;

public void StartPlayer(int resource)
{
    if (player == null) 
        player = MediaPlayer.Create(this, resource);

    player.Reset();
    player.Prepare();
    player.Start();
}

Where resource refers to the audio file in the raw folder which can be accessed by Resource.raw.youraudiofile (where youraudiofile is the name of the audio fie in the raw folder).

You can read more about using the raw folder in the Xamarin documentation.

Community
  • 1
  • 1
Demitrian
  • 3,200
  • 1
  • 24
  • 41