Small code cleanup

This commit is contained in:
Jonas Dellinger 2018-03-12 00:13:38 +01:00
parent e52b9ef61a
commit 023520d3de
2 changed files with 22 additions and 40 deletions

View File

@ -9,9 +9,9 @@ namespace SpotifyAPI.Local.Models
{ {
public class SpotifyUri public class SpotifyUri
{ {
internal Dictionary<UriType, string> _properties = new Dictionary<UriType, string>(); private readonly Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
public string Base { get; internal set; } public string Base { get; }
public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none; public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none;
public string Id => _properties?.LastOrDefault().Value; public string Id => _properties?.LastOrDefault().Value;
@ -29,9 +29,7 @@ namespace SpotifyAPI.Local.Models
public string GetUriPropValue(UriType type) public string GetUriPropValue(UriType type)
{ {
if (!_properties.ContainsKey(type)) return !_properties.ContainsKey(type) ? null : _properties[type];
return null;
return _properties[type];
} }
public static SpotifyUri Parse(string uri) public static SpotifyUri Parse(string uri)
@ -39,17 +37,15 @@ namespace SpotifyAPI.Local.Models
if (string.IsNullOrEmpty(uri)) if (string.IsNullOrEmpty(uri))
throw new ArgumentNullException("Uri"); throw new ArgumentNullException("Uri");
UriType uriType = UriType.none;
string[] props = uri.Split(':'); string[] props = uri.Split(':');
if (props.Length < 3 || !Enum.TryParse(props[1], out uriType)) if (props.Length < 3 || !Enum.TryParse(props[1], out UriType uriType))
throw new ArgumentException("Unexpected Uri"); throw new ArgumentException("Unexpected Uri");
Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } }; Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } };
for (int index = 3; index < props.Length; index += 2) for (int index = 3; index < props.Length; index += 2)
{ {
UriType type = UriType.none; if (Enum.TryParse(props[index], out UriType type))
if (Enum.TryParse(props[index], out type))
properties.Add(type, props[index + 1]); properties.Add(type, props[index + 1]);
} }
@ -58,7 +54,7 @@ namespace SpotifyAPI.Local.Models
public override string ToString() public override string ToString()
{ {
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}"; return $"{Base}:{string.Join(":", _properties.SelectMany(x => new[] { x.Key.ToString(), x.Value }))}";
} }
} }
} }

View File

@ -18,8 +18,7 @@ namespace SpotifyAPI.Local
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
} }
float level; volume.GetMasterVolume(out float level);
volume.GetMasterVolume(out level);
Marshal.ReleaseComObject(volume); Marshal.ReleaseComObject(volume);
return level * 100; return level * 100;
} }
@ -33,8 +32,7 @@ namespace SpotifyAPI.Local
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
} }
bool mute; volume.GetMute(out bool mute);
volume.GetMute(out mute);
Marshal.ReleaseComObject(volume); Marshal.ReleaseComObject(volume);
return mute; return mute;
} }
@ -79,12 +77,10 @@ namespace SpotifyAPI.Local
private static ISimpleAudioVolume GetVolumeObject(int pid) private static ISimpleAudioVolume GetVolumeObject(int pid)
{ {
// get the speakers (1st render + multimedia) device // get the speakers (1st render + multimedia) device
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator()); IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator) new MMDeviceEnumerator();
IMmDevice speakers; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers);
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);
string defaultDeviceId; speakers.GetId(out string defaultDeviceId);
speakers.GetId(out defaultDeviceId);
ISimpleAudioVolume volumeControl = GetVolumeObject(pid, speakers); ISimpleAudioVolume volumeControl = GetVolumeObject(pid, speakers);
Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(speakers);
@ -97,18 +93,13 @@ namespace SpotifyAPI.Local
// As far as Spotify is concerned, if using the "--enable-audio-graph" command line argument, // As far as Spotify is concerned, if using the "--enable-audio-graph" command line argument,
// a new option becomes available in the Settings that makes it possible to change the playback device. // a new option becomes available in the Settings that makes it possible to change the playback device.
IMmDeviceCollection deviceCollection; deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out IMmDeviceCollection deviceCollection);
deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out deviceCollection);
int count; deviceCollection.GetCount(out int count);
deviceCollection.GetCount(out count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
IMmDevice device; deviceCollection.Item(i, out IMmDevice device);
deviceCollection.Item(i, out device); device.GetId(out string deviceId);
string deviceId;
device.GetId(out deviceId);
try try
{ {
@ -134,29 +125,24 @@ namespace SpotifyAPI.Local
{ {
// activate the session manager. we need the enumerator // activate the session manager. we need the enumerator
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o; device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o);
device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2) o;
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
// enumerate sessions for on this device // enumerate sessions for on this device
IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
mgr.GetSessionEnumerator(out sessionEnumerator); sessionEnumerator.GetCount(out int count);
int count;
sessionEnumerator.GetCount(out count);
// search for an audio session with the required name // search for an audio session with the required name
// NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2) // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
ISimpleAudioVolume volumeControl = null; ISimpleAudioVolume volumeControl = null;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
IAudioSessionControl2 ctl; sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl);
sessionEnumerator.GetSession(i, out ctl); ctl.GetProcessId(out int cpid);
int cpid;
ctl.GetProcessId(out cpid);
if (cpid == pid) if (cpid == pid)
{ {
volumeControl = (ISimpleAudioVolume)ctl; volumeControl = (ISimpleAudioVolume) ctl;
break; break;
} }
Marshal.ReleaseComObject(ctl); Marshal.ReleaseComObject(ctl);