gpt4 book ai didi

android - Xamarin for Android:无法从外部文件夹播放mp3

转载 作者:行者123 更新时间:2023-12-03 01:16:41 24 4
gpt4 key购买 nike

我无法播放放置在应用程序外部存储的files文件夹中的mp3文件。

我在OnCreate()中请求了权限

    private const int EXTERNAL_READ_WRITE_REQUEST = 1337;

protected override async void OnCreate(Bundle savedInstanceState)
{
ActivityCompat.RequestPermissions(this, new string[]
{
Manifest.Permission.ReadExternalStorage,
Manifest.Permission.WriteExternalStorage
},
EXTERNAL_READ_WRITE_REQUEST);
}

然后,我确认已授予该权限:
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
if (requestCode == EXTERNAL_READ_WRITE_REQUEST)
{
if (grantResults.ToArray().ToList().IndexOf(Permission.Denied) == -1)
{
ShowMessage("Permissions granted");
}
else
ShowMessage("Permission request denied, unable to continue.");
}

Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

然后在服务(不是 Activity )中,检查权限,获取文件夹,获取文件,然后尝试使用MediaPlayer类播放mp3:
    MediaPlayer player;


public string GetSongDirectory()
{
string result = "";


List<Java.IO.File> files = GetExternalFilesDirs(null).ToList();

files = files.OrderBy(x => x.AbsolutePath.IndexOf("emulated")).ToList();

var f = files.DefaultIfEmpty(null).FirstOrDefault();

if (f != null)
{
result = f.AbsolutePath + "/songs";

if (Directory.Exists(result) == false)
Directory.CreateDirectory(result);
}

return result;
}


[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
string songFolder = GetSongDirectory();

var p = ContextCompat.CheckSelfPermission(this.ApplicationContext, Manifest.Permission.ReadExternalStorage);

if (p == Android.Content.PM.Permission.Granted)
{
try
{
foreach (string fileName in System.IO.Directory.GetFiles(songFolder, "*.*", SearchOption.AllDirectories))
{
player = new MediaPlayer();

JFile f = new JFile(fileName);

Java.IO.FileInputStream fs = new FileInputStream(f);

player.Reset();
//player.SetDataSource(fileName); this didn't work either
player.SetDataSource(fs.FD, 0L, f.Length());
player.Prepare();
player.Start();

break;
}
}
catch (Exception x)
{
x.ToString();
}
}


Toast.MakeText(this, "Greetings from serivce", ToastLength.Long).Show();

return StartCommandResult.Sticky;
}

但是,在调用Prepare时,它将引发异常:
{Java.IO.IOException: Prepare failed.: status=0x1
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod

fileName包含“/storage/1606-3C08/Android/data/com.companyname.mypod/files/songs/IshDARR-Alpha.mp3”

当它属于外部SD卡时,如何通过xamarin中的服务播放此mp3文件?

编辑:我能够使用BinaryReader读取文件的内容,因此它不是文件访问权限问题。

最佳答案

我在网上找到了答案,但是失去了原始作者,因此如何在xamarain上从外部sd卡播放mp3成为了一个难题:

    public class StreamMediaDataSource : MediaDataSource
{
System.IO.Stream data;

public StreamMediaDataSource(System.IO.Stream Data)
{
data = Data;
}

public override long Size
{
get
{
return data.Length;
}
}

public override int ReadAt(long position, byte[] buffer, int offset, int size)
{
data.Seek(position, System.IO.SeekOrigin.Begin);
return data.Read(buffer, offset, size);
}

public override void Close()
{
if (data != null)
{
data.Dispose();
data = null;
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (data != null)
{
data.Dispose();
data = null;
}
}
}

这是一个片段:
StreamReader sr = new StreamReader(fileName);
player.SetDataSource(new StreamMediaDataSource(sr.BaseStream));
player.Prepare();
player.Start();

我相信它失败的原因是因为它使用了读取文件的方法。

如果我尝试使用FileStream读取文件。即使它已通过用户提示通过它授予访问权限,也会抛出拒绝访问的异常。

如果有人需要通过Google搜索它,这是Java日志:
06-10 11:37:26.048 E/MediaPlayerNative(31944): error (1, -2147483648)
06-10 11:37:26.175 D/Mono (31944): DllImport searching in: '__Internal' ('(null)').
06-10 11:37:26.175 D/Mono (31944): Searching for 'java_interop_jnienv_call_object_method'.
06-10 11:37:26.175 D/Mono (31944): Probing 'java_interop_jnienv_call_object_method'.
06-10 11:37:26.176 D/Mono (31944): Found as 'java_interop_jnienv_call_object_method'.

关于android - Xamarin for Android:无法从外部文件夹播放mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62284025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com