简体   繁体   English

MediaPlayer类中的SetDataSource从资产或原始文件夹加载mp3文件

[英]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 . 我有从path播放mp3文件的代码。 I although don't know what the value of my filePath should be when I call StartPlayer(String filePath) . 我虽然不知道调用StartPlayer(String filePath)filePath的值是什么。

My audio files stored are in the assets and raw folders. 我存储的音频文件位于assetsraw文件夹中。 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 . 如果将媒体文件添加为资产,则需要使用AssetManager来获取AssetFileDescriptor An instance of the AssetManager is available by accessing the Assets property on an Android.App.Context , such as an Activity . 通过访问Android.App.Context上的Assets属性(例如Activity ,可以使用AssetManager的实例。 So in an Activity sub-class, you can do the following in your else clause: 因此,在“ Activity子类中,可以在else子句中执行以下操作:

 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. 但是我不得不说,由于您的else子句,您的音频不会第一次播放,当调用该方法时MediaPlayer为null时,该子句将不会运行。 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/ 有关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. 您可以同时使用assetsraw文件夹来存储将用.APK编译的音频文件。

But you should re-consider your strategy in regards to using a filePath as your parameter. 但是您应该重新考虑使用filePath作为参数的策略。 Instead, consider using a string fileName or an int resource . 相反,请考虑使用string fileNameint resource

To retrieve a file from the assets or raw folder cannot be done using a filePath in Android. assetsraw文件夹中检索文件无法使用Android中的filePath完成。 Instead, this is done by either using the AsssetManager or through a Resource as mentioned here . 相反,这是通过使用做AsssetManager或通过Resource提到这里

I have also optimised your code a little as the else clause isn't needed. 我还优化了您的代码,因为不需要else子句。

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. 当试图从访问文件assets的文件夹,您需要使用静态方法OpenFdthis.Assets (其中thisContext你的Activity与文件的名称)。 This will return an AssetFileDescriptor that you can use as a DataSource as follows: 这将返回一个AssetFileDescriptor ,您可以将其用作DataSource ,如下所示:

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 . 您还可以使用raw文件夹,尽管该文件夹要求您指向给定Resource的自动生成的id This is done using the static Create method of the MediaPlayer : 这是使用MediaPlayer的静态Create方法完成的:

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). 其中resource是指raw文件夹中的音频文件,可通过Resource.raw.youraudiofile (其中youraudiofileraw文件夹中的音频文件的名称)访问。

You can read more about using the raw folder in the Xamarin documentation . 您可以在Xamarin文档中阅读有关使用raw文件夹的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM