简体   繁体   中英

SetDataSource in MediaPlayer class to load up an mp3 file from Assets or raw folder

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.

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/

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 .

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