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.
Related
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
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();
}
}
I started building my own Windows Phone 8.1 App. I implemented a mp3 file. Whenever I implement it on the main page in the XAML
<MediaElement x:Name="GoalHorn" Source="/Sounds/mySound.mp3" AutoPlay="False" Visibility="Visible"></MediaElement>
I can call it in the source code and start it by
GoalHorn.Play()
I now wanted to put it on another frame. I used:
Frame.Navigate(typeof(ScoredPage)), scorerBox.Text);
However, when I want to start the sound on the new frame, nothing is done when calling
GoalHorn.Play()
I have it in the XAML of the new Frame aswell. When I set the autoplay to "true", it also works on the frame but I can't stop it.
Can anybody help?
It happens when media is not opened/loaded still and .Paly method is called on the media element. Add MediaOpened event handler and call play method in it.
<MediaElement MediaOpened="GoalHorn_MediaOpened" x:Name="GoalHorn" Source="/Sounds/mySound.mp3" AutoPlay="False" Visibility="Visible"></MediaElement>
Event handler
private void GoalHorn_MediaOpened(object sender, RoutedEventArgs e)
{
GoalHorn.Play();
}
In my app, I have to use an AudioPlaybackAgent (APA) and a mediaelement. I used the APA to play songs , and when I need to play video, I use the MediaElement
When I navigate to a page use the MediaElement, I stop the BackgroundAudioPlayer:
BackgroundAudioPlayer.Instance.Pause();
When I navigate back to a page which need to play music, I call the APA to start again, but now it return the exception "The background audio resources are no longer available." :(
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
try
{
if (BackgroundAudioPlayer.Instance.PlayerState != PlayState.Playing)
BackgroundAudioPlayer.Instance.Play();
}
catch
{
BackgroundAudioPlayer.Instance.Play();
}
}
I can use the MediaPlayerLauncher , but this solution has many disvantage (only fullscreen, lack of my custom control ...).
So is there any way to make the media element work along with the AudioPlaybackAgent, or any other way to playing video ???
This occurs because data get lost when navigating between pages. You can try saving data to IsolatedStorage.
You can find more information in this question:
Save values between page navigation in Windows Phone
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.