SimpleAudioPlayer plays local files and audio data as a stream. This allows you to store audio data in a .NET Stanard Portable Class Library and play it on all supported platforms.
- Available on NuGet: http://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer
- Install into your PCL project and Client projects.
Platform Support
Platform | Version |
---|---|
Xamarin.iOS | iOS 7+ |
Xamarin.Android | API 10+ |
Windows 10 UWP | 10+ |
Xamarin.Mac | All |
Xamarin.tvOS | All |
Tizen | 4.0+ |
Call CrossSimpleAudioPlayer.Current from any project or PCL to gain access to APIs
Duration: length of audio in seconds
CurrentPosition: current playpack position in seconds
Volume: volume of audio between 0 and 1
Balance: balance between left and right as as double, -1 is left only, 0 is both, +1 is right only
IsPlaying: is the audio currently playing
CanSeek: can the playback position be updated
Load(Stream audioStream): load a compatible (wav, mp3, etc) audio from a stream
Load(string fileName): load a compatible audio file stored in the executing platform project
Play(): play the currently loaded audio
Stop(): stop playback and reset current position to start (0)
Pause(): pause playback (use Play() to resume)
Seek(double position): seek to a specific location in the audio (in seconds)
Coded in a shared library (.NET Standard or PCL) using the Xamarin.Forms dependancy service with mysound.wav stored in the PCL as an Embedded Resource
ISimpleAudioPlayer player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
player.Load(GetStreamFromFile("mysound.wav"));
player.Play();
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("YourApp." + filename);
return stream;
}
Or to load multiple instances
var player1 = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
var player2 = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
...
Add the following to the FinishedLaunching method of the AppDelegate class. Seems to be working fine now. Remember to Enable background audio within info.plist!
private void EnableBackgroundAudio()
{
var currentSession = AVAudioSession.SharedInstance();
currentSession.SetCategory(AVAudioSessionCategory.Playback,
AVAudioSessionCategoryOptions.MixWithOthers);
currentSession.SetActive(true);
}
For more examples see the Samples folder or check out https://github.com/adrianstevens/Xamarin-Forms/tree/master/DrumPad2
Contributions are welcome! If you find a bug or want a feature added please report it - and don't forget to include your device make, model and OS version.
If you want to contribute code please file an issue, create a branch, and file a pull request.
MIT License - see LICENSE.txt