MediaPlayer Universal Windows App - c#

I started to develop application using xamarin, and one of projects inside my solution is UWP.
I need to play sound there when someone clicked button, I'm using MediaPlayer to achieve my goal, and on windows 10 (desktop) it works fine, but on my Windows Mobile 10 (Lumia 930) it starts with long delay (about 1 second).
Below I provide my code to play audio source:
MediaPlayer _player = BackgroundMediaPlayer.Current;
_player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));
_player.Play();
My Question is:
Is there any other way to play audio in UWP than MediaPlayer?

If you don't have specific reason to use background audio, you can use just media element to play audio in foreground:
<!-- create element in XAML or in code -->
<MediaElement Name="mediaElement" ... />
// Code - set source or reference to stream
MediaElement mediaElement = new MediaElement();
mediaElement.Source = new Uri("msappx:///Media/sound.mp3");
I would also recommend to check with the list of supported codecs.
In more complex scenarios you may want to look at Audio Graph API.

I'm not sure if this is bad practice or not, but I am able to get instant playback if I pre-load the media.
Something like this example in pseudo-code (c# style):
class Foo
{
private MediaPlayer _player;
Foo() //constructor
{
_player = BackgroundMediaPlayer.Current;
_player.AutoPlay = false;
_player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));
}
void ButtonClicked(Object sender, EventArgs event)
{
_player.Play();
}
}

Related

playing media element when lock screen xamarin forms

I am Using Xamarin forms for play some music , when I lock my phone, the audio will stop. how should I Handel ? I do not want music pause
{
MediaElement KeepScreenOn="True" x:FieldModifier="Public" Source="Voice.mp3" AutoPlay="False" ShowsPlaybackControls="True" x:Name="MyMedia"
}
MediaElement is currently experimental, I cannot found this feature play the music when lock screen, but you can use CrossMediaManager, It is support the play the music when lock screen by default.
Just Used a Button to invoke the play method.
<Button Text="play" Clicked="Button_Clicked"></Button>
private async void Button_Clicked(object sender, EventArgs e)
{
await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
}
Note:when add CrossMediaManager.Current.Init(this); in your OnCreate method of MainActivity.cs. Please do not forget the set the Target Framework to Android 9.0 or later.

Xamarin Media Plugin Auto start or Timer

In my iOS Xamarin forms project I'm using Xam.Plugin.Media from https://github.com/jamesmontemagno/MediaPlugin as follows
async void Handle_Clicked(object sender, System.EventArgs e)
{
await CrossMedia.Current.Initialize();
var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions
{
DefaultCamera = CameraDevice.Front,
SaveToAlbum = true,
});
}
Is it possible to automatically start video recording or set a timer for the recording to start?
Ultimately, I'm trying to build a lightweight remotely controllable camera app. So the device whose camera is controlled needs to be able to automatically trigger/start the camera.
Any hint appreciated.
You can't start a video or take a photo without user interaction with Xamarin Media Plugin.
At the following link you can find all StoreVideoOptions where offered
https://github.com/jamesmontemagno/MediaPlugin/blob/master/src/Media.Plugin/Shared/MediaStoreOptions.cs

Windows Phone MediaElement

I am creating an app using c# and xaml where i have 20 pages each page has some character images and when i tap on that image some dialogues should pop up.so i have taken a MediaElement and made it global like this:
App.xaml:
<MediaElement x:Key="StorySound"
Volume="1"
AutoPlay="True"/>
App.xaml.cs:
public static MediaElement StorySound
{
get { return Current.Resources["StorySound"] as MediaElement; }
}
And on every page on tap event i have written this code:
App.StorySound.Source = new Uri("/Sounds/Dialogues/" + textblock.Text + ".mp3", UriKind.Relative);
App.StorySound.MediaOpened+=StorySound_MediaOpened;
void StorySound_MediaOpened(object sender, RoutedEventArgs e)
{
App.StorySound.Play();
}
This plays the sound on not more than 2 pages when i navigate to the 3rd page using the next button which i have created no sound plays but when i close the app and directly open the 3rd page the sound plays.I have even tested this by using MessageBox to show the current state of the mediaelement i have found that on the first two pages the current state is "Opening" and the sound plays but on the third page the current state is "Closed" so the sound does not Play.is there any memory issue when playing sounds using MediaElement.I cannot use SoundEffect Because all my sound files are .mp3 and if i use soundeffect i will have to convert these sounds in .wav which will increase the size of my app because i have more than 50 sound files.
If I understand you correctly you are hooking the MediaOpened-event on the global StorySound-object on each page but you never seem to unhook it.
I would suggest you to either:
use a local MediaElement on each page that you start (and perhaps stop), or
make sure you unhook all events on the StorySound when you navigate (so that each page is the only "user" of this global resource).
I am sure it will be fine to have may mp3's in your app; you should not need to convert them to wav.

How do I play sound when app begins windows phone

On my windows phone app, I want to play a short .wav audio, similar to windows when it boots up. At first I tried using an event handler when one of my controls was loaded (this worked about 60% of the time, which is very interesting, maybe someone can clear that up as well, I'm thinking it has to do with the order things happen to load. That's why its different each time running it). I'm using visual studios 2012 ultimate, this is my xaml code for the .wav file:
<MediaElement x:Name="MySound"
Source="/quantum_drive.wav"
Volume="1"
AutoPlay="false"
/>
I've also just tried to call this method when the main page loads:
private void MainPage1_Loaded(object sender, RoutedEventArgs e)
{
MySound.Play();
}
For some reason this only works about 60% of the time(seemingly randomly), why is this?! This seems like a common thing to want to do in a phone app, yet I cannot find any information on this on stackoverflow or google searches.
Don't use the media element to do what you are trying to do. It is to buggy for sound effects. It is meant more for user interaction media.
Instead, do the following (you can the full article here)
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
static Stream stream1 = TitleContainer.OpenStream("soundeffect.wav");
static SoundEffect sfx = SoundEffect.FromStream(stream1);
static SoundEffectInstance soundEffect = sfx.CreateInstance();
Now just invoke play sound from your loaded method
public void playSound(){
FrameworkDispatcher.Update();
soundEffect.Play();
}

Set source and play a single mp3 file on each button on windows phone 7

i'm trying to make an application for windows phone 7.1 for schoool. I want to play a sound for each pressed button on my application (just like istantfun button for android) but i have some problem. If i declare on my xaml all the 120 Media Elements my applcation will play only 3/4 sound randomly. I want to make something like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
prova.Source = new Uri("/mp3/call.mp3", UriKind.Relative);
prova.Play();
}
where prova is a single MediaElement declared on the first page of xaml file.
How can i do? Thanks advice
first of all if you want play sound in multi-page app than media element is no good solution.
Besides of it's limitation (single sound at a time) and after considering amount of sound effects maybe you should try using XNA as described here: http://www.dotnetscraps.com/dotnetscraps/post/Play-multiple-sound-files-in-Silverlight-for-Windows-Phone-7.aspx
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
}
With this solution you don't need any XAML elements and thus you can play it app-wide.

Categories