axWindowsMediaPlayer Crashes Program - c#

I am trying to make a MP3 player with playlist functionality, I have googled everywhere, tried examples, but nothing works. Where the code freezes up at is when p[ListBox1.SelectedIndex + 1]; is called, and I have no idea why it locks up the program. I have tried making an array and selecting the next song through that, but that also didn't work.
I think this is the easiest way to do this. But it locks up the program for some reason.
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 System.Threading;
using System.Collections;
namespace MusicPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] f, p;
ArrayList playlist = new ArrayList();
private int current_index = 0;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){
f = openFileDialog1.SafeFileNames;
p = openFileDialog1.FileNames;
for (int i = 0; i < f.Length; i++)
{
listBox1.Items.Add(f[i]);
}
foreach (string d in open.FileNames)
{
listBox1.Items.Add(d);
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex];
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MediaPlayer_PlayStateChange);
}
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
// Locks up here
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
default:
break;
}
}
}
}
EDIT:
I updated my code and this is what it looks like:
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
default:
break;
}
}
void change_song()
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
}
But when I run this, it gets stuck at "Media Changing" and eventually does nothing.

Related

Writing to CSV File in C#

I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
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 System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.

The process cannot access the file because it is being used by another process

The code that I got was from naudio record sound from microphone then save and many thanks to Corey
This is the error message that I get when I run the code for the second or subsequent times.
The first time it runs, it runs with no issues what so ever.
If I change the file name it works perfectly.
Unable to copy file "obj\Debug\Basque.exe" to "bin\Debug\Basque.exe". The process cannot access the file 'bin\Debug\Basque.exe' because it is being used by another process. Basque
Could someone gave me some guidance to where I'm making my error
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 NAudio.Wave;
namespace Basque
{
public partial class FlashCard : Form
{
public WaveIn waveSource = null;
public WaveFileWriter waveFile = null;
public FlashCard()
{
InitializeComponent();
StopBtn.Enabled = false;
StartBtn.Enabled = true;
}
private void StartBtn_Click(object sender, EventArgs e)
{
StartBtn.Enabled = false;
StopBtn.Enabled = true;
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(44100, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
waveFile = new WaveFileWriter(#"C:\Temp\bas0001.wav", waveSource.WaveFormat);
waveSource.StartRecording();
}
private void StopBtn_Click(object sender, EventArgs e)
{
StopBtn.Enabled = false;
waveSource.StopRecording();
}
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
waveFile.Flush();
}
}
void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
StartBtn.Enabled = true;
}
private void PlayBtn_Click(object sender, EventArgs e)
{
}
private void ExitBtn_Click(object sender, EventArgs e)
{
}
}
}
It might help to put a Formclosing method with Application.Exit(); in it. If it only works on the first try, it might be because the application isn't fully closing.
You can check if this will fix it when you check task manager. Just make sure that your application isn't still there. Even if it isn't still there, the Application.Exit(); might help.

AxWMPLib.AxWindowsMediaPlayer autoplay after end of song [duplicate]

This question already has an answer here:
Function call only works when MessageBox.Show() is included?
(1 answer)
Closed 8 years ago.
I try to make simple audio player,
then I try it.
After end of song, the player stopped.
I want to set it automatically and select randomized, at the playlist.
I use URL and a ListBox as playlist..
This is the code snippet at autoplay part:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
axWindowsMediaPlayer1.Ctlenabled = true;
axWindowsMediaPlayer1.Ctlcontrols.play();
listAudio.SelectedIndex = nowPlayIndex;
}
}
But I try it then the URL changed, but not played automatically.
What is wrong with my code?
https://github.com/mudzakkir/MP3Player.git
Please help.
Ok it is now working..
I should use Timer..
My code is now like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mp3Player
{
public partial class MediaPlayer : Form
{
string HomeDir;
string[] sPlaylists, sURLs;
List<string> listURLPlayers = new List<string>();
public MediaPlayer()
{
InitializeComponent();
HomeDir = Directory.GetCurrentDirectory();
string[] playlist = File.ReadAllLines("playlist.txt");
foreach (string fileText in playlist)
{
listAudio.Items.Add(fileText);
}
string[] playlistUrl = File.ReadAllLines("playlistURL.txt");
foreach (string fileText in playlistUrl)
{
listURLPlayers.Add(fileText);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
sPlaylists = openFileDialog1.SafeFileNames;
sURLs = openFileDialog1.FileNames;
for (int i = 0; i < sPlaylists.Length; i++)
{
listAudio.Items.Add(sPlaylists[i]);
}
for (int i = 0; i < sURLs.Length; i++)
{
listURLPlayers.Add(sURLs[i]);
}
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter("playlist.txt");
foreach (var item in listAudio.Items)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
SaveFile = new System.IO.StreamWriter("playlistURL.txt");
foreach (var item in listURLPlayers)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
}
}
private void listAudio_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
timer1.Enabled = true;
}
}
private void listAudio_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
listAudio.SelectedIndex = nowPlayIndex;
}
}
}
Thank You

Working with C# Proxy List

I'm trying to build an application that goes to a certain website (from a textbox) x number of times (also from a textbox) through a proxy. What I'm having trouble with is getting the proxy list into the program and using it in the webBrowser control. Here's what I have so far. Any help is appreciated. Thanks.
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.Security.Permissions;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Net;
namespace LetsBot
{
public partial class TrafficBot : Form
{
public TrafficBot()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
this.txtURL.Clear();
this.txtProxy.Clear();
this.txtURL.Focus();
}
private void btnStart_Click(object sender, EventArgs e)
{
this.lblBrowsing.Show();
this.btnStart.Enabled = false;
int n = Convert.ToInt32(this.txtNumVisits.Text);
for (int i = 0; i < n; i++)
{
string url = this.txtURL.Text;
this.webBotBrowser.Navigate(url);
while (webBotBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string PageText = this.webBotBrowser.DocumentText.ToString();
Thread.Sleep(5000);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
this.btnStart.Enabled = true;
this.webBotBrowser.Stop();
this.txtURL.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Exit Traffic Bot?", "Don't Let Me Go!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Application.Exit();
}
else if (dialogResult == DialogResult.No)
{
}
}
private void webBotBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress > 0)
{
ProgressBar.Value = (int)(e.CurrentProgress / e.MaximumProgress * 100);
}
}
private List<string> getProxyListFromText(string input)
{
List<string> list = new List<string>();
StreamReader reader = new StreamReader(input);
string item = "";
while (item != null)
{
item = reader.ReadLine();
if (item != null)
{
list.Add(item);
}
}
reader.Close();
return list;
for (int i = 0; i < listBox1.Items.Count; i++)
{
object url;
WebClient wc;
url = this.txtURL.Text;
wc = new WebClient();
//This should come from the proxy list
foreach (string prox in getProxyListFromText("Proxies.txt"))
{
wc.Proxy = new WebProxy(prox);
var page = wc.DownloadString(url.ToString());
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var pplname = doc.DocumentNode.SelectNodes("/html/body/div[3]/div/div[2]/div[2]/div/div[4]/p");
}
}
}
}
}

I can't understand why this code is not working. (C#)

I'm new with C# and to learn, I'm watching and trying exemple from the web.
I saw this exemple:
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace MouseController
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Initialize();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
private void Initialize()
{
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
string line = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line += word.Text + " ";
}
string command = line.Trim();
switch (command)
{
case "left":
MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
break;
case "right":
MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
break;
case "up":
MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
break;
case "down":
MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
break;
}
txtOutput.Text += line;
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.UnloadAllGrammars();
recognitionEngine.LoadGrammar(CreateGrammars());
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private Grammar CreateGrammars()
{
Choices commandChoices = new Choices("left", "right", "up", "down");
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(commandChoices);
return new Grammar(grammarBuilder);
}
private void MoveMouse(int x, int y)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(x, y);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
}
}
But nothing happens when I say "up", "down", "left" or "right"...
I also tried this:
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.Speech.Recognition;
namespace SpeechRecognitionExample
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.8f)
txtOutput.Text += word.Text + " ";
}
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.LoadGrammar(new DictationGrammar());
}
private void btnStart_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private void btnStop_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
}
}
And it's not working...
In both of these codes I don't get any errors from Visual Studio.
Why isn't it working?
I checked my microphone and it's working.
Set a breakpoint at the beginning of Initialize() and also on the lambda statement where it says string line = "";. Step through each line to and watch the values of the variables. If you don't get any hits on these breakpoints, then there is something wrong with the engine settings or the input device, I would guess.
Also, you note that you are concatenating multiple words on line and then looking for a single word on the switch statement. If you get multiple words like "up up", it won't match any of your case conditions.
Let us know what you find and we might be able to help you better.

Categories

Resources