Home > OS >  Fatal signal 11 (SIGSEGV) when try to play audio file
Fatal signal 11 (SIGSEGV) when try to play audio file

Time:01-16

I am trying to create a Morse translator for both IOS and Android using Xamarin and C#. I have tried all kinds of things and in the end used Autofac because I needed to reference main project to android and back. That is so because I have Interface in my main project

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

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

and a class in Android project

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();
        }

    }
}

so when I call the PlaySounds functions back in my MainPage function I get a strange error in my terminal: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7ffc7ad11fb8 in tid 31167 (name.supermorse), pid 31167 (name.supermorse). I debugged it and it seems the error occurs when the line var fd = Android.App.Application.Context.Assets.OpenFd("dot.mp3"); from the SoundService.cs runs.... or a similiar one for the dash. Any idea how to fix?

Tried reading my audio files and changing their format

CodePudding user response:

I have create a new sample to test your code. And the dot.mp3 was displayed when I run the project.

In the Page.cs:

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

The interface:

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

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

The android dependency service:

[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();
        }

    }
}

And the file in the Assets folder:

enter image description here

In addition, the build action of the dot.mp3 is AndroidAsset.

  • Related