WPF / C#: async load/buffer next movie clip - c#

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.

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.

XAML: How can I target the same property twice within a storyboard?

I am building a 3D Cube with 3 different view-able sides. I want to represent a choice to the user that can be displayed in 3 different ways. For example, I want to give the user a choice to pick a language. So I want one side of the cube to be the English flag and after a few seconds, it flips to an image of the text "English" and after a few seconds, that image flips to another image signifying the English Language choice(maybe the Australian flag).
I have created a storyboard animation for that. Pseudo-code for that is
Storyboard{
MakeImage1Visible();----Problem part A
RotateImage1By90degrees();
MakeImage1NotVisible();----Problem part B
MakeImage2Visible();
RotateImage2ByMinus90degrees();
//Do the same for the transition between Image2 and Image 3
}
My issue is that this pseudocode forces me to target the same property twice within a storyboard(in problem part A and in problem Part B).
And VS doesn't allow that.
I know I can make multiple storyboards and call them from the code-behind with a timer every few seconds. This is ugly. Is there any more elegant solution?
Thanks.

Delay between on screen display and parallel port trigger

I am working on a research project which needs precise timing/synchronize between the on screen display and a trigger from parallel port in Unity.
What I am trying to do is to flash the screen to white while sending a trigger to the parellel port at the same time (desired difference is within 10ms).
I mesured the screen flash with a photodiode to determine the exact time it turns white, and synchronize it with the trigger from parellel port. I always observed a delay of 40 - 70ms between the trigger and the flash (the flash arrived slower) which is my main problem.
What I have tried so far:
- Update the flash and send the trigger in the same frame (bigger delay)
- Update the flash -> WaitForEndOfFrame() -> send trigger (lower delay but still big). Below is a sample code:
IEnumerator UpdateParallelPort()
{
while (true)
{
yield return new WaitForEndOfFrame();
if (flashed == FlashState.ToWhite)
{
parallelPort.SendTrigger();
}
}
}
I also tested if the flash take multiple frames to be rendered by using ReadPixels to determine at what frame the screen turn white, but it was not the case, it was in the same frame when I issued the command. So I guess the delay comes from the time the buffer being sent to screen ? If that is the case, is there anyway to determine/synchronize the exact timing, or to minimize it ?
This is my first post in Stackoverflow, hope that I explained it clear enough. Thank you in advance for your help!
which needs precise timing
I have bad news, completely forget it.
Unity is a game engine through-and-through.
The whole entire raison d'etre, the most fundamental aspects of it, is that it lets you render mesh of dinosaurs etc, with "total compromise" of granular time, and reasonable overall perceptive time.
Unfortunately, you literally could not choose a worse milieu for the project! Sorry! :O

Web chart for "tons" of points?

I would like to know if is there some good web solutions to show charts for "huge data sets", I've tried amcharts and Highcharts Stock (jquery solutions) without success.
At the beginning they were working, but at the moment the "chrome" is telling me that the javascript memory is full and the page crashes.
I've times where I need to show more than 20 lines, each one with more than 100.000 points, so in the end I can have gigantic jquery arrays that sure will crash the internet browser.
At the moment I am open to change to some flash, silverlight or other solution (not java applet because I am using C#).
What do you guys recommend?
UPDATE #1
For example: one purpose of this application is to see ECG channels.
The person will carry a device with several "sensors" (lets define 10 or 12, more or less), the device will save the data each second (or sometimes even in shorter intervals). And there can be cases that the person will use this system for 3 days).
Minimum data:
60 seconds * 60 minutes * 24 hours * 3 days = 259.200 points per line.
8 lines or more => 2.073.600 total points
Usability:
Well, in this health area normally the "readings" will be similar, no highs or lows enough to be recognized in a 3 days data. So for this example the best would be to load the data just when it is needed > the pan/zoom slide is showing just 1hour and when it moves to other, then AJAX get the rest of the DATA. Sure this is the way to go. BUT this is not the only case in my system.
I've other type or devices where the "highs and lows" are HUGE and the user would like to see ALL data in just one "chart" without zoom in. So, in this situations just from a simple look it is easy to see that something happened on the readings, then the user can make zoom in and since the data is already on memory no need to make more AJAX calls and refresh the chart.
Smart way to go: process the data in a way to do "reduce" the number of points when we are looking at a bigger "scale". Sure, this is the wise way to go, but once again, there are times when the result of some processing math will "fake" and hide the real readings and in the end there are some "behaviors" that will not show up on the chart.
So, for now I really need to find a way to display all of this points.
Note: I really appreciate all the feedback of you guys.
I think I'm with Neil here...there must be some way that this data can be processed before display...I mean, how can this amount of data even be displayed in a window? You say a line has 100000 points...if each of those points was unique in the X,Y plane, 100000 points would completely fill a 300x300 display window. 20 lines like this would completely saturate a normal 1024x1280 display.
Presumably, that can't be what you are looking for, so I'm assuming there must be a lot of cases where the points overlap. Preprocessing the data, to eliminate duplicate data points would help reduce the data size considerably.
It's hard to know exactly how this answer fits, or to give more precise instructions without further details, but if you have questions or clarifications, edit your question and I'll modify my answer (or delete it, if I've misinterpreted.)
Response to Edit 1:
I think that the way to approach the thinking for this is to recognize that for any given view, you can only show as many data points as you have horizontal resolution, so you can limit your data download to that.
From what I'm hearing (and I grant that I have very few details) this problem can be reduced to:
Figuring out how many points to get (based on horizontal resolution)
Calculating those points based on the data, horizontal scroll, zoom, and any heuristics.
Dynamically downloading that data
That sounds not too bad, and your original problem (of too much data crashing the system) disappears. That leaves you with secondary problem of how to calculate the height of the downloaded data.
I've other type or devices where the "highs and lows" are HUGE and the
user would like to see ALL data in just one "chart" without zoom in.
So, in this situations just from a simple look it is easy to see that
something happened on the readings...
There are a number of potential difficulties that I can see here...
If the timescale for these events is too short, they won't be visible on a naively drawn graph. If you have 100000 points in a particular line graph and your default viewing area is 1000 pixels wide with no zooming, you're only seeing 1 out of 100 datapoints. If some spike lasts for 10 of the datapoints, for example, unless you do something special, there's a good chance it won't be visible on the graph (so the user won't know to "zoom in" for more resolution). And how do you determine the height at which to plot the point? The actual datapoint at a specific spot? An average of the 100 data points that pixel covers? A rolling average? If don't average, you could miss spikes entirely. If you do average, you could lower the amplitude of the spikes or troughs if they are of short duration.
This, I think (and, again, I'm doing a lot of guesswork) sounds like the real challenge. Trying to find some way to display the graph which will definitely not be able to show all of the data at one time, but may be able to have some way to highlight points of interest dynamically (calculating, noting, and marking peaks and troughs with notations on the graph...things like that.)
Try out the Zoom Line chart from the FusionCharts stables.
I've myself created charts with 27,000 datapoints; and beyond the initial loading times, the chart worked smoothly.
Here is a blog post about the zoom line chart - http://blog.fusioncharts.com/2011/10/stuck-between-massive-historical-data-and-daily-intricacies-zoom-line-chart-to-the-rescue/
As a bonus, you can render the chart in pure JavaScript or Flash.
And it also works well with server-side languages. Check out their docs for more reading material - http://docs.fusioncharts.com/

C# cross-thread SendToBack/BringToFront operations on controls

I am developing a card game. I have represented cards with PictureBox class. I have a game where players put cards one on top of another, one by one. Z-index of all these cards are different and when card is put on top i give it top z-index with this code:
PictureBox cardPictureBox = move.Card.CardPictureBox;
if (cardPictureBox.InvokeRequired)
cardPictureBox.Invoke(new MethodInvoker(cardPictureBox.BringToFront));
This code works fine and all cards are brought to front when they supposed to. I have a problem when this game ends. That is when I need to put PictureBox objects back to their original place. I put them back but they are now not ordered as I ordered them in Designer. I have to rearrange their z-indexes. I do this with the same code, but in a loop where I do the same thing for every card.
Program doesn't throw an exception but just freezes and that's it!?!?!
Did someone encounter this problem, and does it has an answer?
How can I make a safe cross-thread change of PictureBox z-index without my program freezing?
Thanks in advance
Simplest way would be to skip the invoke when you're doing them all in a loop at the end:
foreach (CardPictureBox cardPB in Collection)
{
cardPB.BringToFront();
}

Categories

Resources