How to get media player in C# to recognise my path? - c#

I'm trying to get the Media Player to play my audio files, but it always returns the error "System.UriFormatException", even though I have provided the right path.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private MediaPlayer m_mediaPlayer;
public void Play(string filename)
{
m_mediaPlayer = new MediaPlayer();
m_mediaPlayer.Open(new Uri(filename));
m_mediaPlayer.Play();
}
private void m_btnSound_Click(object sender, EventArgs e)
{
Play("Sound.wav");
}
}

Related

Struggling to add to ListBox

So I'm making this small program for my assignment at university and I'm finding it hard to add to my list in my form. Here is my code:
public partial class WorkOutBeam : Form
{
Check checkLib;
public BindingList<ListBox> list;
public WorkOutBeam()
{
InitializeComponent();
}
public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName();
forceItem.Show();
}
public void AddToForceList(string name)
{
list.Items.Add(name);
}
}
NewForceName class below:
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (NewForceNames.Text != "")
{
ReferToLibs();
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}
private void NewForceName_Load(object sender, EventArgs e)
{
}
}
So I say to my program, "give me a new force." When it does, it initializes a new form of "NewForceName." I type into a text box and click 'Ok', this starts a public method shown below:
The list is a binding list which refers to the listBox as a data source. However the program tells me that the Items part is inaccessible due to its protection but I don't know how to add it as public. I tried looking in the properties of my listBox but to no avail.
Give this a shot:
public partial class WorkOutBeam : Form
{
Check checkLib;
// public BindingList<ListBox> list; // get rid of this for now
public WorkOutBeam()
{
InitializeComponent();
}
/*public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}*/
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName(this); // pass a reference to this
// instance of WorkoutBeam
forceItem.Show();
}
public void AddToForceList(string name)
{
// we should do some more things here, but let's keep it simple for now
listBox1.Items.Add(name);
}
}
And
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
{
InitializeComponent();
workoutBeam = beam;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewForceNames.Text))
{
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
// DO NOT create new WorkoutBeams every time. Use the original.
/*private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}*/
}
Disclaimer: I did not address each and every problem in this code. This is just enough so that it should "work" as intended.

How do I access a usercontrol instance in another usercontrol instance?

I have two UserControls: Player and Playlist.
Player.cs
namespace Music_Player
{
public partial class Player : UserControl
{
public int currentSongIndex = 0;
public WindowsMediaPlayer player = new WindowsMediaPlayer();
public Player()
{
InitializeComponent();
player.settings.autoStart = false;
}
private void playButton_Click(object sender, EventArgs e)
{
if (playButton.Text == "Play")
{
Play();
}
else
{
Pause();
}
}
public void Play()
{
if (CheckSongs()) { return; }
playButton.Text = "Pause";
player.controls.play();
}
...
Playlist.cs
namespace Music_Player
{
public partial class Playlist : UserControl
{
public Playlist()
{
InitializeComponent();
}
private void playlistTitle_Click(object sender, EventArgs e)
{
player.Play(); // THIS IS WHERE I WANT TO USE THE player INSTANCE OF Player()
}
...
The UserControls are both children of another UC which is in my Form.
How can I use the player which is an instance of Player() inside Playlist?
Suppose you have added a player1 and a playList1 to the form.
To call the method in another UserControl, you can define a property in Form.cs to access the player1 instance.
public Player PlayerInstance
{
get { return player1; }
}
Then you can call Player.Play() in Playlist.cs via the code as followed.
private void playlistTitle_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.FindForm();
form1.PlayerInstance.Play();
}
You probably want to handle events in the form the controls are in, not within the control itself. The form would have a reference to both controls and likely whatever they are controlling. That's where things should be connected.

Motorola BarCode scan not working

I am trying to write a small application to read BarCode using Motorola MC5040 Symbol device. Clicking on a button on form should read BarCode.
I am having hard time finding any sample projects. I reference Symbol and Symbol.Barcode DLLs
Here is the code that is not working. Not sure how to control the side buttons on device either.
public partial class Form1 : Form
{
public static Symbol.Barcode.Reader SymbolReader = new Reader();
public static Symbol.Barcode.ReaderData SymbolReaderData = new ReaderData(ReaderDataTypes.Text, 100);
public static System.EventHandler SymbolEventHandler = null;
public Form1()
{
InitializeComponent();
InitScanner();
}
public void InitScanner()
{
SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
SymbolReader.Actions.Enable();
}
public void SymbolReader_ReadNotify(object sender, EventArgs e)
{
SymbolReader.Actions.Enable();
Symbol.Barcode.ReaderData TheReaderData = SymbolReader.GetNextReaderData();
if (TheReaderData.Result == Symbol.Results.SUCCESS )
{
txtBarcode.Text = TheReaderData.Text.ToString();
SymbolReader_CycleScannerReader();
return;
}
SymbolReader_CycleScannerReader();
}
public void SymbolReader_CycleScannerReader()
{
SymbolReader.Actions.Read(SymbolReaderData);
}
private void button1_Click(object sender, EventArgs e)
{
SymbolReader_ReadNotify(sender, e);
}
}
}
Any pointers or correction will be great.
Here is a sample application using the Symbol.Barcode2 library
https://github.com/bigfont/2013-128CG-Vendord/blob/master/HelpfulStuff/CS_Barcode2Sample1/API.cs
if you initialize a Barcode2 object you can then use that object to capture scan data
var myBarcode2Obj = new Barcode2();
myBarcode2Obj.OnScan += //Your scan even here;

How do I call a public methods in my 'public class' (as c# module) in c#?

There are only (3) things I need to know in here.
Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:
public partial class frm_main : Form
{
public_class pc = new public_class();
public frm_main()
{
InitializeComponent();
this.Load += new System.EventHandler(frm_main_Load);
}
private void close_button_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_main_Load(object sender, EventArgs e)
{
pc.screen_adjust(close_button);
}
}
And here's my public_class.cs code:
public partial class public_class
{
public int cl_b;
public void screen_adjust(Button b)
{
cl_b = frm_main.ActiveForm.Width;
frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
b.Left += frm_main.ActiveForm.Width - cl_b;
}
}
The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:
Did I do a correct "VB.net module" in C# properly?
How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...
Thanks!

Playing two sounds simultaneously c#

I am creating a WP7 application that requires various sound effects to be played (on button press) over looped background music. The background music is initiated by pressing Button 1 and loops fine. When I press button3 (triggers a sound effect), the sound effect overlays on the background music fine on first press. However, when I press button3 again, the background music stops. I cannot figure out why this might be happening!? I have pasted the relevant portions of code below. Would appreciate any help.
public partial class MainPage : PhoneApplicationPage
{
SoundEffect soundEffect;
Stream soundfile;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
{
SoundEffectInstance instance = soundEffect.CreateInstance();
instance.IsLooped = true;
FrameworkDispatcher.Update();
instance.Play();
}
}
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
soundfile = TitleContainer.OpenStream("BackgroundMusic.wav");
soundEffect = SoundEffect.FromStream(soundfile);
LoopClip(soundEffect);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
}
This should work if you are always working with Instances so change your code to this and it should clear up the problem:
public partial class MainPage : PhoneApplicationPage
{
SoundEffectInstance loopedSound = null;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
loopedSound = soundEffect.CreateInstance();
loopedSound.IsLooped = true;
loopedSound.Play();
}
public void PlaySound(string soundFile)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(soundFile, UriKind.Relative)).Stream);
SoundEffectInstance instance = sound.CreateInstance();
instance.Play();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(#"BackgroundMusic.wav", UriKind.Relative)).Stream);
LoopClip(sound);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
The above example assumes your sound files are set with Build Action = Content and are in the top level directory.
You will need to play each sound from a separate thread.
What seems to be happening here is that the different Play method calls are interfering with each other since they are in the same thread.
Try just putting the background music in a separate thread and see if that solves the problem you mention in the question. If so, split the others out as well.

Categories

Resources