I'm using the MediaElement class to play a simple sound on a button click. That works fine for me unless I'm making a double tap on it. On the second tap I want the old sound to stop playing and restart the sound. But the Stop Method doesn't work:
public static MediaElement SoundDelete;
public static void PlaySoundCash()
{
if (SessionHelper.getConfig("SoundDelete") == "true")
{
System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString());
SoundDelete.Stop(); //dosn't work
SoundDelete.Position = new System.TimeSpan(0,0,0,0,0); //dosn't work
System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString());
SoundDelete.Play();
}
}
In the console the timespan is never 0. How can I get the sound playing from the beginning?
I was having the exact same issue. It turns out adding the MediaElement to the Visual Tree will allow you to use this extra functionality.
Adding my MediaElements to the children of my base Grid solved these issues.
For example:
C#
public static MediaElement SoundDelete;
private void MediaInit()
{
// ...
// load your sound file into SoundDelete
// ...
BaseGrid.Children.Add(SoundDelete);
}
public static void PlaySoundCash()
{
System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString());
SoundDelete.Stop();
SoundDelete.Position = new System.TimeSpan.Zero;
System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString());
SoundDelete.Play();
}
or in XAML:
<Grid Name="BaseGrid">
<MediaElement Name="SoundDelete" Source="path_to_your_sound_file.mp3" />
</Grid>
Related
I'm trying to add background music to my WPF program. I also want additional sounds to happen "over" the background music. I tried using a SoundPlayer however, this could only play one piece of audio at once.
I am now trying to use MediaPlayer but I cannot get any audio to play. Here is my code:
In my ShellViewModel I start the background music:
Sounds.StartBackgroundMusic()
In my sounds class I have the following:
private static MediaPlayer _backgroundMusic = new MediaPlayer();
public static void StartBackgroundMusic()
{
_backgroundMusic.Open(new Uri("pack://application:,,,/Assets/Sounds/backgroundmusic.wav"));
_backgroundMusic.MediaEnded += new EventHandler (BackgroundMusic_Ended);
_backgroundMusic.Play();
}
private static void BackgroundMusic_Ended(object sender, EventArgs e)
{
_backgroundMusic.Position = TimeSpan.Zero;
_backgroundMusic.Play();
}
Since I want the background music to loop continuously, I used the answer in this question to add the BackgroundMusic_Ended event. Can someone please help shed some light on why my audio isn't playing?
I tried to reproduce the problem but it worked as expected.
I created a new WPF application in Visual Studio and used the following code in MainWindow.xaml.cs:
using System;
using System.Windows;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private static MediaPlayer _backgroundMusic = new MediaPlayer();
public MainWindow()
{
InitializeComponent();
StartBackgroundMusic();
}
public static void StartBackgroundMusic()
{
_backgroundMusic.Open(new Uri(#"C:\<path-to-sound-file>\music.wav"));
_backgroundMusic.MediaEnded += new EventHandler(BackgroundMusic_Ended);
_backgroundMusic.Play();
}
private static void BackgroundMusic_Ended(object sender, EventArgs e)
{
_backgroundMusic.Position = TimeSpan.Zero;
_backgroundMusic.Play();
}
}
}
Did you try to use a "regular" file path to load a sound file instead of "pack://..."? When I used the wrong path I didn't get any sound and there was no error message or exception.
Since I couldn't use MediaPlayer to play embedded resources and SoundPlayer can only play one sound at a time, I used a combination of them and saved the embedded background music resource to disk so that MediaPlayer could play it. I make a blog about it here
Heres what I did:
I set up my SoundPlayer as this was the simplest one of the two. I created a new SoundPlayer object using the embedded resource
private static readonly SoundPlayer _soundOne = new SoundPlayer(WPF.Properties.Resources.soundOne);
Now the MediaPlayer. I make sure that my audio file is set as an Embedded Resource under the Build Actions in the file’s properties in Visual Studio. Now that we have done this, we can create the method for saving the embedded WAV file to the %temp% location on disk:
public static void SaveMusicToDisk(){
//This sets up a new temporary file in the %temp% location called "backgroundmusic.wav"
using (FileStream fileStream = File.Create(Path.GetTempPath() + "backgroundmusic.wav")){
//This them looks into the assembly and finds the embedded resource
//inside the WPF project, under the assets folder
//under the sounds folder called backgroundmusic.wav
//PLEASE NOTE: this will be different to you
Assembly.GetExecutingAssembly().GetManifestResourceStream("WPF.Assets.Sounds.backgroundmusic.wav").CopyTo(fileStream);
}
}
We play this by creating a new MediaPlayer object and using the temp file location to play the audio:
//Create a new MediaPlayer object
private static readonly MediaPlayer _backgroundMusic = new MediaPlayer();
public static void StartBackgroundMusic(){
//Open the temp WAV file saved in the temp location and called "backgroundmusic.wav"
_backgroundMusic.Open(new Uri(Path.Combine(Path.GetTempPath(), "backgroundmusic.wav")));
//Add an event handler for when the media has ended, this way
//the music can be played on a loop
_backgroundMusic.MediaEnded += new EventHandler(BackgroundMusic_Ended);
//Start the music playing
_backgroundMusic.Play();
}
My BackgroundMusic_Ended method looks like this and just makes sure that the music is always restarted once it has finished:
private static void BackgroundMusic_Ended(object sender, EventArgs e){
//Set the music back to the beginning
_backgroundMusic.Position = TimeSpan.Zro;
//Play the music
_backgroundMusic.Play();
}
Then I just had to worry about disposing of the objects and cleaning up the temp file when the program is closing.
So I was trying to loop Background music in my UWP App, I have a class called soundControl that handles music and sounds like this:
public class soundControl
{
private static MediaElement loop = new MediaElement();
public static async void stopLoop()
{
loop.Stop();
}
public static async void loadLoopTimeBG()
{
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(#"Assets\Sounds");
Windows.Storage.StorageFile file = await folder.GetFileAsync("battle.wav");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
loop.AutoPlay = false;
loop.SetSource(stream, file.ContentType);
loop.IsLooping = true;
}
public static void loopTimeBG()
{
loop.Play();
}
And whenever I want to play this music I call :
soundControl.loadLoopTimeBG();
soundControl.loopTimeBG();
the problem is the it plays just one time and stops and I have no Idea why
I tried another approach like:
loop.MediaEnded += mediaEnded;
and the event handler like this:
private static void mediaEnded(object sender, RoutedEventArgs e)
{
loop.Position = TimeSpan.Zero;
loop.Play();
}
it also didn't work and when debugging it doesn't even triger the mediaEnded event when music is complete.
Any help here would be most appreciated.
Thanks
MediaPlayer
Windows.Media.Playback.MediaPlayer is the recommended player for UWP that does not require to be in the XAML visual tree.
Its API is very similar to MediaElement:
private static MediaPlayer _mediaPlayer = new MediaPlayer();
public static async Task PlayUsingMediaPlayerAsync()
{
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(#"Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("Click.wav");
_mediaPlayer.AutoPlay = false;
_mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
_mediaPlayer.MediaOpened += _mediaPlayer_MediaOpened;
_mediaPlayer.IsLoopingEnabled = true;
}
private static void _mediaPlayer_MediaOpened(MediaPlayer sender, object args)
{
sender.Play();
}
You can even display the visuals of a MediaPlayer in XAML using MediaPlayerElement.
MediaPlayer allows for even more advanced playback scenarios using the MediaPlaybackList with support for looping, shuffle and gapless playback.
mediaElement.SetPlaybackSource(mediaPlaybackList);
MediaElement
After some digging around it seems that there are two issues.
MediaElement is XAML based control (in the Windows.UI.Xaml.Controls namespace), and it seems that it does not work properly until it is actually attached to a visual tree. Once you put the MediaElement on the page, it works as expected.
Secondly, loading source media does not happen immediately. Once you set the source, the control needs some time to actually load the media. For this purpose, you can use the MediaOpened event, that will notify you once it is really loaded.
So the code could look somewhat like this:
public static async Task LoadAndPlayAsync()
{
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(#"Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("Click.wav");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
loop.AutoPlay = false;
loop.SetSource(stream, file.ContentType);
//or simpler -
//loop.Source = new Uri("ms-appx:///Assets/Click.wav", UriKind.Absolute);
loop.MediaOpened += Loop_MediaOpened;
loop.IsLooping = true;
}
private static void Loop_MediaOpened(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//play once the media is actually ready
loop.Play();
}
And before you call the LoadAndPlayAsync method, you have to attach the control somewhere (for example in a Grid):
GridContainer.Children.Add(SoundController.loop);
await SoundController.LoadAndPlayAsync();
I have created a sample project for my tests on my GitHub, you can check it out to see how I implemented it. The first button in the app attaches the control and the second loads and plays the sound. You can see that if you click only the second one, the sound does not play.
I have an app that runs background music at the application level so that the music doesn't stop when the user navigates through pages. However, I also make use of a VideoBrush. As I found out, I cannot have the two running at the same time as the VideoBrush will crash when setting its source.
I found that if I set the source of the MediaElement to null when the user attempts to use the VideoBrush, that everything works. Sure the music stops, much to my chagrin, but no error happens.
However, when the user taps away from the VideoBrush, I am trying to make the music start back up (beginning is fine) to no avail. Simply put, I am having trouble getting the music to start up again.
Here is my code:
App.xaml
<Application.Resources>
<MediaElement x:Key="GlobalMedia" Source="minutelongsong.mp3"
MediaEnded="MediaElement_MediaEnded" Visibility="Collapsed" />
</Application.Resources>
App.xaml.cs
public static MediaElement GlobalMediaElement
{
get { return Current.Resources["GlobalMedia"] as MediaElement; }
}
private void Application_Launching(object sender, LaunchingEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play();
}
private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play();
}
And now the page that is making use of the VideoBrush.
MainPage.xaml
<Canvas x:Name="viewfinderCanvas" Width="480" Height="800" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
<Canvas.Background>
<VideoBrush x:Name="videoBrush" Stretch="Fill">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="previewTransform"
CenterX=".5"
CenterY=".5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Canvas.Background>
</Canvas>
MainPage.xaml.cs
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Pause();
AppMediaElement.Stop();
AppMediaElement.Source = null; //set it to null to allow the cam to be set.
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary)))
{
viewfinderCanvas.Visibility = Visibility.Visible;
cam = new PhotoCamera(CameraType.Primary);
if (Orientation == PageOrientation.PortraitUp || Orientation == PageOrientation.PortraitDown || Orientation == PageOrientation.Portrait)
{
videoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 };
}
videoBrush.SetSource(cam);
}
When the user exits out of the camera VideoBrush by hitting an on screen button, this code is fired. It disposes the cam, and tries to get the music playing again if the user allows the music. However the music will not play, even with this code.
private void zoomout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (cam != null)
{
cam.Dispose();
}
viewfinderCanvas.Visibility = Visibility.Collapsed;
if (allowingamemusic == true)
{
var AppMediaElement = App.Current.Resources["GlobalMedia"] as MediaElement;
AppMediaElement.Source = new Uri("minutelongsong.mp3", UriKind.RelativeOrAbsolute);
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play(); //despite this here, it will not play. No error thrown.
}
}
I switch back and forth between capturing an image and audio in the same page on my phone app.
You were very close, but you also have to set the videoBrush source to something else or it won't let go of the resources.
So when you want to use the MediaElement, then first dispose of the camera like this:
cam.Dispose();
viewfinderBrush.SetSource(new MediaElement()); //so it will let go of cam
cam = null;
//now set source for MediaElemet and do whatever
And when you use the camera again:
mediaElement.Stop();
mediaElement.Source = null; //or else setting the source for the video brush will fail
//now set source for videoBrush and do whatever
Old question, but hopefully this will help someone... it took me a few tries to figure it out.
After writing this last night, and finally testing it this morning, I see what may have happened but am still having the problem of the music not replaying once it's stopped.
MediaElement.MediaFailed was for the entire time being called. I found out that it was calling: AG_E_NETWORK_ERROR. This error is thrown when Zune is running and my device is USB connected to the same computer.
It was suggested that I deploy my app to my phone, close zune, and disconnect the USB. I tried this, and the music still did not play once I tried to replay it. This also happened on the emulator too.
The same AG_E_NETWORK_ERROR is still being thrown.
Since then, I have given up on this as I spent a few hours not finding out how to make this happen. So, I have since gone with the MediaPlayer class and will be using that instead.
------ If anyone solves this problem, I will give you the checkmark for a correct answer.
I am developing a project and I want to add to it a splash screen. I have checked questions here on Stackoverflow and other blogs and MSDN etc. but could not find the solution I am looking for.
I want my SplashScreen,
1- appear and stay on the screen 3-4 seconds but at the same time I want my Main Window NOT TO appear. When my splash screen fades out completely then Main Window should appear. Many of the examples I have checked out do not implement this. Even though I set SplashScreen.Close.(TimeSpan.FromMiliseconds(4000)) MainWindow still apeear immediately front or back of SplashScreen. They say "add an image to your project, make it's Build Action SplashScreen or Resource, if you want to handle fade out time go App.xaml.cs file and implement your own Main() method and put your logic." I know that already. It does not work.
2- If possible I want my splashscreen NOT TO fade out slowly. I want it to disappear suddenly.(if this is not possible or really hard for a intermediate developer to do it, it is ok. you may disregard.)
And please I want C# code not Xaml. My project is based on WPF adn .NET 4.0 client profile.
Thank you.
Why don't you make your splash screen a fully qualified XAML <window> and in your App.xaml set it up as your StartupUri:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="SplashWindow.xaml">
Then in your splash window's Load Event you initialize the main window (preferably somewhere else so the instance sticks around when you close the splash). From here you can also specify a timer for x-seconds to go off and show the main window / hide the splash window.
using System.Threading;
/// -------------------------------
private Timer t;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
App.MainWindow = new MainWindow(); // Creates but wont show
t = new Timer(new TimerCallback(CloseSplash), null, new TimeSpan(0,0,10), new TimeSpan(0,0,0,0,-1));
// Do Other load stuff here?
}
private void CloseSplash(object info)
{
// Dispatch to UI Thread
Dispatcher.Invoke(DispatcherPriority.Normal, x => CloseSplashMain());
}
private void CloseSplashMain()
{
App.MainWindow.Show()
this.Close();
}
You'll have to change your app's main window behaviour though, otherwise closing the splash window will cause the app to close.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
App.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
}
}
Also don't forget to dispose your timer when you're done. It's an IDisposable and will keep firing that method unless it's stopped.
There are answers but I find a an easier one. Just use the Thread.Sleep(int miliSeconds) method in the main window's constructor. This will delay your app in order to open specified miliseconds later.
In the constructor of App.xaml.cs open your splash screen, wait for a few seconds, then close before proceeding with rest of the app. I am using Unity, so I close the splash screen somewhere after the Boostrapper has initialized some services.
public partial class App : Application
{
private static SplashScreen _splashScreen;
public App()
{
OpenSplashScreen();
new Bootstrapper().Run();
}
private void OpenSplashScreen()
{
_splashScreen = new SplashScreen("SplashScreen/splash.jpg");
_splashScreen.Show(false);
}
internal static void CloseSplashScreen(double time)
{
_splashScreen.Close(TimeSpan.FromSeconds(0));
_splashScreen = null;
}
}
where Bootstrapper.cs is listed below:
public class Bootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(Container);
// initialize some services before
App.CloseSplashScreen(0);
}
protected override IModuleEnumerator GetModuleEnumerator()
{
return new ExtendedConfigurationModuleEnumerator();
}
protected override DependencyObject CreateShell()
{
MainWindow shell = new MainWindow(Container);
shell.Show();
return shell;
}
}
The best way and using the API is
SplashScreen splash = new SplashScreen("splashscreen.jpg");
splash.Show(false);
splash.Close(TimeSpan.FromMilliseconds(2));
InitializeComponent();
I'm working on porting a game engine from Java to Windows Phone 7 XNA. One thing I'm struggling with is how to create modal dialogs. The dialog is rendered in XNA using SpriteBatch just like everything else, but what I basically want is something like this:
result = Dialog.Ask("Some Question?", DialogButtons.YesNo);
Where Dialog.Ask doesn't return until the user clicks one of the buttons. The only thing I've done to come close is a method to continuously call RunOneFrame() on the game:
private int runLoopCount;
public void BeginRunLoop() {
int runIndex = ++runLoopCount;
while (runLoopCount == runIndex) {
RunOneFrame();
Thread.Sleep(1);
}
}
public void EndRunLoop() {
--runLoopCount;
}
There are a few problems with this:
RunOneFrame is only supposedly for debugging purposes.
Input doesn't work! Calling TouchPanel.GetState() or GamePad.GetState(PlayerIndex.One) doesn't return new values.
Is there any way to initiate a run loop without throwing away the Game class and all it does for initialization? And I don't really know how to do without the Game class anyway, as there is no Main() method in Windows Phone 7 XNA applications. It just goes right into the Game constructor.
What you want to do is to add another 'state', you just have to show dialog until user do not select something... and _modalDialogIsUp bool..
protected override void Update(GameTime gameTime)
{
if (_modalDialogIsUp)
{
// handle only secesary mouse and button clicks
}
else
{
// normal mouse and button clicks
}
}
protected override void Draw(GameTime gameTime)
{
if (_modalDialogIsUp)
{
// draw only modal dialog
}
else
{
// draw game
}
}
I just can't undestang why you so desparatly want exactly modal behavior... is it some kind of new "sync" sect? :)