在Xamarin.Android中,获取资源ID“ Android.Content.Res.Resources + NotFoundException:”时发生错误

时间:2019-03-13 09:43:47

标签: c# xamarin xamarin.android

我想从Asset文件夹中获取音频文件的id(int),但它给了我错误。

TRUE

以上代码行在android Service 中执行 它给出了错误,

错误日志:

  

03-13 14:03:50.047 W / ResourceType(13170):获取资源号0x00000000的值时没有包标识符   未处理的异常:   Android.Content.Res.Resources + NotFoundException:资源ID#0x0

此文件“ my_file.ogg”位于“ Asset”文件夹中,将构建操作设置为“ AndroidAsset”

2 个答案:

答案 0 :(得分:0)

可以使用AssetsManager来读取资产。尝试使用下面的代码片段从资产中读取文件。

Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd("File name");

更新后的答案

以下代码段将帮助直接从Asset文件夹播放媒体文件。

Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd(fullPath);
     if  (afd != null )
     {
         player.SetDataSource (afd.FileDescriptor, afd.StartOffset, afd.Length);
         player.Prepare ();
         player.Start ();
     }

答案 1 :(得分:0)

就我而言,我有一个Xamarin Forms项目。我在xaml页面所指的iOS项目中包含了一个资源,但是忘记了将其包含在Android项目中。

相关问题