Cross platform media player lib
based on ideas and code of martijn00 ( https://github.com/martijn00/XamarinMediaManager )
Youโll find the story behind this MediaPlayer lib here โฆ
Xmarin.Forms samples for the MediaPlayer โฆ
Simple Xamarin, UWP & WPF samples for the MediaPlayer โฆ
and for those interested some links about the tricks of this library and examples in the wiki section...
This is only a preview, so there are missing features. If you are kind and ask for them, perhaps we'll implement them. Otherwise, as is is opensource contribute. PR (pull requests) are wellcome.
12/01/2019 - The mediaplayer control isn't working well in Android emulator, whereas same features work on a physical device.
platform | sample | build | state |
---|---|---|---|
UWP | XF.UWP | 90 % audio | |
iOS | XF.iOS | 60 % audio | |
Android | XF.Android | 90 % audio | |
MacOS | XF.Mac | 60 % audio | |
WPF | XF.WPF | 70 % audio |
- Available on NuGet: https://www.nuget.org/packages/ZPFMediaPlayer
- For Xamarin.Forms install into your Main and Client projects.
A Xamarin.Forms sample showing how to use MediaPlayer is in this Git, but here are the basics ...
If you wish to access to streamning sites you should add folowing lines to your Info.plist
.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key> NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
Before using the MediaPlayer you should initialize it in the platform dependent project.
Start by adding the using at the header of each concerned file ...
using ZPF.Media;
for an Android project: MainActivity.cs: OnCreate:
...
MediaPlayer.Current.Init();
LoadApplication(new App());
for a UWP project: App.xaml.cs: OnLaunched:
...
MediaPlayer.Current.Init();
Xamarin.Forms.Forms.Init(e);
...
for a WPF project: MainWindow.xaml.cs: MainWindow():
...
ZPF.Media.MediaPlayer.Current.Init(this);
Forms.Init();
...
MediaPlayer.Current.Init() | UWP | iOS | Android | Mac | WPF |
---|---|---|---|---|---|
Mandatory | no | no | no | no | yes |
MediaPlayer.Current.Play("http://freesound.org/data/previews/273/273629_4068345-lq.mp3");
On WPF you should try .. catch
the Play
method for the moment:
[WPF] Fatal exception when playing special song
platform | nativ player |
---|---|
Android | Android.Media.MediaPlayer |
iOS | AVKit.AVPlayerViewController |
MacOS | AVKit.AVPlayerView |
UWP | Windows.Media.Playback.MediaPlayer |
WPF | System.Windows.Controls.MediaElement |
Android
Android.Media.MediaPlayer Player = (Android.Media.MediaPlayer)MediaPlayer.Current.Player;
// Set the low-level power management behavior for the MediaPlayer on Android:
Player.SetWakeMode(Application.Context, Android.OS.WakeLockFlags.Partial);
Remark: The Android Player type will probably change in the future to the ExoPlayer. So check the doc on each release.
UWP
Windows.Media.Playback.MediaPlayer Player = (Windows.Media.Playback.MediaPlayer)MediaPlayer.Current.Player;
WPF
System.Windows.Controls.MediaElement Player = (System.Windows.Controls.MediaElement)ZPF.Media.MediaPlayer.Current.Player;
Main page source code from sample program ...
MediaPlayer.Current.Pause();
MediaPlayer.Current.Play("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
MediaPlayer.Current.Play("http://freesound.org/data/previews/273/273629_4068345-lq.mp3");
MediaPlayer.Current.Play(mi);
MediaPlayer.Current.Play();
MediaPlayer.Current.Playlist.Current
MediaPlayer.Current.Playlist.PlayNext();
MediaPlayer.Current.Playlist.PlayPrevious();
MediaPlayer.Current.Playlist.Add( MediaItem.GetNew("http://www.zpf.fr/podcast/02.mp3", MediaType.Audio, MediaLocation.Remote) );
MediaPlayer.Current.StepBackward();
MediaPlayer.Current.StepForward();
MediaPlayer.Current.Stop();
MediaPlayer.Current.MediaExtractor.CreateMediaItem(mediaItem);
Property | Description |
---|---|
Duration |
TimeSpan |
Position |
TimeSpan |
State |
Playing, Paused, Stopped, Loading, Buffering, Failed |
MediaPlayer.Current.Playlist
MediaPlayer.Current.Playlist.RepeatMode = RepeatMode.Off;
MediaPlayer.Current.Playlist.ShuffleMode = ShuffleMode.Off;
MediaPlayer.Current.Play(NextItem);
MediaPlayer.Current.Play(PreviousItem);
Property | Description |
---|---|
Balance |
From -1 (Left) to +1 (right). 0 = center. |
Volume |
From 0 to 1. |
Muted |
True or False |
event | UWP | iOS | Android | Mac | WPF |
---|---|---|---|---|---|
MediaItemChanged | X | X | |||
MediaItemFailed | X | X | X | X | |
MediaItemFinished | X | X | X | X | X |
PositionChanged | X | X | X | X | X |
StateChanged | X | X |
X = implemented, blank = not yet implemented
- code review
- iOS, Mac, WPF, ...
- MediaExtractor (WPF)**
- sync native playlist with internal playlist (UWP, ...)
- check ExoPlayer on Android
- video
- enhance/(re)design sample application
!!! It seam that since update to VS2019 16.1.0/16.1.1 the build doesn't function anymore. It's still fine with VS2017. !!!
On Windows you can build the solution with Visual Studio 2019 with the latest Xamarin, .NET Core and UWP installed.
For the moment the solution doesn't build with VS2019 on MacOS: https://developercommunity.visualstudio.com/content/problem/536913/vsfm-2019-doesnt-work-with-project-file-sdks-like.html .
Nor on Microsoft Visual Studio Professional 2019 Version 16.1.1 novotnyllc/MSBuildSdkExtras#168 https://developercommunity.visualstudio.com/content/problem/536913/vsfm-2019-doesnt-work-with-project-file-sdks-like.html
Microsoft Visual Studio Professional 2017 Version 15.9.12 OK - Nuget - Android, iOS, MacOS, UWP, WPF
it doesn't build on Microsoft Visual Studio Enterprise 2019 (Preview) for Mac Version 8.1 Preview (8.1 build 2460)
It builds on 'Visual Studio 2017 Developer Command Prompt'
Build WPF debug
msbuild zpfmediaplayer.csproj /p:TargetsToBuild=Wpf /t:Rebuild
Build all platformes debug
msbuild zpfmediaplayer.csproj /p:TargetsToBuild=All /t:Rebuild
Build WPF release
msbuild zpfmediaplayer.csproj /p:TargetsToBuild=Wpf;Configuration=Release /t:Rebuild
Build all platformes release (--> Nuget)
msbuild zpfmediaplayer.csproj /p:TargetsToBuild=All;Configuration=Release /t:Rebuild
Your coffee keeps me awake while working on projects like this ๐โ