阅读Xamarin IDE从SD卡.MP3文件 [英] Read .mp3 file from SD card by Xamarin IDE
问题描述
林建设媒体播放器的家庭作业,并使用Xamarin IDE。
在Xamarin MainActivity文件有外部的.cs。这意味着是C#文件,我有一些麻烦这一点。
我需要得到从SD卡的数据。但是,当我实现了,我有例外的setDataSource失败。
帮帮我吧。
Im building mediaplayer for homework and using Xamarin IDE. MainActivity file in Xamarin have extern .cs. This mean is C# file and I have some trouble with that. I need get data from SD card. But when i implemented that, i had exception "SetDataSource failed". Help me.
namespace PlayMusic
{
[Activity (Label = "PlayMusic", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button play = FindViewById<Button> (Resource.Id.play_btn);
//MediaPlayer mp = MediaPlayer.Create (this, Resource.Raw.anniver);
MediaPlayer mp = new MediaPlayer ();
var directory = Android.OS.Environment.ExternalStorageDirectory;
var fullFileName = Path.Combine(directory.ToString(), "anniver.mp3");
mp.SetDataSource (fullFileName);
play.Click += delegate {
try{
mp.Start();
}
catch(Exception e)
{
e.StackTrace.ToString();
}
};
}
}
}
结果fullFileName =/storage/sdcard0/anniver.mp3
但是,当IED去的setDataSource(..)语句,有异常IOException异常,失败的setDataSource。
我检查anniver.mp3。说的好。
The result fullFileName = "/storage/sdcard0/anniver.mp3" But when IED go to SetDataSource(..) statement, have exception IOException, and failed SetDataSource. I checked anniver.mp3. That good.
推荐答案
由于您使用 Xamarin
您可以使用 C#
的 Path.Combine
来得到你的文件,像这样:
Since you're using Xamarin
you can use c#
's Path.Combine
to get to your file like so:
var directory = Android.OS.Environment.ExternalStorageDirectory;
var fullFileName = Path.Combine(directory.ToString(), "1.mp3");
if (File.Exists(fullFileName))
{
//proceed
}
这篇关于阅读Xamarin IDE从SD卡.MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!