Skip to content

Initialize and setup

zielony71 edited this page Oct 20, 2021 · 5 revisions

Installation

Add the NuGet package to all the projects you want to use it in.

  • In Visual Studio - Tools > NuGet Package Manager > Manage Packages for Solution
  • Select the Browse tab, search for MediaManager
  • Select Plugin.MediaManager
  • Install into each project within your solution

More information on the Xamarin Blog

Platform Support

Platform Supported Version Player
.Net Standard Yes 2.0+ MediaManager
Xamarin.Forms Yes 3.2+ MediaManager
Xamarin.Android Yes API 16+ ExoPlayer
Xamarin.iOS Yes iOS 10+ AVPlayer
Xamarin.Mac Yes 3.0+ AVPlayer
Xamarin.tvOS Yes 10.0+ AVPlayer
Tizen Yes 4.0+ MediaPlayer
Windows 10 UWP Yes 10+ MediaPlayer
Windows WPF Yes 4.7.1+ MediaPlayer

Usage

Call MediaManager.Current from any .Net library or Xamarin project to gain access to APIs.

IMPORTANT: Initialize plugin

Make sure to call Init() in all the native platforms on startup of your app.

CrossMediaManager.Current.Init();

Optionally provide the Activity on Android. This will also be used to bind the Android Service and will be used as Intent to launch from a notification.

public class MainActivity : AppCompatActivity
{
	protected override void OnCreate(Bundle savedInstanceState)
	{
		base.OnCreate(savedInstanceState);
		SetContentView(Resource.Layout.main_activity);

		CrossMediaManager.Current.Init(this);
	}
}

IMPORTANT

Android:

  • This library will automatically request the following permissions: AccessWifiState, AccessNetworkState, Internet, ForegroundService and WakeLock. You do not need to add them to your AndroidManifest.
  • Your app must target Android SDK v28 or higher
  • This library uses ExoPlayer for video playback. This requires that you enable the following
  • Dex tool to D8: <AndroidDexTool>d8</AndroidDexTool>
  • Optional enable R8 Linker to make code smaller: <AndroidLinkTool>r8</AndroidLinkTool>
  • Aapt2 build tools: <AndroidUseAapt2>true</AndroidUseAapt2>
  • Disable multi-dex when using D8 and R8 with AAPT2. Your code should be small enough with those.

iOS:

  • In order for the audio to contiunue to play in the background you have to add the 'Audio, Airplay and Picture in Picture Background mode' and 'Background fetch' to your Info.plist
<key>UIBackgroundModes</key>
<array>
	<string>audio</string>
	<string>fetch</string>
</array>
  • If you are playing audio from a http resource you have to take care of ATS. Optionally you can disable this for playing media. Add the following to your info.plist:
<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoadsInMedia</key>
	<true/>
</dict>

If you want to disable more you could add: NSAllowsLocalNetworking or even NSAllowsArbitraryLoads to disable all checks.

  • If you want to display a artwork/cover that is embedded into an MP3 file, make sure that you use ID3 v2.3 (not v2.4).

UWP:

  • In the Package.appxmanifest under capabilities you need to select: "Background Media Playback", "Internet"
  • Optionally add "Music Library" and "Videos Library" as well if you use that
  • The target minimum Windows 10 version in the project properties must be set to 10.0; Build: 17763 or higher. If not, the VS wuoldn't properly resolve a version of NuGet package.

Tizen:

  • You must request http://tizen.org/privilege/internet, http://tizen.org/privilege/mediastorage, and http://tizen.org/privilege/externalstorage privileges