Reputation: 567
I'm developing an app with xamarin.forms which hase to play some sound. For that is created an interface 'IAudioPlayer'
namespace ClassLibrary.AudioPlayer
{
public interface IAudioPlayer
{
void PlayAudio(string filename);
}
}
And in the droid project i've got the implementation like this:
using System;
using Android.Media;
using Xamarin.Forms;
[assembly: Dependency(typeof(myApp.Droid.AudioPlayer_Android))]
namespace myApp.Droid
{
public class AudioPlayer_Android : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, ClassLibrary.AudioPlayer.IAudioPlayer, AudioTrack.IOnPlaybackPositionUpdateListener
{
public void PlayAudio(string filename)
{
var player = new MediaPlayer();
var fd = global::Android.App.Application.Context.Assets.OpenFd(filename);
player.Prepared += (s, e) =>
{
player.Start();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
}
public void OnMarkerReached(AudioTrack track)
{
throw new NotImplementedException();
}
public void OnPeriodicNotification(AudioTrack track)
{
throw new NotImplementedException();
}
}
}
now when i call DependencyService.Get<ClassLibrary.AudioPlayer.IAudioPlayer>();
it breaks and says "Method 'Get' not found in type 'Xamarin.Forms.DependencyService'.
When im Using DependencyService.Get<>()
with another Type (Local DB implementation for example) it works fine.
Any Idea what i'm doing wrong here?
Upvotes: 2
Views: 2658
Reputation: 33048
Your implementation of IAudioPlayer
is an Android Activity
. Dependency service will try to create an instance of it - probably not a good idea. I cannot really explain the error you get, but you should create a concrete implementation which uses one interface only:
public class MyAndroidPlyer : IAudioPlayer
{}
The Forms dependency service only has pretty basic functionality (you can check out the source here)
If you need something more sophisticated, there are alternatives, like MvvmLight/SimpleIOC.
Upvotes: 5