How to add sound in sound player from resource WPF - wpf

How can i add this sound image
like i make it with background in xaml.
<Controls:FlipView.Background>
<ImageBrush ImageSource="Gamedata/MainMenuBackground.jpg"/>
</Controls:FlipView.Background>
I already have tried this , but it didn't help
SoundPlayer PlaySound = new SoundPlayer(#"pack://application:,,,/Gamedata/Sounds/MenuClick.wav");
PlaySound.Load();

I'm not sure what exactly you're trying to accomplish, but to play a sound with the SoundPlayer class you use the Play() method. To load it from a resource, add it to your Resources.resx file and use Properties.Resources.resourceName.
SoundPlayer PlaySound = new SoundPlayer(Properties.Resources.MenuClick);
PlaySound.Play();

Related

Cannot seek media element in silverlight

This is the situation.
I have a MainPage.Xaml, within this MainPage.Xaml I have embedded a UserControl (VideoWidget.Xaml) (LayouRoot.Children).
VideoWidget.Xaml has a mediaElement embedded in its LayoutRoot.
I'm trying to Pause/Play the MediaElement (mediaElement1) from MainPage.Xaml but does not respond.
e.g.
public MainPage()
{
// Required to initialize variables
InitializeComponent();
Video testVideo = new Video();
VideoWidget video = new VideoWidget(testVideo);
videoWidget.Height = 246;
videoWidget.Width = 290;
LayoutRoot.Children.Add(video);
video.mediaElement1.play();
}
The All methods for mediaElement are not responsive.
Help please.
Mohit
Try putting test control buttons inside the VideoWidget itself. Does that work? If so, there may be a problem with the way you are exposing the mediaElement in the VideoWidget as a public member.

Play sound when binding updates

hallo I want to play wav file in my WPF application when app detects disconnect from network. I am using MVVM pattern and PRISM. What I want to achieve is when my Viewmodel receives Event published by EventAggregator it sets some property to bool. I want to e able to listen to this property change from XAML and play sound based on its value
You might try to use System.Media Namespace SoundPlayer or MediaPlayer to play sounds based on Event Trigger in View Model
Eg:
readonly SoundPlayer _alertBeep = new SoundPlayer("FilePath");
private MediaPlayer _laserBeep = new MediaPlayer("FilePath");
SoundPlayer has option to only play / stop. But MediaPlayer is extended to have more controlling options.
You could make a converter that passes on the value as is and plays a sound with a MediaPlayer. That way, you could attach it to a specific binding instead of the view model.

WPF custom control question

i've done this before but i cannot find my old code.
how do you embed a window inside a window.
let say i created a custom form and saved it as Window1.xaml, and want to embed it in Window2.xaml, without copy and pasting the xaml code.. TIA
EDIT: i think my question is somewhat misleading, i'll rephrase it.
i have this Window1.xaml i added custom headers and background images/colors.
then in Window2.xaml, i want Window1 to be a custom control and embed it here.
not sure if its Content Presenters, still googling for the answer :)
You can't host a WPF Window inside another WPF Window, but you could move the content from one Window to another:
var window1 = new Window1();
var window2 = new Window2();
var content = window1.Content;
window1.Content = null;
window2.Content = content;
Note that you set window1.Content to null or else you get an exception, since the content will have a visual parent otherwise.
UPDATE
It appears all you need to do is to copy all the XAML between the <Window></Window> tags in Window1 into a new UserControl, then host that user control in Window2.
I believe you should make use of Pages or usercontrols in such cases. This way you can navigate to other parts/pages/controls defined in application. CodeKaizen is right , you can't host a window inside another window
I'm not sure you can do that - however, you shouldn't put the user interface directly into a window, use a normal control (either custom or user) instead and reuse that in your windows.
I know you can do it in code behind
//Window you want to show
Window1 child = new Window1();
object content = child.Content;
child.Content = null;
//Where to show
this.grid1.Children.Clear();
this.grid1.Children.Add((UIElement)content);
Hope helps!
It sounds like you really want a UserControl. Change Window1's type from Window to UserControl and then put that UserControl in Window2.

Using Image Control in Silverlight (4)

I'm trying to use the Image control is a very basic way, like here:
http://www.silverlightshow.net/items/Using-the-Image-control-in-Silverlight-2-Beta-1.aspx
So I'm ending up with XAML like this:
<Image x:Name="imgSmall" Stretch="Fill" Source="../Img/Small/105.jpg" Margin="10,0,0,0"></Image>
Which isn't working. The Image is blank, and in the designer the URI is underlined with a message of "...is not part of the project or its build action is not set to 'Resource"
If I change the source to a property on my ViewModel, set like this:
new Uri(App.Current.Host.Source, "../Img/Small/105.jpg");
Then it works fine. I'd much prefer to use the simpler syntax and get the image directly. Is this possible?
(The images are one level up from ClientBin)
Setting all of my web sites images to build=Resource is not possible.
Thanks!
You have to create a converter that takes the relative image path and adds the "absolute" part. You can pass the relative Uri as binding value or as converterParameter.
class ImageConverter : IValueConverter
{
// method convert()
return new BitmapImage(new Uri(App.Current.Host.Source, ((string)parameter));
//...
}
It id doesn't work because image is not added to your project.
Add image to project and in then you can set source from xaml.

Display GIF in a WP7 application with Silverlight

I would like to display gif in my WP7 application.
Is there some way to achieve this ?
I've tryed this one http://imagetools.codeplex.com/ but can't make it working with WP7.
Thanks in advance for any help
In fact,
it's working, but it lacks some documentation.
After some troubles, here's how to use it :
reference ImageTools
reference ImageTools.Controls
reference ImageTools.IO.Gif
Add namespace in xaml :
xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
And resources :
<phone:PhoneApplicationPage.Resources>
<imagetools:ImageConverter x:Key="ImageConverter" />
</phone:PhoneApplicationPage.Resources>
Then use the control with the converter :
<imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />
Your ImageSource should be an Uri, for example :
ImageSource = new Uri("http://mysite/my.gif", UriKind.Absolute);
Don't forget to add decoded :
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
Check out Jamie Rodriguez's post here on using GIFs with WP7. He uses the ImageTools project from CodePlex.
http://blogs.msdn.com/b/jaimer/archive/2010/11/23/working-with-gif-images-in-windows-phone.aspx
I struggled to get the accepted answer working. The following solution worked for me to display a static gif.
public ImageResponse(string imageUrl)
{
InitializeComponent();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
var imageResponse = new ExtendedImage();
imageResponse.UriSource = new Uri(imageUrl);
imageResponse.LoadingCompleted += this.ImageResponseLoadingCompleted;
}
private void ImageResponseLoadingCompleted(object sender, EventArgs e)
{
var imageResponse = (ExtendedImage)sender;
Classes.Util.UiThread.Invoke(() =>
{
this.ImageResponse.Source = imageResponse.ToBitmap();
});
}
Classes.Util.UiThread is a helper class I use to call the UI Thread
this.ImageResponse is a standard image control
Is it an animated GIF? If not, I would try converting the GIF to another supported file format before using it in your app.
WP7 Silverlight supports JPG/PNG.
As per http://msdn.microsoft.com/en-us/library/ff462087(VS.92).aspx the Silverlight image control does not support GIF files.
By using ImageTools you are converting the GIF file to something else on the fly on the device. If you are using gif files that you have control of (i.e. You are bundling them in the XAP or they are coming from your webserver.) you should use converted versions of these files.
This will mean that the app has to do less.
The knock on effect is that:
1. You will have to write less code.
2. The app will have to do less work and so will perform slightly better.
Of course, this doesn't cover animated GIFs. For these you'll need to use a different approach.

Resources