-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
179 lines (173 loc) · 6.61 KB
/
MainWindow.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
using System.Linq;
using System.Threading.Tasks;
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;
using AudioEngine;
using System.Collections.Generic;
namespace SharpPlayer
{
class MainWindow : Window
{
[UI] private Fixed _fixed = null;
[UI] private Label _labelFileName = null;
[UI] private Label _timePosition = null;
[UI] private Button _buttonPlay = null;
[UI] private Button _buttonPause = null;
[UI] private Button _buttonStop = null;
[UI] private Button _buttonOpen = null;
[UI] private Scrollbar _scroll = null;
[UI] private TreeView _treeView = null;
[UI] private TreeStore treestore = null;
private AudioPlayer _player;
private List<string> _playlist;
private int _currentIndex;
private bool _isPlaying;
public MainWindow() : this(new Builder("MainWindow.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("MainWindow").Handle)
{
builder.Autoconnect(this);
DeleteEvent += Window_DeleteEvent;
_buttonPlay.Clicked += ButtonPlay_Clicked;
_buttonPause.Clicked += ButtonPause_Clicked;
_buttonStop.Clicked += ButtonStop_Clicked;
_buttonOpen.Clicked += ButtonOpen_Clicked;
_scroll.ChangeValue += Scrollbar_Changed;
new Task(()=>Scrollbar_Update()).Start();
_treeView = new TreeView();
_treeView.RowActivated += OnRowActivated;
TreeViewColumn listOfFiles = new TreeViewColumn();
listOfFiles.Title = "Список воспроизведения";
treestore = new TreeStore(typeof(string));
CellRendererText cell = new CellRendererText();
listOfFiles.PackStart(cell, true);
listOfFiles.AddAttribute(cell, "text", 0);
treestore = new TreeStore(typeof(string));
_treeView.AppendColumn(listOfFiles);
_treeView.Model = treestore;
_treeView.MarginLeft = 10;
_treeView.MarginTop = 110;
_fixed.Add(_treeView);
_fixed.ShowAll();
}
private void Window_DeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
}
private void LoadFile(string fileName)
{
try
{
_player = new AudioPlayer(fileName);
if(_player.MP3Loading != null)
{
_buttonOpen.Hide();
_buttonPause.Hide();
_buttonPlay.Hide();
_buttonStop.Hide();
_scroll.Hide();
_labelFileName.Text = "Конвертация MP3 в WAV";
_player.MP3Loading.Wait();
_player.OpenBuffer();
_buttonOpen.Show();
_buttonPause.Show();
_buttonPlay.Show();
_buttonStop.Show();
_scroll.Show();
}
_labelFileName.Text = fileName;
_scroll.SetRange(0,_player.Duration);
_player.Play();
_isPlaying = true;
}
catch(Exception ex)
{
_labelFileName.Text = $"Не удалось загрузить файл : {ex.Message}";
}
}
private void ButtonPlay_Clicked(object sender, EventArgs a)
{
if(_player != null) _player.Play();
_isPlaying = true;
}
private void ButtonPause_Clicked(object sender, EventArgs a)
{
if(_player != null) _player.Pause();
_isPlaying = true;
}
private void ButtonStop_Clicked(object sender, EventArgs a)
{
if(_player != null) _player.Stop();
_isPlaying = false;
}
private void ButtonOpen_Clicked(object sender, EventArgs a)
{
Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog ("Выберете файл", null, Gtk.FileChooserAction.Open);
fcd.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
fcd.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok);
fcd.DefaultResponse = Gtk.ResponseType.Ok;
fcd.SelectMultiple = true;
Gtk.ResponseType response = (Gtk.ResponseType) fcd.Run ();
if (response == Gtk.ResponseType.Ok)
{
_playlist = fcd.Filenames.ToList();
if(_player != null)
{
_player.Stop();
_player = null;
}
new Task(() => LoadFile(_playlist.First())).Start();
_currentIndex = 0;
fcd.Dispose ();
treestore.Clear();
foreach(var item in _playlist)
{
treestore.AppendValues(item);
}
}
}
private void Scrollbar_Update()
{
while(true)
{
if((_player != null) && (_player.Duration != 0))
{
if(_isPlaying && _player.IsStopped && (_currentIndex < (_playlist.Count-1)))
{
_currentIndex++;
new Task(() => LoadFile(_playlist.ElementAt(_currentIndex))).Start();
}
var position = _player.Position;
_scroll.Adjustment.Value = position;
_timePosition.Text = $"{Minutes(position).ToString("00")}:{Seconds(position).ToString("00")}";
}
System.Threading.Thread.Sleep(1000);
}
}
private void Scrollbar_Changed(object sender, EventArgs a)
{
if(_player != null)
{
_player.Position = (float)_scroll.Adjustment.Value;
}
}
private int Minutes(double input)
{
return (int)(input/60);
}
private int Seconds(double input)
{
return (int)(input - (Minutes(input)*60));
}
private void OnRowActivated (object sender, RowActivatedArgs args)
{
_currentIndex = Convert.ToInt32(args.Args[0].ToString());
if(_player != null)
{
_player.Stop();
_player = null;
}
new Task(() => LoadFile(_playlist.ElementAt(_currentIndex))).Start();
}
}
}