IHateMyselfx

Form1.cs

May 25th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.59 KB | None | 0 0
  1. using NAudio.Wave;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection.Emit;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
  15.  
  16. namespace MuiscPlayer
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         public List<ZeneClass1> songs = new List<ZeneClass1>();
  21.         private bool isPlaying = false;
  22.         private long currentSample = 0;
  23.         private WaveOutEvent waveOut = new WaveOutEvent();
  24.         private WaveChannel32 waveStream;
  25.         private int currentSongIndex = 0; // Az aktuális zene indexe
  26.  
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.  
  31.             // Új szál létrehozása a FolderBrowserDialog futtatásához
  32.             Thread folderThread = new Thread(SelectFolder);
  33.             folderThread.SetApartmentState(ApartmentState.STA);
  34.             folderThread.Start();
  35.         }
  36.  
  37.         private void SelectFolder()
  38.         {
  39.             using (var dialog = new FolderBrowserDialog())
  40.             {
  41.                 DialogResult result = dialog.ShowDialog();
  42.  
  43.                 if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(dialog.SelectedPath))
  44.                 {
  45.                     // Zenék betöltése a mappából és hozzáadása a listához
  46.                     ZeneClass1.LoadSongsFromFolder(dialog.SelectedPath, songs);
  47.                 }
  48.                 // Form megjelenítése a fő szálon
  49.                 Invoke((Action)(() =>
  50.                 {
  51.                     Show();
  52.                     label1.Text = $"{songs[0]}";
  53.                 }));
  54.             }
  55.         }
  56.  
  57.         private void button3_Click(object sender, EventArgs e)
  58.         {
  59.             if (!isPlaying)
  60.             {
  61.                 isPlaying = true;
  62.                 button3.Text = "Stop";
  63.  
  64.                 Task.Run(() =>
  65.                 {
  66.                     if (waveStream == null || waveStream.Position == waveStream.Length)
  67.                     {
  68.                         // Ha a waveStream null vagy a végére értünk, újra betöltjük a zenét
  69.                         if (songs.Count > 0)
  70.                         {
  71.                             var audioFile = new AudioFileReader(songs[0].FilePath);
  72.                             waveStream = new WaveChannel32(audioFile);
  73.                             currentSample = 0;
  74.                             waveOut.Init(waveStream); // Inicializáció
  75.                         }
  76.                     }
  77.                     else
  78.                     {
  79.                         // Pozíció visszaállítása az előző lejátszás helyére
  80.                         waveStream.Position = currentSample;
  81.                     }
  82.  
  83.                     waveOut.Play(); // Lejátszás
  84.  
  85.                     while (waveOut.PlaybackState == PlaybackState.Playing)
  86.                     {
  87.                         currentSample = waveStream.Position;
  88.                         System.Threading.Thread.Sleep(100);
  89.                         // Zene aktuális pozíciójának és hosszának meghatározása
  90.                         var currentTime = waveStream.CurrentTime.TotalSeconds;
  91.                         var totalTime = waveStream.TotalTime.TotalSeconds;
  92.  
  93.                         // ProgressBar Value értékének beállítása az aktuális lejátszási pozíció alapján
  94.                         try
  95.                         {
  96.                             Invoke((Action)(() =>
  97.                             {
  98.                                 progressBar1.Value = (int)((currentTime / totalTime) * 100);
  99.                                 // Várakozás a következő frissítésig
  100.                                 //System.Threading.Thread.Sleep(1);
  101.                             }));
  102.                         }
  103.                         catch (ObjectDisposedException ex)
  104.                         {
  105.                             Console.WriteLine($"Error_Debugging: {ex.Message}");
  106.                             //throw new Exception();
  107.                         }
  108.                      
  109.  
  110.                         if (currentTime >= totalTime)
  111.                         {
  112.                             // Ha a zene végére értünk, akkor visszaállítjuk a ProgressBar-t 0-ra
  113.                             Invoke((Action)(() =>
  114.                             {
  115.                                 progressBar1.Value = 0;
  116.                             }));
  117.  
  118.                             currentSongIndex++; // Növeljük az aktuális zene indexét
  119.  
  120.                             if (currentSongIndex < songs.Count)
  121.                             {
  122.                                 // Betöltjük a következő zenét az aktuális zene indexével
  123.                                 var audioFileNext = new AudioFileReader(songs[currentSongIndex].FilePath);
  124.                                 waveStream = new WaveChannel32(audioFileNext);
  125.                                 currentSample = 0;
  126.  
  127.                                 // pause - resume
  128.                                 waveOut.Stop();
  129.                                 waveOut.Dispose();
  130.                                 waveOut = new WaveOutEvent();
  131.                                 waveOut.Init(waveStream);
  132.                                 Invoke((Action)(() =>
  133.                                 {
  134.                                     label1.Text = $"{songs[currentSongIndex]}";
  135.                                 }));
  136.                                
  137.                                 waveOut.Play();
  138.                             }
  139.                             else
  140.                             {
  141.                                 // ha nincs tobb zene a lsitaban.
  142.                                 Invoke((Action)(() =>
  143.                                 {
  144.                                     button3.Text = "Play";
  145.                                     isPlaying = false;
  146.                                 }));
  147.                                 break;
  148.                             }
  149.                         }
  150.                     }
  151.  
  152.                     // pozicio tarolasa
  153.                     if (waveOut.PlaybackState == PlaybackState.Stopped)
  154.                     {
  155.                         currentSample = waveStream.Position;
  156.                     }
  157.  
  158.                     Invoke((Action)(() =>
  159.                     {
  160.                         button3.Text = "Play";
  161.                         isPlaying = false;
  162.                     }));
  163.                 });
  164.             }
  165.             else
  166.             {
  167.                 waveOut.Pause();
  168.                 button3.Text = "Play";
  169.                 isPlaying = false;
  170.             }
  171.         }
  172.  
  173.         private void button2_Click(object sender, EventArgs e)
  174.         {
  175.             if (currentSongIndex + 1 < songs.Count)
  176.             {
  177.                 currentSongIndex++; // Növeljük az aktuális zene indexét
  178.             }
  179.             else
  180.             {
  181.                 currentSongIndex = 0; // Ha elértük a lista végét, állítsuk vissza 0-ra
  182.             }
  183.  
  184.             var audioFileNext = new AudioFileReader(songs[currentSongIndex].FilePath);
  185.             waveStream = new WaveChannel32(audioFileNext);
  186.             currentSample = 0;
  187.  
  188.             waveOut.Stop();
  189.             waveOut.Dispose();
  190.             waveOut = new WaveOutEvent();
  191.             waveOut.Init(waveStream);
  192.  
  193.             label1.Text = $"{songs[currentSongIndex]}";
  194.  
  195.             waveOut.Play();
  196.  
  197.             Invoke((Action)(() =>
  198.             {
  199.                 progressBar1.Value = 0; // Reseteld a ProgressBar-t
  200.             }));
  201.         }
  202.  
  203.         private void button1_Click(object sender, EventArgs e)
  204.         {
  205.  
  206.             if (currentSongIndex - 1 > songs.Count)
  207.             {
  208.                 currentSongIndex--; // csokkentjük az aktuális zene indexét
  209.             }
  210.             else
  211.             {
  212.                 currentSongIndex = 0; // Ha elértük a lista végét, állítsuk vissza 0-ra
  213.             }
  214.  
  215.             var audioFileNext = new AudioFileReader(songs[currentSongIndex].FilePath);
  216.             waveStream = new WaveChannel32(audioFileNext);
  217.             currentSample = 0;
  218.  
  219.             waveOut.Stop();
  220.             waveOut.Dispose();
  221.             waveOut = new WaveOutEvent();
  222.             waveOut.Init(waveStream);
  223.  
  224.             label1.Text = $"{songs[currentSongIndex]}";
  225.  
  226.             waveOut.Play();
  227.  
  228.             Invoke((Action)(() =>
  229.             {
  230.                 progressBar1.Value = 0; // Reseteld a ProgressBar-t
  231.             }));
  232.         }
  233.     }
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment