Skip to content

Commit

Permalink
complete player slider function
Browse files Browse the repository at this point in the history
  • Loading branch information
YexuanXiao committed May 24, 2023
1 parent b039a49 commit 00e093d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
28 changes: 28 additions & 0 deletions src/PlayerViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace winrt::Player::implementation
{
PlayerViewModel::PlayerViewModel() {
volume_ = SettingsHelper::GetVolume();
position_ = 0.;
duration_ = 0.;
}
double PlayerViewModel::Volume()
{
Expand All @@ -16,6 +18,24 @@ namespace winrt::Player::implementation
uint32_t PlayerViewModel::Index() {
return index_;
}
double PlayerViewModel::Position() {
return position_;
}
void PlayerViewModel::Position(double value) {
if (value != position_) {
position_ = value;
propertyChanged_(*this, winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Position" });
}
}
double PlayerViewModel::Duration() {
return duration_;
}
void PlayerViewModel::Duration(double value) {
if (value != duration_) {
duration_ = value;
propertyChanged_(*this, winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Duration" });
}
}
void PlayerViewModel::Index(uint32_t value) {
if (value != index_) {
index_ = value;
Expand All @@ -32,6 +52,14 @@ namespace winrt::Player::implementation
winrt::hstring PlayerViewModel::DoubleToString(double value) {
return fast_io::wconcat_winrt_hstring(value);
}
winrt::hstring PlayerViewModel::DurationToString(double value) {
auto const hms{ std::chrono::hh_mm_ss{std::chrono::nanoseconds{static_cast<int64_t>(value) * 100}} };
auto hours{ hms.hours().count() };
if (hours)
return fast_io::wconcat_winrt_hstring(fast_io::mnp::right(hours, 2, L'0'), fast_io::mnp::chvw(L':'), fast_io::mnp::right(hms.minutes().count(), 2, L'0'), fast_io::mnp::chvw(L':'), fast_io::mnp::right(hms.seconds().count(), 2, L'0'));
else
return fast_io::wconcat_winrt_hstring(fast_io::mnp::right(hms.minutes().count(), 2, L'0'), fast_io::mnp::chvw(L':'), fast_io::mnp::right(hms.seconds().count(), 2, L'0'));
}
winrt::event_token PlayerViewModel::PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return propertyChanged_.add(handler);
Expand Down
7 changes: 7 additions & 0 deletions src/PlayerViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ namespace winrt::Player::implementation
void Volume(double value);
uint32_t Index();
void Index(uint32_t value);
double Position();
void Position(double value);
double Duration();
void Duration(double value);
winrt::event_token PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
void PropertyChanged(winrt::event_token const& token) noexcept;
static winrt::hstring DoubleToString(double);
static winrt::hstring DurationToString(double);
private:
double volume_;
uint32_t index_;
double position_;
double duration_;
winrt::event<winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler> propertyChanged_;
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/PlayerViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Player
PlayerViewModel();
Double Volume;
UInt32 Index;
Double Position;
Double Duration;
static String DoubleToString(Double volume);
static String DurationToString(Double duration);
}
}
11 changes: 8 additions & 3 deletions src/RootPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,23 @@
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="11"
Text="0:00:00" />
Text="{x:Bind local:PlayerViewModel.DurationToString(local:RootPage.PlayerViewModel.Position), Mode=OneWay}" />
<Slider
Name="PlayerSlider"
Grid.Column="1"
Margin="0"
HorizontalAlignment="Stretch" />
HorizontalAlignment="Stretch"
IsThumbToolTipEnabled="False"
Maximum="{x:Bind local:RootPage.PlayerViewModel.Duration, Mode=OneWay}"
StepFrequency="1000"
Value="{x:Bind local:RootPage.PlayerViewModel.Position, Mode=TwoWay}" />
<TextBlock
x:Uid="RemainingTime"
Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="11"
Text="0:00:00" />
Text="{x:Bind local:PlayerViewModel.DurationToString(local:RootPage.PlayerViewModel.Duration), Mode=OneWay}" />
</Grid>
<Button
x:Uid="Playing"
Expand Down
38 changes: 33 additions & 5 deletions src/RootPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ namespace winrt::Player::implementation
RootPage::RootPage()
{
InitializeComponent();

playerViewModel_ = winrt::Player::PlayerViewModel{};
player_ = winrt::Windows::Media::Playback::MediaPlayer{};
player_.AudioCategory(Windows::Media::Playback::MediaPlayerAudioCategory::Media);
session_ = player_.PlaybackSession();
commander_ = player_.CommandManager();
list_ = winrt::Windows::Media::Playback::MediaPlaybackList{};
list_.MaxPlayedItemsToKeepOpen(3);
player_.Source(list_);
music_ = list_.Items();
// init state
if (SettingsHelper::CheckFirstUse()) [[unlikely]] {
RootFrame().Navigate(winrt::xaml_typename<Player::Welcome>());
MainNavigation().IsPaneOpen(false);
}
// set player
list_.MaxPlayedItemsToKeepOpen(3);
player_.Source(list_);
player_.AudioCategory(Windows::Media::Playback::MediaPlayerAudioCategory::Media);

InitializeLibraries();
InitializeRegistEvent();
Expand Down Expand Up @@ -88,6 +92,30 @@ namespace winrt::Player::implementation
playerViewModel_.PropertyChanged([&self = *this](IInspectable const&, PropertyChangedEventArgs const&) {
self.player_.Volume(self.playerViewModel_.Volume() / 100.);
});
// slider
session_.PlaybackStateChanged([&self = *this, ui_thread = winrt::apartment_context{}](decltype(session_) const& sender, IInspectable const&) -> IAsyncAction {
auto state{ self.session_.PlaybackState() };
using namespace std::chrono_literals;
if (state == decltype(state)::Playing) [[likely]] {
auto index{ list_.CurrentItemIndex() };
auto info{ self.info_list_.GetAt(index) };
co_await ui_thread;
self.playerViewModel_.Duration(static_cast<double>(info.Duration));
}
while (self.session_.PlaybackState() == decltype(self.session_.PlaybackState())::Playing) [[likely]] {
using namespace std::chrono_literals;
co_await 1s;
co_await ui_thread;
auto position{ self.session_.Position().count() };
if (position > 0) [[likely]] {
self.playerViewModel_.Position(static_cast<double>(position));
}
}
});
playerViewModel_.PropertyChanged([&self = *this, ui_thread = winrt::apartment_context{}](IInspectable const&, PropertyChangedEventArgs const& args) {
if (args.PropertyName() != L"Position") return;
self.session_.Position(std::chrono::duration_cast<decltype(self.session_.Position())>(std::chrono::nanoseconds{ static_cast<int64_t>(self.playerViewModel_.Position()) * 100 }));
});
// when switch to new music, make button ui on
list_.CurrentItemChanged([&self = *this, ui_thread = winrt::apartment_context{}](decltype(list_) const&, Windows::Media::Playback::CurrentMediaPlaybackItemChangedEventArgs const& args) -> IAsyncAction {
auto reason{ args.Reason() };
Expand Down
13 changes: 7 additions & 6 deletions src/RootPage.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace winrt::Player::implementation
static winrt::Windows::Foundation::Collections::IVector<winrt::Data::MusicInfo> InfoList();
static winrt::Windows::Foundation::Collections::IObservableVector<winrt::Data::Library> Libraries();
static winrt::Windows::Foundation::Collections::IObservableVector<Windows::Media::Playback::MediaPlaybackItem> Music();

// events
void Page_Loaded(winrt::Windows::Foundation::IInspectable const&, winrt::Microsoft::UI::Xaml::RoutedEventArgs const&);
void Volume_LostFocus(winrt::Windows::Foundation::IInspectable const&, winrt::Microsoft::UI::Xaml::RoutedEventArgs const&);
Expand Down Expand Up @@ -48,14 +49,14 @@ namespace winrt::Player::implementation
void InitializeLibraries();
// rumtime properties implement
winrt::hstring title_{L"PlayerWinRT"};
Windows::Media::Playback::MediaPlayer player_{};
winrt::Windows::Media::Playback::MediaPlaybackSession session_{ player_.PlaybackSession() };
winrt::Windows::Media::Playback::MediaPlaybackCommandManager commander_{ player_.CommandManager() };
Windows::Media::Playback::MediaPlayer player_{ nullptr };
winrt::Windows::Media::Playback::MediaPlaybackSession session_{ nullptr };
winrt::Windows::Media::Playback::MediaPlaybackCommandManager commander_{ nullptr };
inline static winrt::Data::Library library_;
inline static winrt::Player::PlayerViewModel playerViewModel_;
inline static Windows::Media::Playback::MediaPlaybackList list_{};
inline static winrt::Player::PlayerViewModel playerViewModel_{ nullptr };
inline static Windows::Media::Playback::MediaPlaybackList list_{ nullptr };
inline static winrt::Windows::Foundation::Collections::IObservableVector<winrt::Data::Library> libraries_{ nullptr };
inline static winrt::Windows::Foundation::Collections::IObservableVector<Windows::Media::Playback::MediaPlaybackItem> music_{list_.Items()};
inline static winrt::Windows::Foundation::Collections::IObservableVector<Windows::Media::Playback::MediaPlaybackItem> music_{ nullptr };
inline static winrt::Windows::Foundation::Collections::IVector<winrt::Data::MusicInfo> info_list_{ winrt::single_threaded_vector<winrt::Data::MusicInfo>() };
};
}
Expand Down

0 comments on commit 00e093d

Please sign in to comment.