■ Windows Media Player COM 객체를 사용해 MP3 파일을 재생하는 방법을 보여준다.
▶ MainForm.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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Private #region 재생 상태 /// <summary> /// 재생 상태 /// </summary> private enum PlayStatus { /// <summary> /// 재생 /// </summary> Play, /// <summary> /// 중단 /// </summary> Stop, /// <summary> /// 중지 /// </summary> Pause } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 미디어 플레이어 /// </summary> private MediaPlayer.MediaPlayerClass mediaPlayer; /// <summary> /// 타이머 /// </summary> private Timer timer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.openFileDialog.RestoreDirectory = true; this.openFileDialog.CheckPathExists = true; this.openFileDialog.CheckFileExists = true; this.openFileDialog.Filter = "MP3 파일(*.mp3)|*.mp3"; this.openFileDialog.FilterIndex = 1; this.openFileDialog.DefaultExt = ".mp3"; this.timer = new Timer(); this.timer.Interval = 500; this.timer.Tick += timer_Tick; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { this.positionTrackBar.Minimum = 0; this.positionTrackBar.Maximum = (int)this.mediaPlayer.Duration; this.positionTrackBar.Value = (int)this.mediaPlayer.CurrentPosition; string currentPosition = TimeSpan.FromSeconds(this.mediaPlayer.CurrentPosition).ToString(@"hh\:mm\:ss"); string duration = TimeSpan.FromSeconds(this.mediaPlayer.Duration).ToString(@"hh\:mm\:ss"); this.positionLabel.Text = $"{currentPosition} / {duration}"; } #endregion #region MP3 파일 열기 버튼 클릭시 처리하기 - openButton_Click(sender, e) /// <summary> /// MP3 파일 열기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openButton_Click(object sender, EventArgs e) { if(this.openFileDialog.ShowDialog(this) == DialogResult.OK) { if(this.mediaPlayer == null) { this.mediaPlayer = new MediaPlayer.MediaPlayerClass(); } this.mediaPlayer.FileName = this.openFileDialog.FileName; this.mediaPlayer.Stop(); SetPlayStatusLabelData(PlayStatus.Stop); this.filePathTextBox.Text = this.openFileDialog.FileName; this.volumeTrackBar.Value = GetTrackBarValue(-1200, 0, this.mediaPlayer.Volume); this.volumeLabel.Text = this.volumeTrackBar.Value.ToString(); } } #endregion #region 재생 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// 재생 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, EventArgs e) { this.mediaPlayer.Play(); this.timer.Start(); SetPlayStatusLabelData(PlayStatus.Play); } #endregion #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 중단 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, EventArgs e) { this.mediaPlayer.Stop(); this.timer.Stop(); SetPlayStatusLabelData(PlayStatus.Stop); } #endregion #region 정지 버튼 클릭시 처리하기 - pauseButton_Click(sender, e) /// <summary> /// 정지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pauseButton_Click(object sender, EventArgs e) { this.mediaPlayer.Pause(); SetPlayStatusLabelData(PlayStatus.Pause); } #endregion #region 볼륨 트랙바 스크롤시 처리하기 - volumeTrackBar_Scroll(sender, e) /// <summary> /// 볼륨 트랙바 스크롤시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void volumeTrackBar_Scroll(object sender, EventArgs e) { this.volumeLabel.Text = this.volumeTrackBar.Value.ToString(); int volumn = GetVolume(-1200, 0, this.volumeTrackBar.Value); if(this.volumeTrackBar.Value == 0) { this.mediaPlayer.Mute = true; } else { this.mediaPlayer.Mute = false; this.mediaPlayer.Volume = volumn; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 트랙바 값 구하기 - GetTrackBarValue(minimumVolume, maximumVolume, volume) /// <summary> /// 트랙바 값 구하기 /// </summary> /// <param name="minimumVolume">최소 볼륨</param> /// <param name="maximumVolume">최대 볼륨</param> /// <param name="volume">볼륨</param> /// <returns>트랙바 값</returns> private int GetTrackBarValue(int minimumVolume, int maximumVolume, int volume) { double range = maximumVolume - minimumVolume; double targetValue = volume - minimumVolume; return (int)(targetValue / range * 100); } #endregion #region 볼륨 구하기 - GetVolume(minimumValue, maximumValue, value) /// <summary> /// 볼륨 구하기 /// </summary> /// <param name="minimumValue">최소값</param> /// <param name="maximumValue">최대값</param> /// <param name="value">값</param> /// <returns>볼륨</returns> private int GetVolume(int minimumValue, int maximumValue, int value) { double range = maximumValue - minimumValue; double targetVolume = range * value / 100; return (int)(targetVolume + minimumValue); } #endregion #region 재생 상태 레이블 데이터 설정하기 - SetPlayStatusLabelData(playStatus) /// <summary> /// 재생 상태 레이블 데이터 설정하기 /// </summary> /// <param name="playStatus">재생 상태</param> private void SetPlayStatusLabelData(PlayStatus playStatus) { switch(playStatus) { case PlayStatus.Play : this.playStatusTextBox.Text = "재생"; break; case PlayStatus.Stop : this.playStatusTextBox.Text = "중단"; break; case PlayStatus.Pause : this.playStatusTextBox.Text = "정지"; break; } } #endregion } } |