0

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?

Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104
  • 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 Mar 06 '15 at 11:07
  • 1
    try adding it as an embedded resource. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/#Embedded_Images – Asanka Indrajith Mar 06 '15 at 12:20
  • Possible duplicate of [Write device platform specific code in Xamarin.Forms](http://stackoverflow.com/questions/24251020/write-device-platform-specific-code-in-xamarin-forms) – ruffin Oct 16 '15 at 04:15

1 Answers1

1

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.

Community
  • 1
  • 1
Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104