Plugin.MediaManager
1.2.2
Requires NuGet 2.14 or higher.
dotnet add package Plugin.MediaManager --version 1.2.2
NuGet\Install-Package Plugin.MediaManager -Version 1.2.2
<PackageReference Include="Plugin.MediaManager" Version="1.2.2" />
paket add Plugin.MediaManager --version 1.2.2
#r "nuget: Plugin.MediaManager, 1.2.2"
// Install Plugin.MediaManager as a Cake Addin #addin nuget:?package=Plugin.MediaManager&version=1.2.2 // Install Plugin.MediaManager as a Cake Tool #tool nuget:?package=Plugin.MediaManager&version=1.2.2
MediaManager - Cross platform media plugin for Xamarin and Windows
- Designed to be simple and easy to use
- Native playback of media files from remote http(s), embedded and local sources
- Native media notifications and remote controls
- Queue and playback management by default
- Playback status (Playing, Buffering, Loading, Paused, Progress)
- Events for media handling to hook into
Status:
Support
- Feel free to open an issue. Make sure to use one of the templates!
- Commercial support is available. Integration with your app or services, samples, feature request, etc. Email: hello@baseflow.com
- Powered by: baseflow.com
Wiki
More documenatation and information is available on the Wiki
Blogs
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
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 (.NET Framework) | Yes | 4.7.2+ | MediaPlayer |
Windows WPF (.NET Core) | Yes | 3.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);
}
}
When tapping the notification, it will launch your activity with an intent. In most cases, you probably want to set LaunchMode
on your Activity
to SingleTop
.
That should bring your app back into focus when tapped. You can read more about it here
[Activity(LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : AppCompatActivity
If you want to handle when the app is opened via a notification tap, you can override OnNewIntent
on your activity:
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
// TODO: Handle app opened from notification tap.
}
Disposing
The player can be disposed via CrossMediaManager.Current.Dispose()
. Make sure to call CrossMediaManager.Current.Init()
if you used dispose before playing another media file.
Play a single media item
//Audio
await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
//Video
await CrossMediaManager.Current.Play("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
Play multiple media items
public IList<string> Mp3UrlList => new[]{
"https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3",
"https://ia800605.us.archive.org/32/items/Mp3Playlist_555/CelineDion-IfICould.mp3",
"https://ia800605.us.archive.org/32/items/Mp3Playlist_555/Daughtry-Homeacoustic.mp3",
"https://storage.googleapis.com/uamp/The_Kyoto_Connection_-_Wake_Up/01_-_Intro_-_The_Way_Of_Waking_Up_feat_Alan_Watts.mp3",
"https://aphid.fireside.fm/d/1437767933/02d84890-e58d-43eb-ab4c-26bcc8524289/d9b38b7f-5ede-4ca7-a5d6-a18d5605aba1.mp3"
};
await CrossMediaManager.Current.Play(Mp3UrlList);
Other play possibilities
Task<IMediaItem> Play(IMediaItem mediaItem);
Task<IMediaItem> Play(string uri);
Task<IMediaItem> Play(IEnumerable<IMediaItem> items);
Task<IMediaItem> Play(IEnumerable<string> items);
Task<IMediaItem> Play(FileInfo file);
Task<IMediaItem> Play(DirectoryInfo directoryInfo);
Task<IMediaItem> PlayFromAssembly(string resourceName, Assembly assembly = null);
Task<IMediaItem> PlayFromResource(string resourceName);
- Playing from a
File
can be done for example by using theFile
andDirectory
api's. You download a file from the internet and save it somewhere using these .NET api's. - When playing from
Assembly
you need to add a media file to a assembly and set the build action toEmbedded resource
. - When playing from a
Resource
you should add your media file for example to theAssets
orraw
folder on Android, and theResources
folder on iOS.
For example:
await CrossMediaManager.Current.PlayFromAssembly("somefile.mp3", typeof(BaseViewModel).Assembly);
await CrossMediaManager.Current.PlayFromResource("assets:///somefile.mp3");
await CrossMediaManager.Android.PlayFromResource(Resource.Raw.somefile.ToString());
Control the player
await CrossMediaManager.Current.Play();
await CrossMediaManager.Current.Pause();
await CrossMediaManager.Current.PlayPause();
await CrossMediaManager.Current.Stop();
await CrossMediaManager.Current.StepForward();
await CrossMediaManager.Current.StepBackward();
await CrossMediaManager.Current.SeekToStart();
await CrossMediaManager.Current.SeekTo(TimeSpan position);
Control the Queue
await CrossMediaManager.Current.PlayPrevious();
await CrossMediaManager.Current.PlayNext();
await CrossMediaManager.Current.PlayPreviousOrSeekToStart();
await CrossMediaManager.Current.PlayQueueItem(IMediaItem mediaItem);
await CrossMediaManager.Current.PlayQueueItem(int index);
Extensions:
void ToggleRepeat();
void ToggleShuffle();
Retrieve and set information
IDictionary<string, string> RequestHeaders { get; set; }
TimeSpan StepSizeForward { get; set; }
TimeSpan StepSizeBackward { get; set; }
MediaPlayerState State { get; }
TimeSpan Position { get; }
TimeSpan Duration { get; }
TimeSpan Buffered { get; }
float Speed { get; set; }
RepeatMode RepeatMode { get; set; }
ShuffleMode ShuffleMode { get; set; }
bool ClearQueueOnPlay { get; set; }
bool AutoPlay { get; set; }
bool KeepScreenOn { get; set; }
Extensions:
bool IsPlaying();
bool IsBuffering();
bool IsPrepared();
bool IsStopped();
Properties available on CrossMediaManager.Current.MediaPlayer.*
IVideoView VideoView { get; set; }
bool AutoAttachVideoView { get; set; }
VideoAspectMode VideoAspect { get; set; }
bool ShowPlaybackControls { get; set; }
int VideoHeight { get; }
int VideoWidth { get; }
Hook into events
event StateChangedEventHandler StateChanged;
event BufferedChangedEventHandler BufferedChanged;
event PositionChangedEventHandler PositionChanged;
event MediaItemFinishedEventHandler MediaItemFinished;
event MediaItemChangedEventHandler MediaItemChanged;
event MediaItemFailedEventHandler MediaItemFailed;
Retrieve metadata for media
Depending on the platform and the media item metadata will be extracted from ID3 data in the file.
CrossMediaManager.Current.Queue.Current.Title;
CrossMediaManager.Current.Queue.Current.AlbumArt;
CrossMediaManager.Current.Queue.Current.*
Since the metadata might not be available immediately you can subscribe for updates like this:
var mediaItem = await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
mediaItem.MetadataUpdated += (sender, args) => {
var title = args.MediaItem.Title;
};
Alternatively you could also use the PropertyChanged
event to see updates to the metadata.
You can also get a single frame from a video:
string url = "https://something.com/something.mov";
var mediaItem = await CrossMediaManager.Current.Extractor.CreateMediaItem(url);
var image = await CrossMediaManager.Current.Extractor.GetVideoFrame(mediaItem, TimeSpan.FromSeconds(1));
ImageSource imageSource = image.ToImageSource();
FormsImage.Source = imageSource;
Add Video Player to the UI
The video view will automatically be attached to the player. If you have multiple video views and you want to hook it up yourself do:
CrossMediaManager.Current.MediaPlayer.AutoAttachVideoView = false;
After that you can manually add the video view like this:
For android we need a VideoView
in the axml layout.
<mediamanager.platforms.android.video.VideoView
android:id="@+id/your_videoview"
android:layout_width="match_parent"
android:layout_height="300dp" />
Then find the view in code:
playerView = view.FindViewById<VideoView>(Resource.Id.your_videoview);
For iOS, MacOS or tvOS we need to add a VideoView
either in code, or in a Xib or Storyboard.
var playerView = new VideoView();
View.AddSubview(playerView);
Then for all platforms we have to add the player view to the MediaPlayer
CrossMediaManager.Current.MediaPlayer.VideoView = playerView;
Play a non standard format like HLS, Dash or SS
MediaManager will try to make a guess which media type or format is used. Sometimes this will not be picked up or be wrong, but you can enforce it by setting it yourself like this:
var item = await CrossMediaManager.Current.Extractor.CreateMediaItem("https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8");
item.MediaType = MediaType.Hls;
await CrossMediaManager.Current.Play(item);
By enforcing it there is still no guarantee that the native system actually is able to play the item.
Platform specific features
Feature | Android | iOS, Mac, tvOS | UWP | Tizen | WPF |
---|---|---|---|---|---|
Audio | ✓ | ✓ | ✓ | ✓ | ✓ |
Video | ✓ | ✓ | ✓ | ✓ | ✓ |
Queue | ✓ | ✓ | ✓ | ✓ | ✓ |
Notifications | ✓ | ✓ | ✓ | ✓ | ✓ |
Volume | ✓ | ✓ | ✓ | ✓ | ✓ |
Media Extraction | ✓ | ✓ | ✓ | ✓ | ✓ |
HLS | ✓ | ✓ | |||
DASH | ✓ | ||||
SmoothStreaming | ✓ | ||||
ChromeCast | ✓ | ||||
Airplay | ✓ | ||||
Xamarin.Forms | ✓ | ✓ | ✓ | ✓ |
You can also directly access the native platform implementation if you need it!
//Android
CrossMediaManager.Android.*
//iOS, MacOS or tvOS
CrossMediaManager.Apple.*
//UWP
CrossMediaManager.Windows.*
//Tizen
CrossMediaManager.Tizen.*
//WPF
CrossMediaManager.Wpf.*
Xamarin.Forms
Adding a VideoView
to a Page in Forms is easy as this:
<mm:VideoView VerticalOptions="FillAndExpand" Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" />
Your Xamarin.Forms page could look like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mm="clr-namespace:MediaManager.Forms;assembly=MediaManager.Forms"
x:Class="YourClassName" >
<ContentPage.Content>
<StackLayout>
<mm:VideoView VerticalOptions="FillAndExpand" Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ShowControls="False" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
You can even use the normal Play(object)
method and not set source. When you navigate to the view that contains the VideoView
, the player will automatically attach to the view.
If you want a Page that contains a player you can open the VideoPage
.
Navigation.PushAsync(new MediaManager.Forms.VideoPage());
Reactive extensions
Add the Reactive NuGet package to all the projects you want to use it in.
Usage:
CrossMediaManager.Current.Reactive().*
FFmpegMediaMetadataRetriever on Android
If you want to use FFmpegMediaMetadataRetriever on Android to extract the metadata you can set to use this extension like this:
CrossMediaManager.Android.Extractor = new FFmpegMediaExtractor();
Intercept share requests from the native platform or other apps
Android:
//Add code to the OnCreate(Bundle savedInstanceState) of your MainActivity
if(await CrossMediaManager.Android.PlayFromIntent(Intent))
{
//If true maybe do an action like opening a Player Page.
}
IMPORTANT
Android:
- This library will automatically request the following permissions:
AccessWifiState
,AccessNetworkState
,Internet
,ForegroundService
andWakeLock
. 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
Tizen:
- You must request
http://tizen.org/privilege/internet
,http://tizen.org/privilege/mediastorage
, andhttp://tizen.org/privilege/externalstorage
privileges
Building the source code
- On Windows you need Visual Studio 2019 with the latest Xamarin, .NET Core, UWP and Windows 10 SDK installed.
- On Visual Studio for Mac 2019 multi-target is not supported. Therefor you need to compile from command line on a Mac. Simple go to the folder where the source code is and run:
msbuild MediaManager.sln /t:rebuild
to make a release build run:msbuild MediaManager.sln /t:rebuild /p:Configuration=Release
. To restore your nuget packages run:msbuild MediaManager.sln /t:restore
.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-android33.0 is compatible. net7.0-ios was computed. net7.0-ios16.1 is compatible. net7.0-maccatalyst was computed. net7.0-maccatalyst16.1 is compatible. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net7.0-windows10.0.19041 is compatible. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- No dependencies.
-
net7.0-android33.0
- Xam.Plugins.Android.ExoPlayer (>= 2.18.11)
- Xam.Plugins.Android.ExoPlayer.MediaSession (>= 2.18.11)
-
net7.0-ios16.1
- No dependencies.
-
net7.0-maccatalyst16.1
- No dependencies.
-
net7.0-windows10.0.19041
- Microsoft.Windows.SDK.BuildTools (>= 10.0.22621.755)
- Microsoft.WindowsAppSDK (>= 1.2.221209.1)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Plugin.MediaManager:
Package | Downloads |
---|---|
Plugin.MediaManager.Forms
Cross platform .NET plugin to play and control Audio and Video. This adds .NET MAUI / Xamarin.Forms specific features |
|
Plugin.MediaManager.Reactive
Reactive extensions for MediaManager |
|
Plugin.MediaManager.ExoPlayer
ExoPlayer supports more advanced video and audio playback. This is based on the MediaManager plugin |
|
Plugin.MediaManager.AzureMediaServices
Cross platform .NET plugin to play and control Audio and Video |
|
Plugin.MediaManager.FFmpegMediaMetadataRetriever
Cross platform .NET plugin to play and control Audio and Video |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.2 | 37,820 | 3/3/2023 |
1.2.1 | 1,381 | 2/8/2023 |
1.2.0 | 1,686 | 1/12/2023 |
1.1.1 | 122,139 | 5/6/2022 |
1.1.0 | 23,336 | 1/31/2022 |
1.0.9 | 88,106 | 6/9/2021 |
1.0.8 | 86,696 | 1/6/2021 |
1.0.7 | 4,839 | 12/15/2020 |
1.0.6 | 7,142 | 12/8/2020 |
1.0.5 | 1,403 | 12/8/2020 |
1.0.4 | 11,743 | 10/21/2020 |
1.0.3 | 47,275 | 9/4/2020 |
1.0.1 | 3,517 | 8/20/2020 |
1.0.0 | 5,921 | 8/13/2020 |
0.9.9 | 3,322 | 8/11/2020 |
0.9.8-alpha3 | 1,913 | 4/30/2020 |
0.9.8-alpha2 | 1,334 | 4/24/2020 |
0.9.8-alpha1 | 974 | 4/22/2020 |
0.9.7 | 35,197 | 3/4/2020 |
0.9.6 | 48,485 | 1/13/2020 |
0.9.5 | 3,263 | 1/10/2020 |
0.9.4 | 1,880 | 1/9/2020 |
0.9.3 | 1,554 | 1/8/2020 |
0.9.2 | 1,743 | 1/8/2020 |
0.9.1 | 6,537 | 12/12/2019 |
0.9.0 | 8,383 | 12/12/2019 |
0.8.18 | 20,705 | 10/21/2019 |
0.8.17 | 3,597 | 10/17/2019 |
0.8.16 | 1,906 | 10/15/2019 |
0.8.15 | 1,580 | 10/14/2019 |
0.8.14 | 1,723 | 10/14/2019 |
0.8.13 | 1,410 | 10/14/2019 |
0.8.12 | 2,548 | 10/8/2019 |
0.8.11 | 3,093 | 9/26/2019 |
0.8.10 | 1,735 | 9/25/2019 |
0.8.9 | 3,565 | 9/9/2019 |
0.8.8 | 1,931 | 9/3/2019 |
0.8.7 | 4,843 | 8/13/2019 |
0.8.6 | 2,222 | 8/9/2019 |
0.8.5 | 1,758 | 8/6/2019 |
0.8.4 | 1,767 | 8/6/2019 |
0.8.3 | 1,625 | 8/4/2019 |
0.8.2 | 1,435 | 8/4/2019 |
0.8.1 | 1,390 | 8/4/2019 |
0.8.0 | 1,667 | 8/2/2019 |
0.7.9 | 3,077 | 8/1/2019 |
0.7.8 | 1,499 | 7/31/2019 |
0.7.7 | 2,575 | 7/27/2019 |
0.7.6 | 1,446 | 7/26/2019 |
0.7.5 | 1,459 | 7/26/2019 |
0.7.4 | 1,624 | 7/23/2019 |
0.7.3 | 2,769 | 7/16/2019 |
0.7.2 | 2,541 | 7/9/2019 |
0.7.1 | 1,626 | 7/4/2019 |
0.7.0 | 1,723 | 7/3/2019 |
0.6.9 | 4,177 | 7/2/2019 |
0.6.8 | 1,738 | 7/1/2019 |
0.6.7 | 4,859 | 6/7/2019 |
0.6.6 | 1,528 | 6/7/2019 |
0.6.5 | 1,792 | 6/6/2019 |
0.6.4 | 1,556 | 6/6/2019 |
0.6.3 | 1,796 | 6/3/2019 |
0.6.2 | 4,952 | 5/28/2019 |
0.6.1 | 1,792 | 5/23/2019 |
0.6.0 | 2,239 | 5/20/2019 |
0.5.9 | 1,607 | 5/19/2019 |
0.5.8 | 3,045 | 5/17/2019 |
0.5.7 | 1,487 | 5/16/2019 |
0.5.6 | 1,600 | 5/15/2019 |
0.5.5 | 2,938 | 5/2/2019 |
0.5.4 | 1,820 | 4/26/2019 |
0.5.3 | 2,836 | 4/15/2019 |
0.5.2 | 12,887 | 4/15/2019 |
0.5.1 | 1,613 | 4/13/2019 |
0.5.0 | 1,944 | 4/12/2019 |
0.4.5 | 75,887 | 10/10/2017 |
0.4.4 | 6,085 | 8/21/2017 |
0.4.3 | 7,566 | 7/28/2017 |
0.4.1 | 2,447 | 7/25/2017 |
0.4.0 | 24,631 | 3/17/2017 |
0.3.0 | 14,481 | 2/21/2017 |
0.1.3 | 2,119 | 12/28/2016 |
0.1.2 | 2,258 | 12/26/2016 |
0.1.1 | 2,091 | 11/30/2016 |
0.1.0 | 2,596 | 11/16/2016 |
0.1.0-beta3 | 1,617 | 11/6/2016 |
0.1.0-beta2 | 1,538 | 10/31/2016 |
0.1.0-beta1 | 1,318 | 10/30/2016 |
0.0.9 | 1,688 | 10/17/2016 |
0.0.8 | 1,616 | 10/13/2016 |
0.0.7 | 1,592 | 9/29/2016 |
0.0.6 | 1,835 | 9/26/2016 |
0.0.5 | 2,130 | 9/20/2016 |
0.0.4 | 1,893 | 9/7/2016 |
0.0.3 | 1,885 | 8/25/2016 |
0.0.1 | 1,907 | 8/11/2016 |
0.0.0.1 | 2,153 | 9/27/2016 |