so uhh I was trying to make a media player for my first project in C#.NET (my biggest mistake)
after like straight 7 hours of stealing peoples code from other answers in other questions I just had a problem that i can’t find out how to fix it… so annoying
and I guess I know the problem but I don’t know how to fix it…
the problem is when i press the btn that starts the sound/music … it works fine but it doesn’t stop the goddam sound and starts a new music instead… any help plkz? PLKZ? PPPLLLKKKZZZ?
I’m working on this problem for straight 11:30 hours plkz help me ;^;
(I hope I didn’t made any mistake in learning the C# when I’m only 14 years old and jumping from JS and PHP to C# anyway it doesn’t matter I’m in this now ????)
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Media;
using NAudio.Wave;
using TagLib;
using System.IO;
using WindowsFormsApp1.Properties;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int playing = 0;
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
public static class GlobalFilePath
{
public static string FilePath { get; set; }
}
public void ReadMp3Metadata(string mp3FilePath)
{
// Open the MP3 file
var mp3File = TagLib.File.Create(mp3FilePath);
// Read the title (name) of the song
string title = Path.GetFileNameWithoutExtension(mp3FilePath);
label1.Text = title;
// Check if there are any pictures
if (mp3File.Tag.Pictures.Length > 0)
{
// Get the first embedded picture
IPicture pic = mp3File.Tag.Pictures[0];
// Extract image data (byte array)
byte[] imageData = pic.Data.Data;
// You can convert it to an Image object if needed
Image albumArt = Image.FromStream(new MemoryStream(imageData));
pictureBox2.BackgroundImage = albumArt;
}
// Don't forget to dispose the file to free resources
mp3File.Dispose();
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length > 0)
{
// Store the first file path globally
GlobalFilePath.FilePath = files[0];
ReadMp3Metadata(GlobalFilePath.FilePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
string path = GlobalFilePath.FilePath;
if (path != null)
{
var mp3Reader = (Mp3FileReader)null;
var waveOut = (WaveOut)null;
ReadMp3Metadata(path);
if (playing == 0)
{
waveOut.Play();
playing = 2;
Console.WriteLine(playing);
}
else if(playing == 1)
{
waveOut.Stop();
playing = 1;
Console.WriteLine(playing);
}
}
}
I tried many things many methods many other ways but non of them work
like making lot of class and functions
but still no no work
hackergod1212 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.