[英]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. 我存储的音频文件位于
assets
和raw
文件夹中。 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. 您可以同时使用
assets
和raw
文件夹来存储将用.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 fileName
或int resource
。
To retrieve a file from the assets
or raw
folder cannot be done using a filePath
in Android. 从
assets
或raw
文件夹中检索文件无法使用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
子句。
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
的文件夹,您需要使用静态方法OpenFd
从this.Assets
(其中this
是Context
你的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();
}
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
(其中youraudiofile
是raw
文件夹中的音频文件的名称)访问。
You can read more about using the raw
folder in the Xamarin documentation . 您可以在Xamarin文档中阅读有关使用
raw
文件夹的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.