VioletaBabel

18일 : mp3 플레이어 본문

BCA/3. C# WPF
18일 : mp3 플레이어
Beabletoet 2018. 3. 12. 13:45
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
<!-- MainWindow.xaml -->
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!-- Content는 이름. HorizontalAlignment는 좌우정렬, VerticalAlignment는 수직정렬, Margin은 왼쪽/위/오른쪽/아래 순으로 여백, Width는 너비, Height는 높이-->
        <Button x:Name="Play" Content="Play" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="50" Height="50" Click="Play_Click"/>
        <Button x:Name="Stop" Content="Stop" HorizontalAlignment="Left" Margin="65,10,0,0" VerticalAlignment="Top" Width="50" Height="50" Click="Stop_Click"/>
        <Button x:Name="Pause" Content="Pause" HorizontalAlignment="Left" Margin="120,10,0,0" VerticalAlignment="Top"  Width="50" Height="50" Click="Pause_Click"/>
        <Button x:Name="Resume" Content="Resume" HorizontalAlignment="Left" Margin="175,10,0,0" VerticalAlignment="Top"  Width="50" Height="50" Click="Resume_Click"/>
        <Slider x:Name="Volume" HorizontalAlignment="Left" Margin="120,99,0,0" VerticalAlignment="Top" ValueChanged="Volume_ValueChanged" Height="24" Width="251" IsMoveToPointEnabled="True"/>
        <Label x:Name="VolumeValue" Content="a" HorizontalAlignment="Left" Margin="65,99,0,0" VerticalAlignment="Top"/>
        <Slider x:Name="Timeline" HorizontalAlignment="Left" Margin="120,177,0,0" VerticalAlignment="Top" Height="22" Width="251" ValueChanged="Timeline_ValueChanged" IsMoveToPointEnabled="True"/>
        <Label x:Name="TimeValue" Content="b" HorizontalAlignment="Left" Margin="64,173,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.449,0"/>
        <!-- 바꾸려는 애를 클릭 후 속성 탭의 레이아웃에서 많은 것을 쉽게 바꿀 수 있다.-->
        <!-- 속성 탭의 번개를 누르면 그 버튼이 사용할 수 있는 이벤트의 목록이 나옴-->
 
    </Grid>
</Window>
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
 
namespace WpfApp2
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        private MediaPlayer player = new MediaPlayer();
        private bool play = false;
        private bool pause = false;
        private int m = 0;
        private int s = 0;
        delegate void Work();
        Thread t1;
        public MainWindow()
        {
            InitializeComponent();
            Volume.Value = 50.0f;
            Volume.Maximum = 100.0f;
            Volume.Minimum = 0;
            VolumeValue.Content = Convert.ToString((int)Volume.Value);
            TimeValue.Content = Convert.ToString((int)m) + ":0" + Convert.ToString((int)s);
            t1 = new Thread(new ThreadStart(TimeCheck));
        }
        private void Play_Click(object sender, RoutedEventArgs e)
        {
            //player.Open(new Uri("C:/Users/user/source/repos/WpfApp2/WpfApp2/150708_extreme_race.mp3"));
 
            if (!play)
            {
                //프로젝트폴더의 bin폴더 안의 debug 폴더에 음악파일을 두면 이렇게도 불러올 수 있음.
                //위는 절대경로, 아래는 상대경로인 셈.
                player.Open(new Uri("150708_extreme_race.mp3", UriKind.Relative));
                player.Play();
                play = true;
                player.MediaEnded += endPlay;//람다 식으로 노래가 끝났을경우 시작
                while (true)
                    if (player.NaturalDuration.HasTimeSpan)
                    {
                        Timeline.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
                        break;
                    }
                Play.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Work(TimeCheck));
            }
        }
 
        private void TimeCheck()
        {
            if (play && !pause)
            {
                Play.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
                {
                    while (true)
                        if (player.NaturalDuration.HasTimeSpan)
                        {
                            double p = player.NaturalDuration.TimeSpan.TotalSeconds;
                            Timeline.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
                            break;
                        }
                }));
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate 
                {
                    m = (int)player.Position.Minutes;
                    s = (int)player.Position.Seconds;
                    Timeline.Value = (double)(m * 60 + s);
                }));
                
                TimeSpan ts = new TimeSpan(00, m, s, 0);
                string sec;
                if (s > 9)
                    sec = Convert.ToString(s);
                else
                    sec = "0" + Convert.ToString(s);
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
                {
                    TimeValue.Content = Convert.ToString((int)m) + ":" + sec;
                }));
                Play.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Work(TimeCheck));//play가 true이니 다시 실행
            }
        }
 
        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            if (play)
            {
                player.Stop();
                pause = false;
                play = false;
                Timeline.Value = 0;
                TimeValue.Content = "0:00";
            }
        }
 
        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            if (play)
            {
                player.Pause();
                pause = true;
            }
        }
 
        private void Resume_Click(object sender, RoutedEventArgs e)
        {
            if (pause)
            {
                player.Play();
                pause = false;
                Play.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Work(TimeCheck));
            }
        }
 
        private void endPlay(object sender, EventArgs e)
        {//노래가 완전히 끝났을 경우.
            play = false;
            pause = false;
            Timeline.Value = 0;
            TimeValue.Content = "0:00";
        }
 
        private void Volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            player.Volume = Volume.Value / 100.0f;
            VolumeValue.Content = Convert.ToString((int)Volume.Value);
        }
 
        private void Timeline_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (play)
            {
                int t = (int)Timeline.Value;
                m = t / 60;
                s = t % 60;
                TimeSpan ts = new TimeSpan(00, m, s, 0);
                player.Position = ts;
                string sec;
                if (s > 9)
                    sec = Convert.ToString(s);
                else
                    sec = "0" + Convert.ToString(s);
                TimeValue.Content = Convert.ToString((int)m) + ":" + sec;
            }
            else
            {
                Timeline.Value = 0;
                TimeValue.Content = "0:00";
            }
        }
    }
}
 
cs

MediaOpened, MediaEnded 같은 이벤트 함수들은

player.MediaOpened += loadMusiced; 같은 식으로 함수를 만들어줄 수 있다. (물론 loadMusiced를 만들어줘야 함.)

'BCA > 3. C# WPF' 카테고리의 다른 글

33. 타워디펜스에 사용한, 간단한 길 찍는 툴  (0) 2018.05.03
30. 연출용 툴  (0) 2018.04.27
20일 : system.net.json  (0) 2018.03.15
19일 : 캔버스에 도형 그리기  (0) 2018.03.13
17일 : C# 윈도우 폼 입문, 계산기  (0) 2018.03.09
Comments