Skip to content

Commit

Permalink
Added volume bar, configuration and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timweiss committed Jun 24, 2017
1 parent 0a9cc97 commit 1e0f0e7
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 16 deletions.
84 changes: 84 additions & 0 deletions LunaticPlayer/Client/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace LunaticPlayer.Client
{
class ConfigurationData
{
public double Volume { get; set; }
}

class Configuration
{
private const string Filename = "config.json";

private static Configuration _instance;

public ConfigurationData Data { get; set; }

/// <summary>
/// The source of configuration data (Filesystem, Default Object, ...).
/// </summary>
public string Source { get; set; }

public static Configuration GetInstance()
{
if (_instance == null)
{
_instance = new Configuration();
_instance.Initialize();
}

return _instance;
}

/// <summary>
/// Loads the configuration and stores it to the filesystem if necessary.
/// </summary>
private void Initialize()
{

if (File.Exists(Filename))
{
Data = JsonConvert.DeserializeObject<ConfigurationData>(File.ReadAllText(Filename));
Source = "Filesystem";
}
else
{
Source = "DefaultDataObject";

SetupData();

// Erstellt eine Konfigurationsdatei für das nächste Mal.
Save();
}
}

/// <summary>
/// Creates a <seealso cref="ConfigurationData"/> object with default values.
/// </summary>
private void SetupData()
{
var data = new ConfigurationData()
{
Volume = 0.4
};

Data = data;
}

/// <summary>
/// Saves the current configuration to the file specified at <see cref="Filename"/>.
/// </summary>
public void Save()
{
Console.WriteLine("Saving configuration to filesystem.");
File.WriteAllText(Filename, JsonConvert.SerializeObject(Data));
}
}
}
14 changes: 14 additions & 0 deletions LunaticPlayer/Controls/VolumeBar.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="LunaticPlayer.Controls.VolumeBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LunaticPlayer.Controls"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="100">
<Grid Background="Transparent">
<Border Padding="5,15">
<Slider Name="VolumeSlider" Orientation="Horizontal" Maximum="1" Value="{Binding Volume}" ValueChanged="VolumeSlider_ValueChanged"/>
</Border>
</Grid>
</UserControl>
45 changes: 45 additions & 0 deletions LunaticPlayer/Controls/VolumeBar.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
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;

namespace LunaticPlayer.Controls
{
public class VolumeBarData
{
public double Volume { get; set; }
}

/// <summary>
/// Interaktionslogik für VolumeBar.xaml
/// </summary>
public partial class VolumeBar : UserControl
{
public VolumeBarData Data { get; set; }
public Action OnValueChange { get; set; }

public VolumeBar(VolumeBarData data)
{
InitializeComponent();

DataContext = Data = data;
}

private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Data.Volume = VolumeSlider.Value;

OnValueChange?.Invoke();
}
}
}
15 changes: 13 additions & 2 deletions LunaticPlayer/LunaticPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Client\Configuration.cs" />
<Compile Include="Controls\VolumeBar.xaml.cs">
<DependentUpon>VolumeBar.xaml</DependentUpon>
</Compile>
<Compile Include="PopupBanner.xaml.cs">
<DependentUpon>PopupBanner.xaml</DependentUpon>
</Compile>
Expand All @@ -81,6 +85,10 @@
<Compile Include="Windows\DialogWindow.xaml.cs">
<DependentUpon>DialogWindow.xaml</DependentUpon>
</Compile>
<Page Include="Controls\VolumeBar.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="PlayerInterface.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -163,8 +171,8 @@
<Resource Include="Resources\unmute_mat.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\mute_92.png" />
<Resource Include="Resources\unmute_92.png" />
<Resource Include="Resources\voloff_92.png" />
<Resource Include="Resources\volume_92.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\list_92.png" />
Expand Down Expand Up @@ -200,6 +208,9 @@
<ItemGroup>
<Resource Include="Resources\help_white_92.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\mute_92.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Data.SQLite.Core.1.0.105.0\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.105.0\build\net451\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
16 changes: 15 additions & 1 deletion LunaticPlayer/Player/RadioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RadioPlayer
{
private readonly MediaPlayer _player;

public bool Muted => _player.Volume == 0.0;
public bool Muted { get; set; }

public double Volume => _player.Volume;

Expand Down Expand Up @@ -39,5 +39,19 @@ public void ToggleMute()
{
_player.Volume = _player.Volume == 0.0 ? 0.5 : 0.0;
}

public void ToggleMute(double volume)
{
if (Muted)
{
_player.Volume = volume;
Muted = false;
}
else
{
_player.Volume = 0.0;
Muted = true;
}
}
}
}
22 changes: 15 additions & 7 deletions LunaticPlayer/PlayerInterface.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LunaticPlayer"
xmlns:controls="clr-namespace:LunaticPlayer.Controls"
mc:Ignorable="d"
Title="Lunatic Player" Height="245" Width="375" Closed="Window_Closed" Loaded="Window_Loaded">

Expand Down Expand Up @@ -79,29 +80,36 @@
</Border>
</DockPanel>
</Grid>

<Grid Height="45" VerticalAlignment="Bottom" Background="#FF767676">
<Grid.Effect>
<DropShadowEffect Direction="90" Opacity="0.2" ShadowDepth="4"/>
</Grid.Effect>
<StackPanel Orientation="Horizontal" Height="45" VerticalAlignment="Bottom" Margin="0,0,50,0">
<Border Padding="5" Height="45" Width="45" HorizontalAlignment="Left">
<StackPanel Name="ButtonToolbar" Orientation="Horizontal" Height="45" VerticalAlignment="Bottom" Margin="0,0,50,0">
<Border ToolTip="Change Volume" Padding="5" Height="45" Width="45" HorizontalAlignment="Left">
<Button Style="{StaticResource TransparentImageButton}" Name="VolumeMeterButton" Click="VolumeButton_OnClick" >
<Button.Background>
<ImageBrush ImageSource="Resources/volume_92.png"/>
</Button.Background>
</Button>
</Border>
<Border ToolTip="Mute" Padding="5" Height="45" Width="45" HorizontalAlignment="Left">
<Button Style="{StaticResource TransparentImageButton}" Name="MuteButton" Click="MuteButton_Click">
<Button.Background>
<ImageBrush ImageSource="Resources/mute_92.png"/>
<ImageBrush ImageSource="Resources/voloff_92.png"/>
</Button.Background>
</Button>
</Border>
<Border Padding="5" Height="45" Width="45" HorizontalAlignment="Left">
<Border ToolTip="Show History" Padding="5" Height="45" Width="45" HorizontalAlignment="Left">
<Button Style="{StaticResource TransparentImageButton}" Name="SongListButton" Click="SongListButton_OnClick" >
<Button.Background>
<ImageBrush ImageSource="Resources/list_92.png"/>
</Button.Background>
</Button>
</Border>
</StackPanel>
<Border Padding="5" Height="45" Width="45" HorizontalAlignment="Right">

<Border ToolTip="Settings" Padding="5" Height="45" Width="45" HorizontalAlignment="Right">
<Button Style="{StaticResource TransparentImageButton}" Name="OptionsButton" Click="OptionsButton_OnClick" >
<Button.Background>
<ImageBrush ImageSource="Resources/settings_92.png"/>
Expand Down
48 changes: 45 additions & 3 deletions LunaticPlayer/PlayerInterface.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using LunaticPlayer.Classes;
using LunaticPlayer.Client;
using LunaticPlayer.Controls;

namespace LunaticPlayer
{
Expand Down Expand Up @@ -40,6 +42,7 @@ public MainWindow()
_interfaceTimer.Interval = 1000;
_interfaceTimer.Elapsed += ReloadInterface;

_radioPlayer.SetVolume(Configuration.GetInstance().Data.Volume);

LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
Expand Down Expand Up @@ -134,6 +137,7 @@ private void PlayButtonClicked()
else
{
_radioPlayer.PlayFromUrl(ApiClient.StreamUrl);
_radioPlayer.SetVolume(Configuration.GetInstance().Data.Volume);
_isPlaying = true;
TBPlayButton.Description = "Stop";

Expand All @@ -153,15 +157,15 @@ private void PlayButtonClicked()
/// </summary>
private void MuteRadioStream()
{
_radioPlayer.ToggleMute();
_radioPlayer.ToggleMute(Configuration.GetInstance().Data.Volume);

if (_radioPlayer.Muted)
{
TBMuteButton.Description = "Unmute";
var packUri = "pack://application:,,,/LunaticPlayer;component/Resources/unmute_mat.ico";
TBMuteButton.ImageSource = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;

var appUri = new Uri("pack://application:,,,/LunaticPlayer;component/Resources/unmute_92.png");
var appUri = new Uri("pack://application:,,,/LunaticPlayer;component/Resources/mute_92.png");
MuteButton.Background = new ImageBrush(new BitmapImage(appUri));
}
else
Expand All @@ -170,11 +174,13 @@ private void MuteRadioStream()
var packUri = "pack://application:,,,/LunaticPlayer;component/Resources/mute_92.png";
TBMuteButton.ImageSource = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;

var appUri = new Uri("pack://application:,,,/LunaticPlayer;component/Resources/mute_92.png");
var appUri = new Uri("pack://application:,,,/LunaticPlayer;component/Resources/voloff_92.png");
MuteButton.Background = new ImageBrush(new BitmapImage(appUri));
}
}

private VolumeBar volumeBar;

#region Button Events

private void Button_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -215,6 +221,35 @@ private void SongListButton_OnClick(object sender, RoutedEventArgs e)
sWindow.Show();
}

private void VolumeButton_OnClick(object sender, RoutedEventArgs e)
{
if (volumeBar == null)
{
var vol = Configuration.GetInstance().Data.Volume;
volumeBar = new VolumeBar(new VolumeBarData() { Volume = vol });
volumeBar.Height = 50;
volumeBar.Width = 100;
volumeBar.OnValueChange = OnVolumeChange;
}

if (this.ButtonToolbar.Children.Contains(volumeBar))
{
if (volumeBar.Visibility == Visibility.Visible)
{
volumeBar.Visibility = Visibility.Collapsed;
}
else
{
volumeBar.Visibility = Visibility.Visible;
}

}
else
{
this.ButtonToolbar.Children.Insert(1, volumeBar);
}
}

#endregion

/// <summary>
Expand All @@ -224,6 +259,7 @@ private void SongListButton_OnClick(object sender, RoutedEventArgs e)
/// <param name="e"></param>
private void Window_Closed(object sender, EventArgs e)
{
Configuration.GetInstance().Save();
_radioPlayer.Stop();
Application.Current.Shutdown();
}
Expand Down Expand Up @@ -297,5 +333,11 @@ private void RunFadeInAnimation()
Storyboard.SetTarget(sb, this.PlayerContent);
sb.Begin();
}

private void OnVolumeChange()
{
_radioPlayer.SetVolume(volumeBar.Data.Volume);
Configuration.GetInstance().Data.Volume = Math.Round(volumeBar.Data.Volume, 2);
}
}
}
Binary file modified LunaticPlayer/Resources/mute_92.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LunaticPlayer/Resources/voloff_92.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading

0 comments on commit 1e0f0e7

Please sign in to comment.