본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - SoundPlayer wav ( 사운드플레이어 웨이브 MP3 )

728x90
반응형
 WPF & C# - SoundPlayer wav ( 사운드플레이어 웨이브 MP3 )

 

SoundPlay.zip
0.05MB
SoundPlay.exe
0.01MB

MainWindow.xaml

1
2
3
4
5
6
<StackPanel>
    <Label x:Name="lbl" Content=""></Label>
    <Button x:Name="btnPlay" Height="25" Click="btnPlay_Click">선택 재생</Button>
    <Button x:Name="btnPlayAll" Height="25" Click="btnPlayAll_Click">전체 재생</Button>
    <ListBox x:Name="lbx" Drop="ListBox_Drop" Height="200" AllowDrop="True"></ListBox>
</StackPanel>
cs

 

MainWindow.xaml.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public MainWindow()
{
    InitializeComponent();
}
 
SoundPlayer soundPlayer = new SoundPlayer();
 
private void ListBox_Drop(object sender, DragEventArgs e)
{
    List<string> lstFile = GetDropFilesPaths(e);
    foreach (string s in lstFile)
    {
        lbx.Items.Add(s);
    }
}
 
/// <summary>
/// GetDropFilesPaths 드랍파일 가져오기
/// </summary>
/// <param name="e">DropData</param>
/// <returns>DropFiles</returns>
private List<string> GetDropFilesPaths(DragEventArgs e, bool deduplication = false)
{
    string[] eDataFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
    List<string> lstFile = new List<string>();
    foreach (string eDataFile in eDataFiles)
    {
        try
        {
            // 폴더이면 하위폴더 파일가져오기
            if ((File.GetAttributes(eDataFile) & FileAttributes.Directory== FileAttributes.Directory)
            {
                // var filesss = Directory.EnumerateFiles(eDataFile, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg"));
                var filesss = Directory.EnumerateFiles(eDataFile, "*.*", SearchOption.AllDirectories);   // 하위폴더 포함
                lstFile.AddRange(filesss);
            }
            // 파일이면 파일가져오기
            else if ((File.GetAttributes(eDataFile) & FileAttributes.Directory!= FileAttributes.Directory)
            {
                lstFile.Add(eDataFile);
            }
        }
        catch
        {
            MessageBox.Show("가져오지 못한 파일 또는 폴더가 있습니다.");
            continue;
        }
    }
    // 중복데이터제거
    if (deduplication)
        lstFile.Distinct();
    return lstFile;
}
 
Thread thread1 = null;
Thread thread2 = null;
 
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
    strPlay = "one";
    thread1 = new Thread(new ThreadStart(Play));
    thread1.Start();
}
 
string strPlay = "";
private void btnPlayAll_Click(object sender, RoutedEventArgs e)
{
    strPlay = "all";
    thread1 = new Thread(new ThreadStart(Play));
    thread1.Start();
}
 
void Play()
{
    if (strPlay == "one")
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
        {
            if (lbx.SelectedItem != null)
            {
                // UI 갱신
                a = lbx.SelectedItem.ToString();
                thread2 = new Thread(new ThreadStart(UI));
                thread2.Start();
 
                soundPlayer.SoundLocation = lbx.SelectedItem.ToString();
                soundPlayer.Play();
            }
        }));
    }
    else if (strPlay == "all")
    {
        foreach (string s in lbx.Items)
        {
            // UI 갱신
            a = s;
            thread2 = new Thread(new ThreadStart(UI));
            thread2.Start();
 
            soundPlayer.SoundLocation = s.ToString();
            soundPlayer.PlaySync();
        }
    }
}
 
string a = "";
void UI()
{
    if (strPlay == "one")
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
        {
            lbl.Content = "one Play : " + lbx.SelectedItem.ToString();
        }));
    }
    else if (strPlay == "all")
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
        {
            lbl.Content = "all Play : " + a;
        }));
    }
    thread2.Abort();
}
}
cs

 

PlaySync() 는 해당곡을 끝까지 Play 하라는 함수

UI는 Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate 도 쓰고, Forms 의 Application.DoEvents(); 도 쓰고 또 다른것도 가져다 써 봤지만 간헐적으로 UI 갱신을 놓치기에 어쩔 수 없이 별도 쓰레드를 사용 함

이후로 상당히 만족 하고 있음.

좀 더 손 봐야 하지만, 테스트용이라 이쯤에서...

 

728x90
반응형