How to play a wav file from a ListBox? - c#

Good afternoon. I'm making a small program on wpf. There is a ListBox in which wav files from a folder are loaded. There is a RadioButton (there are several of them), when you click on it, certain audio files are added to the list. But I do not know how to play the selected audio track when clicking on the wav element in the ListBox
SoundPlayer player;
string[] playList;
private void сmClassic(object sender, RoutedEventArgs e)
{
lbList.Items.Clear();
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\1395355\Desktop\Студент\C#\4_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
foreach (FileInfo fi in files)
{
lbList.Items.Add(fi.ToString());
}
string d = playList[lbList.SelectedIndex];
// player.Open(d); Yes, there is a mistake, but I was looking for an alternative
player.Play();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}

Code from answer in original source.
private void сmClassic(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\1395355\Desktop\Студент\C#\4_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
lbList.ItemsSource = files;
}
private readonly SoundPlayer player = new SoundPlayer();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
player.Stop();
if(lbList.SelectedItem is FileInfo file)
{
player.SoundLocation=file.FullName;
player.PlaySync();
}
else
{
player.SoundLocation=null;
}
}

Related

File being used by another process when using File.CopyTo method

This is the code for my windows form. The goal of the form is to be able to preview a pdf on the application and then chose a folder with a drop down that you would like to copy the file to (this is what a "house" refers to in the code). This is a little project for work to make it simpler to organize files that get sent to the company. The listBox works populating with all the pdfs and the preview window works as well (this is the axAcroPDF component). But when hitting the send button to copy the file it says the file is being used by another process.
public partial class Form1 : Form { public Form1() { InitializeComponent();
}
string selectedPDF = "";
string selectedHouse = "";
DirectoryInfo currentPDF;
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "PDF Files(*.pdf) |*.pdf;";
openFileDialog1.ShowDialog();
if(openFileDialog1.FileName != null)
{
axAcroPDF1.LoadFile(openFileDialog1.FileName);
}
}
private void refreshBTN_Click(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\bkamide\Desktop\ExamplePDFS");
FileInfo[] smFiles = dinfo.GetFiles("*.pdf");
foreach (FileInfo fi in smFiles)
{
pdfList.Items.Add(Path.GetFileName(fi.Name));
}
}
private void pdfList_SelectedIndexChanged(object sender, EventArgs e)
{
string firstSelectedItem = pdfList.SelectedItem.ToString();
selectedPDF = firstSelectedItem;
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\bkamide\Desktop\ExamplePDFS\" + firstSelectedItem);
currentPDF = dinfo;
}
private void btnSend_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
axAcroPDF1.EndInit();
var sourceFile = new FileInfo(currentPDF.FullName);
var dest = Path.Combine(#"C:\Users\bkamide\Desktop\ExamplePDFS\", selectedHouse, sourceFile.FullName);
sourceFile.CopyTo(dest, true);
}
private void cbHouse_SelectedIndex(object sender, EventArgs e)
{
selectedHouse = cbHouse.SelectedIndex.ToString();
}
}
I tried in the send method to reset the openFileDialog and axAcroPDF (not sure if I did that right) to see if those were the processes that could be using the file. I also tried restarting my computer to make sure nothing in the background was using it as well. I also did try the using method but I was not quite sure how to implement it within this.

How to save and load to preferences Windows Forms

I am making program, which copy files from directory to other directories in Windows Forms C#. But I tried save and load directory path to Preferences > Settings and its not working. Copying, deleting its already good, only this saving and loading doesnt work. I want do this: user can in textbox write path to source and target directory and save it. That when user want copy files, he dont need write path again and again because program save path in this preferences.
Form:
There are source path and target path for directory. Then "Save values" should save this paths as string. And "Save backup" copy files. Text_box1 is textbox next to "Source path" and text_box2 is "Target path"
Here is code:
namespace AutoSave
{
public partial class Form1 : Form
{
string sourcePath;
string targetPath;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var dir = new DirectoryInfo(targetPath);
if (dir.Exists)
{
DeleteDirectory(targetPath);
CopyFiles();
}
else
{
CopyFiles();
}
}
private void CopyFiles()
{
try
{
System.IO.Directory.CreateDirectory(targetPath);
var dir1 = new DirectoryInfo(sourcePath);
foreach (FileInfo file in dir1.GetFiles())
{
string targetFilePath = Path.Combine(targetPath, file.Name);
file.CopyTo(targetFilePath);
}
label2.Text = "Save is successful";
}
catch
{
label2.Text = "Something is wrong";
}
}
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//SAVE PATHS
private void button2_Click(object sender, EventArgs e)
{
sourcePath = textBox1.Text;
targetPath = textBox2.Text;
Properties.Settings.Default.SourcePath = targetPath;
Properties.Settings.Default.TargetPath = sourcePath;
Properties.Settings.Default.Save();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
Have I bad code or what I did bad please? Have you some tips? In advance I am sorry for my English i tried my best :D Thanks for help.

Stuck with WMP in C#

I want to fetch 5 media files from a Directory [not with dialog box] and play with axwindowsmediaplayer in C# one after one. Songs to be played as playlist.
I am new to C#.
private void Form1_Load(object sender, EventArgs e)
{
/*
foreach(string dir in Directory.GetDirectories(#"E:\\Project"))
{
DirectoryInfo info = new DirectoryInfo(dir);
this.listBox1.Items.Add(info.Name);
}
foreach(string file in Directory.GetFiles("E:\\Project\\Surprise"))
{
FileInfo FileInfo = new FileInfo(file);
this.listBox1.Items.Add(FileInfo.Name);
}
*/
string path = #"E:\\Project\\Surprise";
foreach (string s in Directory.GetFiles(path, "*.mp4").Select(Path.GetFileName))
listBox1.Items.Add(s);
listBox1.SelectedItems.Clear();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listBox1.SelectedItem.ToString();
axWindowsMediaPlayer1.Ctlcontrols.play();
}

ListBox1 Left Click List Duplicates

I have the following issue: Once I click on the list it duplicates, unfortunately.
The list should not duplicate when it has been clicked.
There is a refresh button for refreshing the list - this is to be used only if there is something in a specific folder.
Code: C# .NET
https://gyazo.com/f7d026a956c648a1ecfd1a749a7c3b77
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\fakepath\Something");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}
You could clear the element each time the an item is selected:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\Administrator\Desktop\Something\Something");
listBox1.Items.Clear();
// etc..

open pdf file selected in listbox

I have all the dirctories (2014, 2012), the files of each selected folder (.pdf) in the listbox 2
I get the dirctories by this code
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
DirectoryInfo[] diri_info = newDirectoryInfo(FBD.SelectedPath).GetDirectories();
foreach (DirectoryInfo diri in diri_info)
{
listBox1.Items.Add(diri);
}
and i get the files by this code
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
DirectoryInfo dirictory_choisis = (DirectoryInfo)listBox1.SelectedItem;
FileInfo[] files = dirictory_choisis.GetFiles();
listBox2.Items.Clear();
foreach (FileInfo file in files)
{
listBox2.Items.Add(file);
}
}
else
{
MessageBox.Show("selectioner un dossier");
}
}
Now how I can open the selected file (.pdf) ?
i use this code but dosn't work ( throw an exception file dosn't found)
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo) listBox2.SelectedItem;
Process.Start(file.Name);
}
There is a syntax error in your code: "newDirectoryInfo"
By the way, file.Name only returns the name (not including path). You should replace that line with:
Process.Start(file.FullName);
So the listBox2_Click should be like this:
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)listBox2.SelectedItem;
Process.Start(file.FullName);
}

Categories

Resources