Skip to content

Commit

Permalink
done #16 #20 #21
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Sep 23, 2017
1 parent 5d1a1d8 commit 677564c
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 153 deletions.
26 changes: 14 additions & 12 deletions Legato.Sample/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 72 additions & 43 deletions Legato.Sample/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,102 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Console;

namespace Legato.Sample
{
public partial class Form1 : Form
{
#region Field

private Legato _Legato { get; set; }
private int _SongPosition;

#endregion Field

public Form1()
{
InitializeComponent();
}

_Legato = new Legato();
_SongPosition = 0;
#region Properties

Icon = Properties.Resources.legato;
private Legato _Legato { get; set; }

_UpdateAlbumArt();
#endregion Properties

#region Methods

/// <summary>
/// Legatoに対するイベントリスナを追加します
/// </summary>
private void _AddLegatoEventListeners()
{
_Legato.Connected += () =>
{
WriteLine("接続されました");
};

_Legato.Communicator.CurrentTrackChanged += () =>
_Legato.Disconnected += () =>
{
_UpdateAlbumArt();
WriteLine("切断されました");
};

_Legato.Communicator.StatePropertyChanged += (state) =>
_Legato.Communicator.CurrentTrackChanged += (track) =>
{
WriteLine("CurrentTrackChanged:");
Write($"Title:{track.Title} ");
Write($"Artist:{track.Artist} ");
Write($"Album:{track.Album} ");
Write($"Genre:{track.Genre} ");
Write($"Duration:{track.Duration} ");
Write($"TrackNumber:{track.TrackNumber} ");
Write($"Year:{track.Year} ");
Write($"ChannelType:{track.ChannelType} ");
Write($"BitRate:{track.BitRate} ");
Write($"SampleRate:{track.SampleRate} ");
WriteLine();

_UpdateAlbumArt();
};

_Legato.Communicator.PositionPropertyChanged += (position) => {
_SongPosition = position;
_UpdateSongPosition();
_Legato.Communicator.StatePropertyChanged += (state) =>
{
WriteLine($"StatePropertyChanged: {state}");
_UpdateAlbumArt();
};
}

/// <summary>
/// 再生時間の表示を更新します
/// </summary>
private void _UpdateSongPosition()
{
var totalSec = _SongPosition / 1000;
var min = totalSec / 60;
var sec = totalSec % 60;
_Legato.Communicator.PositionPropertyChanged += (position) =>
{
var totalSec = position / 1000;
var min = totalSec / 60;
var sec = totalSec % 60;

CurrentPos.Text = $"{min:D2}:{sec:D2}";
CurrentPos.Text = $"{min:D2}:{sec:D2}";
};
}

/// <summary>
/// フォームに表示されているアルバムアートを更新します
/// </summary>
private void _UpdateAlbumArt()
{
pictureBox1.Image = _Legato.AlbumArt ?? Properties.Resources.logo;
if (_Legato?.IsRunning ?? false)
pictureBox1.Image = _Legato.AlbumArt ?? Properties.Resources.logo;
else
pictureBox1.Image = Properties.Resources.logo;
}

#endregion Methods

#region Procedures

private void Form1_Load(object sender, EventArgs e)
{
Icon = Properties.Resources.legato;

_Legato = new Legato();
_AddLegatoEventListeners();
_UpdateAlbumArt();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
_Legato.Dispose();
}

private void buttonPlayPause_Click(object sender, EventArgs e)
{
if (_Legato?.IsRunning ?? false)
Expand All @@ -88,24 +121,20 @@ private void buttonPrev_Click(object sender, EventArgs e)
}
}

private void buttonFetch_Click(object sender, EventArgs e)
private void buttonPlayerInfo_Click(object sender, EventArgs e)
{
WriteLine($"IsRunning:{_Legato.IsRunning}");
Write($"IsRunning:{_Legato.IsRunning} ");

if (_Legato?.IsRunning ?? false)
{
WriteLine($"State:{_Legato.State}");
WriteLine($"IsShuffle:{_Legato.IsShuffle}");
WriteLine($"Volume:{_Legato.Volume}");
WriteLine($"Position:{_Legato.Position} - {_Legato.Duration}");

var track = _Legato.CurrentTrack;
WriteLine($"Title:{track.Title}");
WriteLine($"Artist:{track.Artist}");
WriteLine($"Album:{track.Album}");

pictureBox1.Image = _Legato.AlbumArt ?? Properties.Resources.logo;
Write($"State:{_Legato.State} ");
Write($"Volume:{_Legato.Volume} ");
Write($"IsShuffle:{_Legato.IsShuffle} ");
Write($"IsRepeat:{_Legato.IsRepeat} ");
Write($"IsMute:{_Legato.IsMute} ");
Write($"Position:{_Legato.Position} ");
}
WriteLine();
}

#endregion Procedures
Expand Down
41 changes: 20 additions & 21 deletions Legato/Interop/AimpRemote/CommunicationWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Legato.Interop.AimpRemote.Entities;
using Legato.Interop.AimpRemote.Enum;
using Legato.Interop.Win32.Enum;
using static Legato.Interop.Win32.API;
Expand All @@ -19,9 +19,8 @@ public class CommunicationWindow : Form
public event Action<NotifyType, IntPtr> NotifyMessageReceived;

// Changed events
public event Action<PropertyType> PropertyChanged;
public event Action TrackInfoChanged;
public event Action CurrentTrackChanged;
public event Action<PlayerProperty> PropertyChanged;
public event Action<TrackInfo> CurrentTrackChanged;

// PropertyChanged events
public event Action<TimeSpan> DurationPropertyChanged;
Expand All @@ -32,6 +31,11 @@ public class CommunicationWindow : Form
public event Action<PlayerState> StatePropertyChanged;
public event Action<int> VolumePropertyChanged;

public void OnPositionPropertyChanged(int position)
{
PositionPropertyChanged?.Invoke(position);
}

public CommunicationWindow()
{
// メッセージ専用ウインドウに変更
Expand All @@ -53,22 +57,17 @@ public CommunicationWindow()

NotifyMessageReceived += (type, lParam) =>
{
if (type != NotifyType.Property)
Debug.WriteLine($"[NotifyMessage] {type} 0x{lParam.ToString("X")}");

// PropertyChanged を発行
if (type == NotifyType.Property)
PropertyChanged?.Invoke((PropertyType)lParam);
PropertyChanged?.Invoke((PlayerProperty)lParam);

// CurrentTrackChanged を発行
else if (type == NotifyType.TrackStart)
CurrentTrackChanged?.Invoke();
CurrentTrackChanged?.Invoke(Helper.GetCurrentTrack());

// TrackInfoChanged を発行
else if (type == NotifyType.TrackInfo && (int)lParam == 1)
TrackInfoChanged?.Invoke();
else if (type == NotifyType.TrackInfo) { }

else if (type != NotifyType.TrackInfo)
else
throw new ApplicationException($"NotifyType '{type}' is undefined value");
};

Expand All @@ -77,31 +76,31 @@ public CommunicationWindow()
var propertyValue = Helper.SendPropertyMessage(type, PropertyAccessMode.Get).ToInt32();

// DurationPropertyChanged を発行
if (type == PropertyType.Duration)
if (type == PlayerProperty.Duration)
DurationPropertyChanged?.Invoke(TimeSpan.FromMilliseconds(propertyValue));

// IsMutePropertyChanged を発行
else if (type == PropertyType.IsMute)
else if (type == PlayerProperty.IsMute)
IsMutePropertyChanged?.Invoke(propertyValue != 0);

// IsRepeatPropertyChanged を発行
else if (type == PropertyType.IsRepeat)
else if (type == PlayerProperty.IsRepeat)
IsRepeatPropertyChanged?.Invoke(propertyValue != 0);

// IsShufflePropertyChanged を発行
else if (type == PropertyType.IsShuffle)
else if (type == PlayerProperty.IsShuffle)
IsShufflePropertyChanged?.Invoke(propertyValue != 0);

// PositionPropertyChanged を発行
else if (type == PropertyType.Position)
PositionPropertyChanged?.Invoke(propertyValue);
else if (type == PlayerProperty.Position)
OnPositionPropertyChanged(propertyValue);

// StatePropertyChanged を発行
else if (type == PropertyType.State)
else if (type == PlayerProperty.State)
StatePropertyChanged?.Invoke((PlayerState)propertyValue);

// VolumePropertyChanged を発行
else if (type == PropertyType.Volume)
else if (type == PlayerProperty.Volume)
VolumePropertyChanged?.Invoke(propertyValue);

else
Expand Down
7 changes: 1 addition & 6 deletions Legato/Interop/AimpRemote/Entities/TrackInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace Legato.Interop.AimpRemote.Entities
/// </summary>
public class TrackInfo
{
/// <summary>
/// 現在の再生状況を取得・設定します。
/// </summary>
public bool IsActive { get; set; }

/// <summary>
/// 再生中の曲のビットレートを取得・設定します。
/// </summary>
Expand All @@ -20,7 +15,7 @@ public class TrackInfo
/// <summary>
/// 音源の出力タイプを取得・設定します。
/// </summary>
public ChannelType channelType { get; set; }
public ChannelType ChannelType { get; set; }

/// <summary>
/// 再生中の曲の長さを取得・設定します。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Legato.Interop.AimpRemote.Enum
{
public enum PropertyType : uint
public enum PlayerProperty : uint
{
/* TODO
/// <summary>
Expand Down
13 changes: 0 additions & 13 deletions Legato/Interop/AimpRemote/Exceptions/AIMPNotRunningException.cs

This file was deleted.

Loading

0 comments on commit 677564c

Please sign in to comment.