how I put sound in a silverlight animation? - c#

I'm trying to put a sound to play when the animation in my silverlight animation starts. I put the sound in the user control with blend, selected auto-play, and created a PlaySoundAction to play the sound on load. Nothing worked. Anyone knows what I could be missing?

Did you use Media Element? since you have not posted any code, here is a sample on how to include sound in silverlight
MediaElement media = new MediaElement();
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNameSpace.Sound1.wav");
media.SetSource(stream);
media.AutoPlay = false;
media.Stop();
media.Play();

Related

Controlling Sounds in audio in multiple forms

I want to play a playlist in the resources folder for a windows forms project I'm working on (wav or mp3). The application will be completely offline. (actually a game project)
What I want to do is this:
I need to be able to stop or convert a music that I created in one form to another sound file, depending on the situation.
In this case, I couldn't figure out how to write a code block. There are many examples that describe how we can pause and start the music on single form, but I have not come across an example that covers the whole project and can be intervened from different forms.
I'm not sure if I explained exactly what I wanted to ask, but I can give an example as follows. Let's say I created 3 forms.
1st form and 2nd form game menus.
In these menus, the same music should continue to play.
But when I reach the 3rd form, this music should be cut and replaced with in-game music.
What I want to ask is: How can I control the music I created in form 1, in form 2 or form 3?
The reason I don't use the soundplayer class is because I have to play multiple sounds at once.
i.e., somehow I need to make the music player "static" so that I can control it from all forms, but I couldn't find the way around it.
var importer = new RawSourceWaveStream(Properties.Resources.sound, new WaveFormat());
var soundFx = new WaveOut();
soundFx.Init(importer);
soundFx.Play();
For example, in this way, I start playing the music in the form1. I need to give the command to change this music in the form3, but somehow I cannot reach the "soundfx" object that I defined there because I cannot make this process static.
I think that your intuition is valid. It is probably not the best way, but I inffer that it is a way that you would manage to do.
Have a public static class, that holds a private (or public) instance of you sound player. Have public static methods of the different things that you want to do with the audio player.
Control that class from all forms.
Since this is your intuition. I am curious of what have you so far in this way?
Anyhow, I have a few questions.
When you say "convert" the music, what do you mean?
What do you mean that you couldnt find a way around it?
Can you add some code of how you are controlling the sounds? With some code, people will be able to better support you.
It is recommended that you use MediaPlayer. From your description, MediaPlayer can better meet your needs. Compared with SoundPlayer, MediaPlayer has the following characteristics:
Multiple sounds can be played at the same time (create multiple MediaPlayer objects);
You can adjust the volume (Volume property);
You can use Play, Pause, Stop and other methods to control;
You can set the IsMuted property to True to achieve mute;
You can use the Balance property to adjust the balance of the left and right speakers;
The speed of audio playback can be controlled through the SpeedRatio property;
The length of the audio can be obtained through the NaturalDuration property, and the current playback progress can be obtained through the Position property;
Seek can be performed through the Position property;
Use MediaPlayer to play audio files as follows:
MediaPlayer player = new MediaPlayer();
player.Open(new Uri("BLOW.WAV", UriKind.Relative));
player. Play();
A MediaPlayer object can only play one file at a time. And the file is played asynchronously, you can also call Close to release the file.
For details, please refer to https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-7.0
When using it in VS, you need to perform the following steps first:
This way you can find MediaPlayer in the toolbar.
If you still have any questions please speak up.

How to play songs in repeat mode in an UWP app

I have created a UWP app to play certain tracks in the background. Basically by following this link: https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/ .
I want to set the repeat count for certain songs, so if a song has repeat count 10, that song is meant to be repeated 10 times before moving on to the next song in the playlist.
On the Windows phone 8.0 platform, the AudioPlayerAgent had the following event which indicated that the play state has changed. It was easy to override that event and add custom logic to repeat songs.
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackEnded:
// keep repeating the same track
player.Position = new TimeSpan(0, 0, (int)0);
// add custom logic here..
break;
}
NotifyComplete();
}
What would be an equivalent event in the UWP platform?
So far I have tried the following events on the UWP platform, but to no avail..
BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded;
BackgroundMediaPlayer.Current.MediaOpened += Current_MediaOpened;
With Windows 10, version 1607, a new single-process model has been introduced that greatly simplifies the process of enabling background audio.
Media continues to play when your app moves from the foreground to the background. This means that even after the user has minimized your app, returned to the home screen, or has navigated away from your app in some other way, your app can continue to play audio.
Starting with Windows 10, version 1607,the recommended best practice for playing media is to use the MediaPlayer class instead of MediaElement
Play a media file with MediaPlayer
_mediaPlayer = new MediaPlayer();
_mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
_mediaPlayer.Play();
MSDN: Play media in the background
Now your app can manage the playlist or loop settings and use the media player instance to call Play method again.

Setting AlbumArt of AudioTrack

I'm using Background Audio Player on Windows Phone 8 and I'm trying to set an album art for my audio track. As I've read here and at MSDN the image MUST be at shared/media/ - ok I've checked the image is there and I try to do:
AudioTrack myTrack = new AudioTrack(track.source, track.Title, track.Artist, track.Album, new Uri("shared/media/Episode29.jpg", UriKind.Relative));
The music starts, but there is no image. Am I doing something wrong?
I've also run Background Audio Player Sample mentioned here at MSDN and I also see no image.

Windows Library to play a video file

this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
The above code used the Media library to display an image from the Uri fed.(label contains the URL).
What library should I use to play a video file in the same manner.
(I need to play a video file instead of displaying and image)
Help ?
You can use the MediaElement for this.
It is located in System.Windows.Controls namespace and relies on the Windows Media Player. IF WMP is installed on your system MediaElement is an easy way to go.
The code to do this should look like this:
MediaElement me = new MediaElement();
// initialize and do other stuff to MediaElement
this.Background = new VisualBrush(me);

Display Current Video in Windows Phone 8 using AudioVideoCaptureDevice?

I've managed to setup code for a Windows Phone 8 Application that initializes and can start/stop recording video using an AudioVideoCaptureDevice. (saves it to an IRandomAccessStream)
//Initialize Camera Recording
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
captureDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution);
captureDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264;
captureDevice.AudioEncodingFormat = CameraCaptureAudioFormat.Aac;
captureDevice.RecordingFailed += captureDevice_RecordingFailed;
However, I cannot figure out how to hook this recording up to a VideoBrush to display the recording to the user. I want the user to be able to see the video they are recording as it is happening...
I know there is a tutorial that shows how to do this using the old APIs for Windows Phone 7 (CaptureSource, VideoDevice, etc.) but I specifically need to use the AudioVideoCaptureDevice to record. Anyone know how to display this video on screen?
Well, I was able to solve my problem.
Apparently there is a library in Microsoft.Devices that contains extensions for the VideoBrush class.
Therefore, in order to set the videobrush source to an AudioVideoCaptureDevice, you must first have:
using Microsoft.Devices;
at the top of your class in which your using the videobrush.
Hope this is able to help someone else.
You should be able to simply use VideoBrush.SetSource(captureDevice).

Categories

Resources