0
votes

I do have a Xamarin Forms project where I want Android, iOS and WinPhone as targets. (Right now I only look at Android.)

I am having trouble playing Audio and think I will need to implement it differently on each device.

I started with adding this code in the app.cs of the shared project

#if __ANDROID__
    MediaPlayer _player;
    _player = MediaPlayer.Create(this, Resources.Raw.bear); // "Resources.Raw.bear");
    _player.Start();
#endif

but found that I cannot really reference the bear.wav, so I think I have to add the bear.wav for each project/platform and also handle the playing code separately. (See also http://developer.xamarin.com/recipes/android/media/audio/play_audio/ )

Unfortunately, I could not find a sample - or any documentation - on how to do this.

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/ is the closest I could find, but it seems to be highly focused on renderers.

What I thought I would do would be to define an abstract class in the shared project (some simple class, just with a void play method), and use it, and have the specific implementations in each platform-project. How to go about this?

1
As far as I know, your resources have to be added to each platform. You cannot add them to the shared project/pcl and share them.Johan

1 Answers

1
votes

It looks like Write device platform specific code in Xamarin.Forms answers the question - which is not so much a Xamarin Forms question as a Visual Studio shared project question and can be solved by creating an interface/abstract class and then implementing that for each platform and injecting the implementation.