Свой UI на WMPLib.WMPlayer

  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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WindowsMediaPlayer myPlayer = new WindowsMediaPlayer(); //создаётся плеер
//WMPLib.IWMPPlaylist playlist = WMP.playlistCollection.newPlaylist("CoVKA");
//WMPLib.IWMPMedia media;
//for (int i = 1; i < 28; i++)
// {
// media = player.newMedia(music[i].Url.ToString());
// media.setItemInfo("name", String.Format("{0} - {1}", music[i].Artist, music[i].Title)); //E_ACCESSDENIED
// playlist.appendItem(media);
// }
double Player_file_position = 0;
string Player_file_current_file = null;
Timer myTimer = new Timer();
bool isFileChanged()
{
return this.listBox1.SelectedItem.ToString() != this.Player_file_current_file;
}
private void Form1_Load(object sender, EventArgs e)
{
FolderBrowserDialog of = new FolderBrowserDialog();
of.RootFolder = Environment.SpecialFolder.Desktop;
of.Description = "Choose mp3 file!";
of.ShowNewFolderButton = false;
string Path;
if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK) Path = of.SelectedPath;
else Path = "C:\\";
System.IO.DirectoryInfo df = new System.IO.DirectoryInfo(Path);
System.IO.FileInfo[] fiArr = df.GetFiles("*.mp3");
foreach (var file in fiArr)
{
this.listBox1.Items.Add(Path + "\\" + file);
}
//this.button1.Click += new System.EventHandler(this.button1_Click);
this.myTimer.Interval = 1000;
this.myTimer.Tick += new EventHandler(tick);
}
private void tick(Object sender, EventArgs e)
{
if (this.trackBar2.Value == this.trackBar2.Maximum) this.myTimer.Stop();
else this.trackBar2.Value++;
this.label2.Text = this.trackBar2.Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (Player_file_current_file == null && this.listBox1.SelectedItem == null)
{
this.myPlayer.URL = this.listBox1.Items[0].ToString(); //путь к файлу присваиваем проигрывателю
this.Player_file_current_file = this.myPlayer.URL;
this.myPlayer.controls.play(); //воспроизводим
}
if (this.listBox1.SelectedItem != null && Player_file_current_file == null)
{
this.myPlayer.URL = this.listBox1.SelectedItem.ToString(); //путь к файлу присваиваем проигрывателю
this.Player_file_current_file = this.myPlayer.URL;
this.myPlayer.controls.play(); //воспроизводим
}
if (myPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
if (isFileChanged())
{
this.myPlayer.URL = this.listBox1.SelectedItem.ToString(); //путь к файлу присваиваем проигрывателю
this.Player_file_current_file = this.myPlayer.URL;
this.myPlayer.controls.play(); //воспроизводим
}
}
if (myPlayer.playState == WMPLib.WMPPlayState.wmppsPaused)
{
this.myPlayer.controls.currentPosition = Player_file_position;
this.myPlayer.controls.play(); //воспроизводим
}
if (myPlayer.playState == WMPLib.WMPPlayState.wmppsStopped)
{
this.myPlayer.controls.play(); //воспроизводим
}
this.myPlayer.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(myPlayer_StateChange);
this.myPlayer.PositionChange += new _WMPOCXEvents_PositionChangeEventHandler(myPlayer_PositionChange);
// this.myPlayer.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(myPlayer_PlayStateChange);
this.myTimer.Start();
}
private void myPlayer_PlayStateChange(int state)
{
label2.Text = state.ToString();
}
private void myPlayer_StateChange(int state)
{ //this.Text += " " + state.ToString();
switch (state)
{
case 0: break;
case 1: this.label1.Text = "00:00"; break;
case 2:
case 3:
{
this.label1.Text = this.myPlayer.currentMedia.duration.ToString();//currentPlaylist.Item[0].getItemInfoByAtom(1);
this.trackBar2.Maximum = (int)this.myPlayer.currentMedia.duration;
break;
}
default: break;
}
}
private void myPlayer_PositionChange(double oldPosition, double newPosition)//(object sender, _WMPOCXEvents_PositionChangeEventHandler e)
{
this.Text = oldPosition.ToString() + " | " + newPosition.ToString();
this.label2.Text = newPosition.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
if (this.myPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
Player_file_position = myPlayer.controls.currentPosition;
this.myPlayer.controls.pause();
this.myTimer.Stop();
}
}
private void button3_Click(object sender, EventArgs e)
{
this.myPlayer.controls.stop(); //останавливаем воспроизведение
}
private void button4_Click(object sender, EventArgs e)
{
this.myPlayer.controls.previous();
}
private void button4_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.myPlayer.controls.fastReverse();
}
}
private void button4_MouseUp(object sender, MouseEventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
this.myPlayer.settings.volume = this.trackBar1.Value;
this.Text = (myPlayer.settings.volume.ToString() + " | " + this.trackBar1.Value);
//this.button1_Click(sender, e);
}
private void button6_Click(object sender, EventArgs e)
{
if (myPlayer.settings.volume < 100)
{
myPlayer.settings.volume++;
}
}
private void button7_Click(object sender, EventArgs e)
{
if (myPlayer.settings.volume > 0)
{
myPlayer.settings.volume--;
}
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
this.myPlayer.controls.currentPosition = this.trackBar2.Value;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = this.listBox1.SelectedItem.ToString();
this.axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}