2

Based on the answer of this question and this example I've implemented a Visualizer using Xamarin.

myVisualizer = new Visualizer(0);
myVisualizer.SetEnabled(false);
myVisualizer.SetCaptureSize(Visualizer.GetCaptureSizeRange()[1]);
myVisualizer.SetDataCaptureListener(new VisualizerCapturer(), Visualizer.MaxCaptureRate, true, false);

The code works until I call myVisualizer.SetDataCaptureListener(...) and the exception

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Specified cast is not valid. at Android.Media.Audiofx.Visualizer.SetDataCaptureListener (Android.Media.Audiofx.Visualizer+IOnDataCaptureListener listener, System.Int32 rate, System.Boolean waveform, System.Boolean fft) [0x0000b] in /Users/builder/data/lanes/4009/9578cdcd/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Media.Audiofx.Visualizer.cs:811

gets thrown. My IOnDataCaptureListener implementation is without any code (I've made breakpoints inside the methods, no method gets called before the exception gets thrown)

public class VisualizerCapturer : Visualizer.IOnDataCaptureListener
{
    public IntPtr Handle
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void OnFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate)
    {
        throw new NotImplementedException();
    }

    public void OnWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate)
    {
        throw new NotImplementedException();
    }
}

I have absolutely no idea whats my problem and I hope anybody can help me with my problem.

Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49

1 Answers1

3

Your implementation of Visualizer.IOnDataCaptureListener does not inherit from Java.Lang.Object thus it is a plain C# class and not an Android Callable Wrapper (ACW) based class and thus your VisualizerCapturer object is not valid to be passed between the C# and Java runtimes.

Inherit your VisualizerCapturer from Java.Lang.Object and implement the Visualizer.IOnDataCaptureListener methods and you will be fine:

public class VisualizerCapturer : Java.Lang.Object, Visualizer.IOnDataCaptureListener
{
 ~~~~
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165