在哪里放置媒体资源并启动媒体播放器? [英] Where to put media resource and start media player?
问题描述
我想使用标准 Android 媒体播放器在 Xamarin 应用中播放歌曲.
我有两个问题无法回答:
- 我在这里放置媒体文件(.mp3 文件)或如何链接到该文件(它也保存在我的 SD 卡上)我尝试将其保存在原始文件夹中,但似乎没有上班
- 如何正确启动播放器?要么我的代码不起作用,要么播放器找不到该文件,因为它没有正确存储.
然而,这是我用来准备媒体播放器的方法:
受保护的 MediaPlayer 播放器;公共无效StartPlayer(){如果(玩家==空){播放器 = 新媒体播放器();player.SetDataSource("Ressources.raw.test");玩家准备();player.Start();text = "运行";} 别的 {player.Reset();player.SetDataSource("Ressources.raw.test");玩家准备();player.Start();}}
我尝试将其保存在原始文件夹中,但这似乎不起作用
您不能直接使用字符串Ressources.raw.test"作为DataSource
.如果您需要播放 Resource/Raw
文件夹中的歌曲,这里有两种解决方案.
解决方案 1:
您可以尝试使用 MediaPlayer.Create(Context context, int resid)方法:
<块引用>为给定资源 ID 创建 MediaPlayer 的便捷方法.成功后,prepare() 将已被调用,不得再次调用.
用途:
MediaPlayer player = MediaPlayer.Create(this, Resource.Raw.test);player.Start();
解决方案 2:
将您的 Resource/Raw/test
路径转换为 Android.Net.Uri
:
player = new MediaPlayer();Android.Net.Uri uri = Android.Net.Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.test);player.SetDataSource(this, uri2);玩家准备();player.Start();
<块引用>
我如何链接到文件(它也保存在我的 SD 卡上)
解决方案:
尝试使用 SetDataSource(Context context, Uri uri)方法,例如:
player = new MediaPlayer();//我的文件路径是Path == file:///storage/emulated/0/netease/cloudmusic/Music/love.MP3var uri = Android.Net.Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory + "/netease/cloudmusic/Music/love.MP3");player.SetDataSource(this, uri);玩家准备();player.Start();
I want to play a song in a Xamarin app using the standard android media player.
I've got two questions that i haven't been able to answer myself:
- Here do I put the media file (.mp3-file) or how do i link to the file (it's also saved on my SD-Card) I tried saving it in the raw-folder, but that doesn't seem to work
- How do I correctly launch the player? Either my code doesn't work or the player doesn't find the file because it isn't stored correctly.
However, that's the method I use to prepare the media player:
protected MediaPlayer player;
public void StartPlayer() {
if (player == null) {
player = new MediaPlayer();
player.SetDataSource("Ressources.raw.test");
player.Prepare();
player.Start();
text = "Running";
} else {
player.Reset();
player.SetDataSource("Ressources.raw.test");
player.Prepare();
player.Start();
}
}
I tried saving it in the raw-folder, but that doesn't seem to work
You can't directly use string "Ressources.raw.test" as the DataSource
. If you need play a song from Resource/Raw
folder, here is two solutions.
Solution 1:
You could try using MediaPlayer.Create(Context context, int resid) method:
Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.
Usage:
MediaPlayer player = MediaPlayer.Create(this, Resource.Raw.test);
player.Start();
Solution 2:
Convert your Resource/Raw/test
path to Android.Net.Uri
:
player = new MediaPlayer();
Android.Net.Uri uri = Android.Net.Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.test);
player.SetDataSource(this, uri2);
player.Prepare();
player.Start();
how do I link to the file (it's also saved on my SD-Card)
Solution:
Try using SetDataSource (Context context, Uri uri) method, for example:
player = new MediaPlayer();
//My file path is Path == file:///storage/emulated/0/netease/cloudmusic/Music/love.MP3
var uri = Android.Net.Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory + "/netease/cloudmusic/Music/love.MP3");
player.SetDataSource(this, uri);
player.Prepare();
player.Start();
这篇关于在哪里放置媒体资源并启动媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!