xamarin 尝试播放音频文件时出现致命信号11(SIGSEGV)

w8f9ii69  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在尝试使用Xamarin和C#创建一个适用于iOS和Android的莫尔斯翻译器。我尝试了各种方法,最后使用了Autofac,因为我需要引用主项目和Android。这是因为我的主项目中有接口

namespace SuperMorse
{
    public interface ISoundService
    {
        void PlayDotSound();
        void PlayDashSound();

        int dot { get; set; }
        int dash { get; set; }
    }
}

和Android项目中的类

namespace SuperMorse.Droid
{
    public class SoundService : ISoundService
    {
        public int dot { get; set; }
        public int dash { get; set; }

        public SoundService()
        {
            dot = Android.App.Application.Context.Resources.GetIdentifier("dot", "raw", Android.App.Application.Context.PackageName);
            dash = Android.App.Application.Context.Resources.GetIdentifier("dash", "raw", Android.App.Application.Context.PackageName);
        }
        public void PlayDotSound()
        {
            var fd = Android.App.Application.Context.Assets.OpenFd("dot.mp3");
            MediaPlayer dotPlayer = new MediaPlayer();
            dotPlayer.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            dotPlayer.Prepare();
            dotPlayer.Start();
        }
        public void PlayDashSound()
        {
            var fd = Android.App.Application.Context.Assets.OpenFd("dash.mp3");
            MediaPlayer dashPlayer = new MediaPlayer();
            dashPlayer.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            dashPlayer.Prepare();
            dashPlayer.Start();
        }

    }
}

所以当我在MainPage函数中调用PlaySounds函数时,我在终端中得到一个奇怪的错误:致命信号11(SIGSEGV),代码2(SEGV_ACCERR),tid 31167(名称.supermorse)中的故障地址0x 7 ffc 7ad 11 fb 8,pid 31167(名称.supermorse).我对其进行了调试,似乎错误发生在var fd = Android.App.Application.Context.Assets.OpenFd(“dot.mp3”)行时;从soundserviceidocs运行...或类似的一个破折号。有什么想法如何修复?
尝试阅读我的音频文件并更改其格式

li9yvcax

li9yvcax1#

我已经创建了一个新的示例来测试您的代码。当我运行该项目时,显示了dot.mp3
在页面.cs:

private void button_Clicked(object sender, EventArgs e)
{
   ISoundService soundService = DependencyService.Get<ISoundService>();
   soundService.PlayDotSound();
}

接口:

namespace AppTest
{
    public interface ISoundService
    {
        void PlayDotSound();
        void PlayDashSound();

        int dot { get; set; }
        int dash { get; set; }
    }
}

Android依赖项服务:

[assembly: Xamarin.Forms.Dependency(typeof(AppTest.Droid.SoundService))]
namespace AppTest.Droid
{
    public class SoundService : ISoundService
    {
        public int dot { get; set; }
        public int dash { get; set; }

        public SoundService()
        {
            dot = Android.App.Application.Context.Resources.GetIdentifier("dot", "raw", Android.App.Application.Context.PackageName);
            dash = Android.App.Application.Context.Resources.GetIdentifier("dash", "raw", Android.App.Application.Context.PackageName);
        }
        public void PlayDotSound()
        {
            var fd = Android.App.Application.Context.Assets.OpenFd("dot.mp3");
            MediaPlayer dotPlayer = new MediaPlayer();
            dotPlayer.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            dotPlayer.Prepare();
            dotPlayer.Start();
        }
        public void PlayDashSound()
        {
            var fd = Android.App.Application.Context.Assets.OpenFd("dash.mp3");
            MediaPlayer dashPlayer = new MediaPlayer();
            dashPlayer.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            dashPlayer.Prepare();
            dashPlayer.Start();
        }

    }
}

以及“资源”文件夹中的文件:

此外,dot.mp3的构建操作是AndroidAsset

相关问题