Android, Xamarin: 从SD卡上的某个地方获取mp3并转换成URI来播放
Android, Xamarin: Get mp3 from certain place on SD card and convert into URI to play
我目前正在开发一个播放音乐的小应用程序。为了测试我的应用程序,我想播放存储在我的 SD 卡上的 mp3:
SD-Card/Music/MYFILE.mp3
这需要适合这里:
private void InitPlayer() {
mediaPlayer = MediaPlayer.Create(this, "URI TO MY FILE");
}
所以我基本上需要将 uri link 获取到我的文件。我还没有找到一种方法来实现它。有人可以帮助我吗?
谢谢 :)
我在 Xamarin 论坛上找到了包含以下方法的 post。
https://forums.xamarin.com/discussion/103478/how-to-get-path-to-external-sd-card
public static string GetBaseFolderPath( bool getSDPath = false )
{
string baseFolderPath = "";
try
{
Context context = Application.Context;
Java.IO.File[] dirs = context.GetExternalFilesDirs( null );
foreach ( Java.IO.File folder in dirs )
{
bool IsRemovable = Android.OS.Environment.InvokeIsExternalStorageRemovable( folder );
bool IsEmulated = Android.OS.Environment.InvokeIsExternalStorageEmulated( folder );
if ( getSDPath ? IsRemovable && !IsEmulated : !IsRemovable && IsEmulated )
baseFolderPath = folder.Path;
}
}
catch ( Exception ex )
{
Console.WriteLine( "GetBaseFolderPath caused the follwing exception: {0}", ex );
}
return baseFolderPath;
}
获得 basefolderPath
后,您可以通过将此添加到地址(basefolderPath-string)来访问您的音乐文件夹和文件。
例如
baseFolderPath += "/YOUR_MUSIC_FOLDER/YOUR_MUSIC_FILE"
添加到您的第二个问题
要单独进入存储文件夹,请使用以下方法:
Android.OS.Environment.getExternalStorageDirectory()
在使用此方法之前,您可能需要在清单中添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我目前正在开发一个播放音乐的小应用程序。为了测试我的应用程序,我想播放存储在我的 SD 卡上的 mp3:
SD-Card/Music/MYFILE.mp3
这需要适合这里:
private void InitPlayer() {
mediaPlayer = MediaPlayer.Create(this, "URI TO MY FILE");
}
所以我基本上需要将 uri link 获取到我的文件。我还没有找到一种方法来实现它。有人可以帮助我吗?
谢谢 :)
我在 Xamarin 论坛上找到了包含以下方法的 post。
https://forums.xamarin.com/discussion/103478/how-to-get-path-to-external-sd-card
public static string GetBaseFolderPath( bool getSDPath = false )
{
string baseFolderPath = "";
try
{
Context context = Application.Context;
Java.IO.File[] dirs = context.GetExternalFilesDirs( null );
foreach ( Java.IO.File folder in dirs )
{
bool IsRemovable = Android.OS.Environment.InvokeIsExternalStorageRemovable( folder );
bool IsEmulated = Android.OS.Environment.InvokeIsExternalStorageEmulated( folder );
if ( getSDPath ? IsRemovable && !IsEmulated : !IsRemovable && IsEmulated )
baseFolderPath = folder.Path;
}
}
catch ( Exception ex )
{
Console.WriteLine( "GetBaseFolderPath caused the follwing exception: {0}", ex );
}
return baseFolderPath;
}
获得 basefolderPath
后,您可以通过将此添加到地址(basefolderPath-string)来访问您的音乐文件夹和文件。
例如
baseFolderPath += "/YOUR_MUSIC_FOLDER/YOUR_MUSIC_FILE"
添加到您的第二个问题
要单独进入存储文件夹,请使用以下方法:
Android.OS.Environment.getExternalStorageDirectory()
在使用此方法之前,您可能需要在清单中添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />