I have an accord videoSourcePlayer control in my form.Why is it that it is not rendering the video that I select? Please see my code below:
// Open video file using DirectShow
private void openVideoFileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// create video source
FileVideoSource = new FileVideoSource(openFileDialog.FileName);
// open it
sourceInitialiization = true;
OpenVideoSource(FileVideoSource);
}
}
// Open video source
private void OpenVideoSource(IVideoSource source)
{
// set busy cursor
this.Cursor = Cursors.WaitCursor;
// close previous video source
CloseVideoSource();
// start new video source
videoSourcePlayer.VideoSource = new AsyncVideoSource(source);
videoSourcePlayer.Start();
// reset statistics
statIndex = statReady = 0;
// start timers
timer.Start();
alarmTimer.Start();
//alarmTimer1.Start();
videoSource = source;
this.Cursor = Cursors.Default;
}
In my laptop where I initially coded this program, this works perfectly, but if I transfer it to another machine, say a desktop or another laptop, the code doesn't work anymore. It runs but it doesn't render the video and there is not error detected in the debugger too.
I tried downloading a sample video project from accord framework but I still couldn't get it to play a video on the desktop except on my laptop. What am I missing? Thank you.
Have you subscribed to the NewFrameReceived event?
Related
This Question is In reference with my previous question Embedding VLC player in WInform Application in .Net Core. Core.Intialize() Giving Exception
I want to run the player for certain time and during that time video should be on repeat. Currently code looks like this ...
Core.Initialize();
var libvlc = new LibVLC();
// Make VideoView control
VideoView vv = new VideoView();
vv.MediaPlayer = new MediaPlayer(libvlc);
vv.Dock = DockStyle.Fill;
// Add it to the form
Controls.Add(vv);
var uri = new Uri(#"C:\vid.3gp");
// Use command line options as Options for media playback (https://wiki.videolan.org/VLC_command-line_help/)
var media = new Media(libvlc, uri, ":input-repeat=65535");
vv.MediaPlayer.Play(media);
//Set fullscreen
this.FormBorderStyle = FormBorderStyle.None;
this.Size = Screen.PrimaryScreen.Bounds.Size;
this.Location = Screen.PrimaryScreen.Bounds.Location;
How I can close the player after certain time. currently even if I close the form with the player video keeps playing in background till I close the whole application.
Just to inform the this winform application is created in .netcore3.1.
Regards.
Create MediaPlayer as class field and call it to start/pause/stop it in you WinForm application.
private LibVLC _libVlc;
private MediaPlayer _mediaPlayer;
...
// Call this method in your constructor/initializer
private void StartMediaPlayer(string videoUrl)
{
using var media = new Media(_libVlc, new Uri(videoUrl), ":input-repeat=65535");
_mediaPlayer = new MediaPlayer(_libVlc)
{
Media = media
};
_mediaPlayer.Play();
}
// Method to stop media player
private void button1_Click(object sender, EventArgs e)
{
_mediaPlayer.Stop();
}
I try to open an external editor during Runtime and edit an image which is currently is set to a PictureBox, edit the image and update the image after closing the editor.
For this, I have a simple c# windows application with a PictureBox two Button one to load PictureBox Image from file and another to edit the image using MS Paint.
Here is the code:
public partial class Form1 : Form
{
string myImagePath = #"C:\temp\bt_logo.png";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(myImagePath);
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
launchEditor.FileName = "mspaint";
launchEditor.Arguments = myImagePath;
launchEditor.UseShellExecute = true;
System.Diagnostics.Process.Start(launchEditor);
}
}
The Editor is openned successfully but the changes cannot be saved because of access problem:
Any idea how to solve this problem?
EDIT1:
In order to stop accessing the image when the editor is open, I modified the code of button2_Click() as follows:
pictureBox1.Image = null;
System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
launchEditor.FileName = "mspaint";
launchEditor.Arguments = myImagePath;
launchEditor.UseShellExecute = true;
System.Diagnostics.Process.Start(launchEditor);
pictureBox1.Image = Image.FromFile(myImagePath);
same resault. As another try, I made a copy of the image, modified it and copied it to the original image,
System.IO.File.Copy(myImagePath, myImagePath_temp, true);
System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
launchEditor.FileName = "mspaint";
launchEditor.Arguments = myImagePath_temp;
launchEditor.UseShellExecute = true;
System.Diagnostics.Process.Start(launchEditor);
System.IO.File.Copy(myImagePath_temp, myImagePath, true);
the same resault!
This question is essentially a duplicate of
Open Image from file, then release lock?
Image.FromFile locks the file on disk, until the image variable in your code is disposed of. You should observe the accepted answer to that question above; it provides a way to load the image data from disk, copy it inside the memory of your program, and then release the lock on the file and use the copy of the bitmap data in your picturebox
You'll find an answer on how to load your image without it being locked in the file system. I'm writing an answer instead of a comment because I wanted to give a bit of further advice to the rest of the program
Take a look at the FileSystemWatcher class; Notification when a file changes?
you can use it to detect changes to your file so the picture box can be updated when the file is saved rather than when paint is quit- this will be easier than coding to detect the app closing
Problem:
As a part of school project, I attempt to build an application that provides a guitar AMP using the NAudio library.
When i plug in the guitar it recognizes it, and everything is working properly, but there is a huge delay between the input and the output from the speakers.
Here is my source code:
private void button2_Click(object sender, EventArgs e)
{
if (sourceList.SelectedItems.Count == 0) return;
int deviceNumber = sourceList.SelectedItems[0].Index;
sourceStream = new WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
sourceStream.StartRecording();
WaveInProvider waveIn = new WaveInProvider(sourceStream);
waveOut = new DirectSoundOut();
waveOut.Init(waveIn);
waveOut.Play();
}
in this code I catch an event of a button click that uses the selected input (microphone/guitar) and converts the sound it recieves to output.
The delay between the input and the output is around ~1sec and it's a deal breaker.
How do I improve the delay, to make the application more responsive?
DirectSoundOut and WaveIn are not particularly low-latency audio APIs. For something like this, ASIO is preferable. AsioOut is unfortunately a bit more complicated to work with, but it should allow you to get much lower latencies.
i need to know how to save a sound stream in a file on the fly after record
this is my code, that i need to save an external file wav or smth.
so at that point here there are record button play button and play objects
private void recordButton_Click(object sender, EventArgs e)
{
// Get audio data in 1/2 second chunks
microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
// Allocate memory to hold the audio data
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
// Set the stream back to zero in case there is already something in it
stream.SetLength(0);
// Start recording
microphone.Start();
SetButtonStates(false, false, true);
UserHelp.Text = "recording";
// StatusImage.Source = microphoneImage;
}
private void playButton_Click(object sender, EventArgs e)
{
if (stream.Length > 0)
{
// Update the UI to reflect that
// sound is playing
SetButtonStates(false, false, true);
UserHelp.Text = "play";
// Play the audio in a new thread so the UI can update.
Thread soundThread = new Thread(new ThreadStart(playSound));
soundThread.Start();
}
}
private void playSound()
{
// Play audio using SoundEffectInstance so we can monitor it's State
// and update the UI in the dt_Tick handler when it is done playing.
SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
soundInstance = sound.CreateInstance();
soundIsPlaying = true;
soundInstance.Play();
}
You need to build the .wav header properly and then write the audio stream.
Check this example
I am trying to change the code http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download from picture box to image or bitmap as I dont want to display any image or plan to display, all I want is that it will output the image to file.
I have tried changing the webcam.cs from PictureBox _FrameImage to Bitmap _FrameImage and PictureBox ImageControl to Bitmap ImageControl and casting (Bitmap) at e.WebCamImage
As well as changing it on the main form:
private void bWebcam_Click(object sender, EventArgs e)
{
WebCam webcam = new WebCam();
Bitmap image = null;
webcam.InitializeWebCam(ref image);
webcam.Start();
webcam.Stop();
FileStream fstream = new FileStream("testWebcam.jpg", FileMode.Create);
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
}
Unhappyly it doesnt seem to work so
how could I change it from picture
box to Bitmap or Image or similar
storage before saving it to a file or
save it to file directly ?
The source code I am using is:
http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download
Instead of using the WebCam class, why not just use the WebCamCapture class directly (since you are not displaying this in a form) and handle the ImageCapture event directly. The event argument for the event contains the Image. You could, in the event handler save the image to disk. Alternately, if you want to use the sample and the WebCam class, and you have a form. Use a PictureBox but leave it hidden (set Visible to false) and then just copy the image from there and save to disk when you need to.
Here is some sample code of using the WebCamCapture class instead of the WebCam class. It should be noted that this code is based on the sample code from the link provided in the question. I have kept the style of the sample so that code lines up.
Edit: Adding example of using WebCamCapture instead of WebCam class. This code should be used to modify Form1.cs in the sample code.
// Instead of having WebCam as member variable, have WemCamCapture
WebCamCapture webCam;
// Change the mainWinForm_Load function
private void mainWinForm_Load(object sender, EventArgs e)
{
webCam = new WebCamCapture();
webCam.FrameNumber = ((ulong)(0ul));
webCam.TimeToCapture_milliseconds = 30;
webCam.ImageCaptured += webcam_ImageCaptured;
}
// Add the webcam Image Captured handler to the main form
private void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
Image imageCaptured = e.WebCamImage;
// You can now stop the camera if you only want 1 image
// webCam.Stop();
// Add code here to save image to disk
}
// Adjust the code in bntStart_Click
// (yes I know there is a type there, but to make code lineup I am not fixing it)
private void bntStart_Click(object sender, Event Args e)
{
webCam.Start(0);
}
Using reflector you can see that internally the WebCam class uses a timer to simulate a framerate. Therefore calling start and stop right after each other will never generate an image since the application doesnt handle application events (and therefore the timer tick event) in between starting and stopping. You should register on the ImageChanged event and call stop in there.
Good luck
** Edit: the start logic **
public void Start(ulong FrameNum)
{
try
{
this.Stop();
this.mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, this.m_Width, this.m_Height, base.Handle.ToInt32(), 0);
Application.DoEvents();
SendMessage(this.mCapHwnd, 0x40a, 0, 0);
SendMessage(this.mCapHwnd, 0x432, 0, 0);
this.m_FrameNumber = FrameNum;
this.timer1.Interval = this.m_TimeToCapture_milliseconds;
this.bStopped = false;
this.timer1.Start();
}
catch (Exception exception)
{
MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + exception.Message);
this.Stop();
}
}
In WebCam.cs you have:
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
If you modify the ImageCaptured code you can do what you want: e.WebCamImage is an Image.
for example you could change/add constructor to accept a file name and, in the ImageCaptured event, you could save image to file.