I'm trying to write a simple program that I'm going to be using inside another program for playing back audio and video files using the Microsoft.DirectX.AudioVideoPlayback.dll file. I've got the code listed below since it doesn't have to be incredibly complex. The problem that I am having is that... well, the program does nothing. Not even the main window shows up and I don't know why. I'm using .Net 4.0 and the DirectX DLL version says it's 1.0.2902.0. I tried moving the initialization for the audio and video files to different places (The load event and the button press event specifically). When in the button press event, the form loads, but as soon as I press a button, the program hangs. No errors or anything. Anyone know what is going on here? If someone has a better idea for playing audio and video files, I'm willing to consider that too.
using Microsoft.DirectX.AudioVideoPlayback;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MediaPlayer
{
public partial class Player : Form
{
Audio derp;
Video herp;
public Player()
{
InitializeComponent();
this.derp = new Audio("<Audio File Name>");
this.herp = new Video("<Video File Name>");
this.herp.Owner = this.panel1;
}
private void btnPlayPauseStop_Click(object sender, EventArgs e){
switch(((Button)sender).Text){
case "Play":
if (!herp.Playing)
herp.Play();
break;
case "Pause":
if (!herp.Paused)
herp.Pause();
break;
case "Stop":
if (!herp.Stopped)
herp.Stop();
break;
}
}
private void Player_Load(object sender, EventArgs e)
{
}
}
}
I have been using this for making my very own "Media Player Classic" clone
Be sure to debug this in 32 bit mode:
Project Menu --> {project} Properties ... --> Build --> Platform Target = x86
For video, I do not load Audio (it gets done automatically). So instead of using both, use only the one needed (video or audio)
The next thing is to ensure that the panel is visible and big enough to see (check the size after "new Video(...)")
Related
I want to make a C# Windows Forms App That displays a pen which changes into a pineapple when you click it, turns into an apple when you click the pineapple and the back into a pen, which, when you click, starts a music video.
What doesn't work for me is the video, which I do not want to display in a Windows Media Player plainly because I don't like it. Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using Microsoft.DirectX;
namespace Picture_Button
{
public partial class Form1 : Form
{
Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
private int clicks = 0;
public Form1()
{
InitializeComponent();
video.Owner = this;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
clicks++;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
switch (clicks)
{
case 0: pictureBox1.Image = Properties.Resources.Pineapple; break;
case 1: pictureBox1.Image = Properties.Resources.Apple; break;
case 2: pictureBox1.Image = Properties.Resources.Pen; break;
case 3: video.Play(); break;
case 4: video.Dispose(); break;
}
}
}
}
And literally nothing happens, the program simply freezes as if it were into an infinite loop here:
Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
Nothing shows up. Any ideas what the problem is?
EDIT: I'm trying to handle the Ending event so I can make the app exit when the video ends and somehow I managed to get this exception:
System.NullReferenceException occurred
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.DirectX.AudioVideoPlayback
StackTrace:
at VideoWndProc(HWND__* hWnd, UInt32 uMsg, UInt32 wParam, Int32 lParam)
InnerException:
By adding this code:
video.Ending += new System.EventHandler(this.Video_Ending);
//some code
private void Video_Ending(object sender, EventArgs e)
{
//throw new NotImplementedException();
video.Dispose();
Application.Exit();
}
There are two separate issues here:
Support for Legacy .net
First issue: "DirectX for Managed Code" is very old and based on .net version 1.1. In order to use this assembly in .net 4 or newer, you need to enable loading these older formats. You do this by changing the file "app.config" in your project and setting useLegacyV2RuntimeActivationPolicy to true on the startup node, so it looks similar to this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true" >
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
Note that during debugging, you can get an exception "Managed Debugging Assistant 'LoaderLock' has detected a problem". You can ignore this exception. Tell Visual Studio that it should not stop at this exception during debugging.
Related question/answer
Codec support
The second issue is that you need to have a codec installed that allows DirectX to play mp4 files.
These codecs are not included by default in most versions of Windows (also not in Windows 10). Even when your Windows Media Player can playback an mp4 file, this doesn't mean the correct codec is available that can be used from DirectX.
I found that installing the LAV filters is a simple and non-intrusive way of making most video formats available to DirectX on Windows.
Video Playback in Debug Mode
You will find that often when you start the application from Visual Studio, video playback will be choppy and have a low quality. Quality will be perfect when starting the application without debugging.
I am trying to launch my Selenium Launch when a button is press on my windows form. I am unsure on how to link the class library with the Windows Form. I am getting stuck because for the script/class to run correctly the project Output Type must be set to "Class Library" otherwise it gives out loads of errors.
Here is the class I am trying to launch :
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void TestMethod1()
{
// Set what browser to use
ChromeDriver driver = new ChromeDriver(#"C:\Users\Alex\Documents\Selenium");
// Set the base website
string baseURL = "http://kd.svr-webdev-01.df.local";
driver.Navigate().GoToUrl(baseURL + "/");
driver.Close();
}
}
}
All you need to do is put your unit test (TestMethod1) body into a button click event. Easiest way to make that event is just to drop a button onto the form using the designer and double click it.
Visual Studio will create a blank event for you, and you just need to copy/paste your current code into there.
Even, to make it simpler, create a console application and stick that body into the the "Main" method.
'Random_Number_File_Writer.Form1' does not contain a definition for 'saveFileDialog1_FileOk' and no extension method 'saveFileDialog1)_FileOk' accepting a first argument of type 'Random_Number_File_Writer.Form1' could be found (are you missing a using directive or an assembly reference?)
That, is the error message that I am getting. I tried going to my college's lab for assistance, but the person is not familiar with C# and it took us around an hour just to get my line numbers showing (just for reference...) And then I went to my professor and he said he was going to be busy for a while.. So I thought I'd try here as an alternate source of help.
I looked at the questions already on here regarding similar errors, but it still leaves me puzzled as how to correct this one in particular, and as I referenced the code in my textbook as closely as possible, I'm not sure I understand why I'm even getting this error.
Here's the code, and I'm sorry if it's difficult to read. Oh, and I know that this is the part generating the error, because I had it running yesterday, WITHOUT this part. But part of the assignment is having the save as dialog.
try
{
//Initial opening point for save file dialogue
saveFileDialog1.InitialDirectory = #"C:\Users\Heather\Documents\Visual Studio 2010\Projects\Random Number File Writer";
//Save As popup - Opening the file for writing usage once it's created.
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
randomNumberFile = File.CreateText(openFileDialog1.FileName);
}
else // Popup informing user that the data will not save to a file because they didn't save.
{
MessageBox.Show("You elected not to save your data.");
}
here's the using stuff that didn't format:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; // Added to be able to use StreamWriter variable type
And here is the code snippet it gives when I double click and it takes me to the Form1.Designer.CS window.
this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
saveFileDialog1_FileOk Looks like it's supposed to be an event handler( a method) so make sure that you have a method like
public void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
in your Form1 class
I added to the reference the OpenHardwareMonitorLib.dll
Now I added in my code: using OpenHardwareMonitor.Hardware;.
Then I did in the top form level: Isensor Sensor;
But I can't "new" it i cant create an instance of it and I'm getting null exception on it in the constructor:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ISensor Sensor;
public Form1()
{
InitializeComponent();
string t = Sensor.Name;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Cannot create an instance of the abstract class or interface 'OpenHardwareMonitor.Hardware
I tried ot look in the source code in the code.google.com site: http://code.google.com/p/open-hardware-monitor/source/browse/#svn%2Ftags%2F0.3.2%2FWMI
But i dont want to use all this code. I downloaded the program Open Hardware Monitor and except the exe file there is a dll file im using now in my project i was sure i can use the dll to use it easier. All i want for now is to get the temperature of the video card gpu.
First, you can't new interfaces. You can only new concrete classes.
Second, I recommend renaming your variable from Sensor to sensor, or _sensor, or something along these lines. There is a Sensor class. It's best to avoid confusion.
What I did was downloaded the DLL and opened it up in ILSpy. Let's see what classes implement this interface. I urge you to download ILSpy and try this out for yourself.
Here's the resulting ILSpy window. Now in the bottom right I had done an "Analyze" on the interface to see where it is exposed. There don't appear to be any factory methods that return an ISensor.
Back over on the left side, we see that one class implements ISensor: the Sensor class. This class has four constructors. These will come up in intellisense in Visual Studio, or if you navigate to the Sensor class in ILSpy you can see the constructors.
What you'll end up needing to do is sensor = new Sensor(...); As for which constructor you use... that's up to you.
So i have simple application, just a few lines:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;
namespace asdasd
{
public partial class Form1 : Form
{
public Device joystick;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (
DeviceInstance di in
Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
throw new Exception("No joystick found.");
}
}
}
}
and i try to get the active joystick on my computer, but i get error:
i have the assembly Microsoft.DirectX.DirectInput and i have directX SDK 2010 installed.
Can someone tell me where is the problem?
Try adding this to the config file:
http://devonenote.com/2010/08/mixed-mode-assembly-error-after-upgrading-to-dotnet-4-0/
(if configuration already exists, just merge these in)
And, maybe it's not the right place, but just take a look at XNA... Things are usually much easier with that.
I couldn't paste the XML directly here, it doesn't show up.
The DirectX assemblies are built against .NET v1.1 Microsoft stopped actively developing them before .NET v2.0 was released.
They cannot be used in projects targeting other than .NET v1.1. XNA is the "blessed" path forward for managed access to Direct X features. I don't know all if it's features, but SlimDX appears to give a more Direct X feeling API for C# than XNA, though I have not used it, I've heard a lot about it.
You might find better responses for chosing an upgrade path over at gamedev.stackexchange.com though.