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.
Related
I am using Unity in my Android app. When the app opens, I show the user data in the Unity scene. User data contains texts and images.
In order to load this data in the scene, I make a lot of
UnityPlayer.UnitySendMessage(GameObject,Function,Params)
calls to many C# scripts in Unity from Android. Each of these calls performs a specific task like loading an image, creating a text, applying the text font etc.
Problem is that the loading of this data right now looks incremental (if that's the right word). So the text is visible first and after a fraction of a second the image shows. Some text effects show later. This looks ugly.
How can I make all the UI elements load at the same time even if it takes some time? Is there a way to freeze the scene before each game object is ready to be drawn so that user sees the final output as it should be and not as a series of UI element loading at different times.
Edit: On Android side, this is a simplified sample of how I make the calls
//I call these methods one after the other which make Unity calls
createText(textList)
createImages(imageList)
private fun createText(texts: List<Text>) {
for (text in texts) {
UnityPlayer.UnitySendMessage("Text", "CreateText", text.name+","+text.fontSize+","+text.bold)
}
}
private fun createImages(images: List<Image>) {
for (image in images) {
UnityPlayer.UnitySendMessage("Image", "CreateImage", image.name+","+image.url+","+image.size)
}
}
On Unity, I have the implementations for the same in different C# scripts. Something to note and my bad for not mentioning it is that Images are downloaded from a url which is passed to Unity as in the method above. That is what causes this lag in images showing up but I'm fine with that as long as everything else shows up at the same time.
Sorry for the title. I couldn't think of a better one. Also, sorry for my English.
I have these four forms. Forms 2-4 have a button that when pressed will play a specific sound from Form 1. There are times that all buttons from the three forms will be pressed almost the same time which causes the audio played from Form 1 overlap each other. How do I prevent this?
Edit:
When all buttons are pressed, I want the audio to "take turns" on being played. "Audio 2, 3" must wait for "Audio 1" to finish playing before they play, and then Audio 3" must wait as well for "Audio 2" to finish. Each "Audio playback" is composed of a SpeechSynthesizer (played using .SpeakAsync) and a SoundPlayer (played using .PlaySync). There's an internet outage on my ISP so I'm just using my phone. It would be quite hard to manually type the code on my phone so sorry for that.
Hi I did a similar thing with a game I made where I had the problem of sounds playing over the top of eachother. In the method that dealt with the sounds, I did this:
SoundPLayer sound = new SoundPlayer(..);
SoundPLayer anotherSound = new SoundPlayer(..);
public async void gameMethod()
{
sound.Play();
await Task.Delay(400); //length of sound
//some code ...
anotherSound.Play();
}
This worked for me and just used await Task.Delay(..) to add more time around sounds - not sure if this is the way to do this but it might be helpful
I need a main 'home' video (full screen HD) running in a loop, and then at some point (say the user presses a key) another short movie plays (one of twelve selected at random), and then back to the home movie loop.
I'm using the mediaElement in WPF from my C# code - Is there a way to load the next movie into memory so that it's ready to play instantly? It currently takes about a second or so...
(I'm actually using two media elements with the triggered movie on top - I was doing a cross fade but have taken that out now until I can buffer the next clip. Not sure if this is the best way of doing this, any ideas? Many Thanks.
I figured out a way in case anyone is interested.
You can just specify a bunch of mediaElements on top of each other (can have different z index but not essential). Then did
video.Opacity = 0.0;
video.Play();
video.Pause();
for all of them, then when you want to play one you just set the opacity to 1 and play it and it'll play instantly.
Don't know if that's the best way, but works well. Now going to implement a cross fade to finish up. Happy days.
I am working on an MVVM C# Metro application that uses media components, particularly leveraging the play to capabilities.
Normally it seems that you would bind properties, however I need to make calls such as MediaElement.Play(source); and things such as that. The best solution I have come up with thus far is to fire an event from the view model that is handled by the code behind.
Is this in fact the best practice, or is there a more sophisticated approach?
There is an useful series of articles on MSDN that might help you to do this in more efficient way:
Multimedia Overview
Audio and Video How-to Topics
NOTE: These are still .Net 4 examples, but I bet that it will not require a lot efforts to run it with Windows 8 with all its enhancements.
I have tried doing a media player in WPF using MVVM way once, what I felt from my experience is that, it will be a real pain in doing it in the MVVM way. I would suggest write code behind if its less complex and faster than sticking to MVVM always, You can separate it as a user control later with some dependency properties if you want it to look cleaner.
Anyways, regarding the media playback what you could "also" do is keep a media player (Media Player) in your viewmodel and create a videobrush pointing to that mediaplayer and use it to show your video in the view. Rectangle or any other element for which you set the drawing brush can be used.
As your media player is the viewmodel, now you can play it, stop it, seek it etc
Something like this,
var player = new MediaPlayer();
var myVideo = new VideoDrawing { Rect = new Rect(0, 0, 1, 1), Player = player };
var dBrush = new DrawingBrush(myVideo);
// Use drawing brush to fill a rectangle
rectangle.Fill = dBrush;
I want to make a simple 2d game in Silverlight, but it seems like things have changed since the last time I tried to make a game using mode 13h graphics. Can someone give me a run-down of how you'd go about it.
I just mean at a high-level, focusing on the silverlight-specific aspects; not general game design.
A fictional example might be:
'The main game loop shouldn't be a loop, use a DispatchTimer instead. Use a Canvas as the main drawing object; but realize that we don't bother drawing individual pixels - all of your in-game objects should be represented by controls. Be sure to set the 'UseHardwareFlag' to true'. Etc, etc...
If you want to stick to the mode 13 way of programming have a look at the WriteableBitmap.
Some very nice demos here
I succeeded in porting Wolf3D (2 and a half D) to Silverlight this way.
I used the CompositionTarget.Rendering event
EDIT
I also found this, it is less mode 13 and more in line with your example.