1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-09 06:15:49 +02:00

Move sound notification handling to a separate library

This commit is contained in:
chylex 2017-06-03 15:32:18 +02:00
parent a1aebab114
commit c018a2a7bc
14 changed files with 181 additions and 75 deletions

View File

@ -11,7 +11,6 @@
using TweetDuck.Core.Handling;
using TweetDuck.Core.Notification;
using TweetDuck.Core.Notification.Screenshot;
using TweetDuck.Core.Notification.Sound;
using TweetDuck.Core.Other;
using TweetDuck.Core.Utils;
using TweetDuck.Plugins;
@ -20,6 +19,7 @@
using TweetDuck.Resources;
using TweetDuck.Updates;
using TweetDuck.Updates.Events;
using TweetLib.Audio.Utils;
namespace TweetDuck.Core{
sealed partial class FormBrowser : Form{
@ -38,7 +38,7 @@ sealed partial class FormBrowser : Form{
private FormWindowState prevState;
private TweetScreenshotManager notificationScreenshotManager;
private ISoundNotificationPlayer soundNotification;
private SoundNotification soundNotification;
public FormBrowser(PluginManager pluginManager, UpdaterSettings updaterSettings){
InitializeComponent();
@ -427,7 +427,7 @@ public void PlayNotificationSound(){
}
if (soundNotification == null){
soundNotification = SoundNotification.New();
soundNotification = new SoundNotification();
soundNotification.PlaybackError += soundNotification_PlaybackError;
}

View File

@ -1,12 +0,0 @@
using System;
namespace TweetDuck.Core.Notification.Sound{
interface ISoundNotificationPlayer : IDisposable{
string SupportedFormats { get; }
event EventHandler<PlaybackErrorEventArgs> PlaybackError;
void Play(string file);
void Stop();
}
}

View File

@ -1,28 +1,29 @@
using System.Runtime.InteropServices;
using TweetDuck.Core.Notification.Sound;
using System;
using TweetLib.Audio;
using TweetLib.Audio.Utils;
namespace TweetDuck.Core.Notification{
static class SoundNotification{
private static bool? IsWMPAvailable;
sealed class SoundNotification : IDisposable{
public string SupportedFormats => player.SupportedFormats;
public event EventHandler<PlaybackErrorEventArgs> PlaybackError;
public static ISoundNotificationPlayer New(){
if (IsWMPAvailable.HasValue){
if (IsWMPAvailable.Value){
return new SoundPlayerImplWMP();
}
else{
return new SoundPlayerImplFallback();
}
}
private readonly AudioPlayer player;
try{
SoundPlayerImplWMP implWMP = new SoundPlayerImplWMP();
IsWMPAvailable = true;
return implWMP;
}catch(COMException){
IsWMPAvailable = false;
return new SoundPlayerImplFallback();
}
public SoundNotification(){
this.player = AudioPlayer.New();
this.player.PlaybackError += Player_PlaybackError;
}
public void Play(string file){
player.Play(file);
}
private void Player_PlaybackError(object sender, PlaybackErrorEventArgs e){
PlaybackError?.Invoke(this, e);
}
public void Dispose(){
player.Dispose();
}
}
}

View File

@ -3,16 +3,16 @@
using System.IO;
using System.Windows.Forms;
using TweetDuck.Core.Notification;
using TweetDuck.Core.Notification.Sound;
using TweetLib.Audio.Utils;
namespace TweetDuck.Core.Other.Settings{
partial class TabSettingsSounds : BaseTabSettings{
private readonly ISoundNotificationPlayer soundNotification;
private readonly SoundNotification soundNotification;
public TabSettingsSounds(){
InitializeComponent();
soundNotification = SoundNotification.New();
soundNotification = new SoundNotification();
soundNotification.PlaybackError += sound_PlaybackError;
tbCustomSound.Text = Config.NotificationSoundPath;

View File

@ -39,7 +39,7 @@
[assembly: NeutralResourcesLanguage("en")]
[assembly: CLSCompliant(false)]
[assembly: CLSCompliant(true)]
#if DEBUG
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("UnitTests")]

View File

@ -123,10 +123,6 @@
<DependentUpon>FormNotificationTweet.cs</DependentUpon>
</Compile>
<Compile Include="Core\Notification\SoundNotification.cs" />
<Compile Include="Core\Notification\Sound\SoundPlayerImplFallback.cs" />
<Compile Include="Core\Notification\Sound\SoundPlayerImplWMP.cs" />
<Compile Include="Core\Notification\Sound\ISoundNotificationPlayer.cs" />
<Compile Include="Core\Notification\Sound\PlaybackErrorEventArgs.cs" />
<Compile Include="Core\Notification\TweetNotification.cs" />
<Compile Include="Core\Other\FormAbout.cs">
<SubType>Form</SubType>
@ -208,7 +204,6 @@
</Compile>
<Compile Include="Core\Notification\Screenshot\TweetScreenshotManager.cs" />
<Compile Include="Core\Utils\InjectedHTML.cs" />
<Compile Include="Core\Utils\NativeCoreAudio.cs" />
<Compile Include="Core\Utils\TwoKeyDictionary.cs" />
<Compile Include="Core\Utils\WindowState.cs" />
<Compile Include="Core\Utils\WindowsUtils.cs" />
@ -340,17 +335,10 @@
<Content Include="Resources\Scripts\update.js" />
</ItemGroup>
<ItemGroup>
<COMReference Include="WMPLib">
<Guid>{6BF52A50-394A-11D3-B153-00C04F79FAA6}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="lib\TweetLib.Audio\TweetLib.Audio.csproj">
<Project>{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}</Project>
<Name>TweetLib.Audio</Name>
</ProjectReference>
<ProjectReference Include="subprocess\TweetDuck.Browser.csproj">
<Project>{b10b0017-819e-4f71-870f-8256b36a26aa}</Project>
<Name>TweetDuck.Browser</Name>

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.6
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TweetDuck", "TweetDuck.csproj", "{2389A7CD-E0D3-4706-8294-092929A33A2D}"
EndProject
@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TweetDuck.Browser", "subpro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "tests\UnitTests.csproj", "{A958FA7A-4A2C-42A7-BFA0-159343483F4E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TweetLib.Audio", "lib\TweetLib.Audio\TweetLib.Audio.csproj", "{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
@ -26,6 +28,10 @@ Global
{A958FA7A-4A2C-42A7-BFA0-159343483F4E}.Debug|x86.ActiveCfg = Debug|x86
{A958FA7A-4A2C-42A7-BFA0-159343483F4E}.Debug|x86.Build.0 = Debug|x86
{A958FA7A-4A2C-42A7-BFA0-159343483F4E}.Release|x86.ActiveCfg = Release|x86
{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}.Debug|x86.ActiveCfg = Debug|x86
{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}.Debug|x86.Build.0 = Debug|x86
{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}.Release|x86.ActiveCfg = Release|x86
{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,37 @@
using System;
using System.Runtime.InteropServices;
using TweetLib.Audio.Impl;
using TweetLib.Audio.Utils;
namespace TweetLib.Audio{
public abstract class AudioPlayer : IDisposable{
private static bool? IsWMPAvailable;
public static AudioPlayer New(){
if (IsWMPAvailable.HasValue){
if (IsWMPAvailable.Value){
return new SoundPlayerImplWMP();
}
else{
return new SoundPlayerImplFallback();
}
}
try{
SoundPlayerImplWMP implWMP = new SoundPlayerImplWMP();
IsWMPAvailable = true;
return implWMP;
}catch(COMException){
IsWMPAvailable = false;
return new SoundPlayerImplFallback();
}
}
public abstract string SupportedFormats { get; }
public abstract event EventHandler<PlaybackErrorEventArgs> PlaybackError;
public abstract void Play(string file);
public abstract void Stop();
public abstract void Dispose();
}
}

View File

@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Media;
using TweetLib.Audio.Utils;
namespace TweetDuck.Core.Notification.Sound{
sealed class SoundPlayerImplFallback : ISoundNotificationPlayer{
string ISoundNotificationPlayer.SupportedFormats => "*.wav";
namespace TweetLib.Audio.Impl{
sealed class SoundPlayerImplFallback : AudioPlayer{
public override string SupportedFormats => "*.wav";
public event EventHandler<PlaybackErrorEventArgs> PlaybackError;
public override event EventHandler<PlaybackErrorEventArgs> PlaybackError;
private readonly SoundPlayer player;
private bool ignorePlaybackError;
@ -17,7 +18,7 @@ public SoundPlayerImplFallback(){
};
}
void ISoundNotificationPlayer.Play(string file){
public override void Play(string file){
if (player.SoundLocation != file){
player.SoundLocation = file;
ignorePlaybackError = false;
@ -34,11 +35,11 @@ void ISoundNotificationPlayer.Play(string file){
}
}
void ISoundNotificationPlayer.Stop(){
public override void Stop(){
player.Stop();
}
void IDisposable.Dispose(){
public override void Dispose(){
player.Dispose();
}

View File

@ -1,13 +1,13 @@
using System;
using System.Runtime.InteropServices;
using TweetDuck.Core.Utils;
using TweetLib.Audio.Utils;
using WMPLib;
namespace TweetDuck.Core.Notification.Sound{
sealed class SoundPlayerImplWMP : ISoundNotificationPlayer{
string ISoundNotificationPlayer.SupportedFormats => "*.wav;*.mp3;*.mp2;*.m4a;*.mid;*.midi;*.rmi;*.wma;*.aif;*.aifc;*.aiff;*.snd;*.au";
namespace TweetLib.Audio.Impl{
sealed class SoundPlayerImplWMP : AudioPlayer{
public override string SupportedFormats => "*.wav;*.mp3;*.mp2;*.m4a;*.mid;*.midi;*.rmi;*.wma;*.aif;*.aifc;*.aiff;*.snd;*.au";
public event EventHandler<PlaybackErrorEventArgs> PlaybackError;
public override event EventHandler<PlaybackErrorEventArgs> PlaybackError;
private readonly WindowsMediaPlayer player;
private bool wasTryingToPlay;
@ -29,7 +29,7 @@ public SoundPlayerImplWMP(){
player.MediaError += player_MediaError;
}
void ISoundNotificationPlayer.Play(string file){
public override void Play(string file){
wasTryingToPlay = true;
try{
@ -48,7 +48,7 @@ void ISoundNotificationPlayer.Play(string file){
}
}
void ISoundNotificationPlayer.Stop(){
public override void Stop(){
try{
player.controls.stop();
}catch{
@ -56,7 +56,7 @@ void ISoundNotificationPlayer.Stop(){
}
}
void IDisposable.Dispose(){
public override void Dispose(){
player.close();
Marshal.ReleaseComObject(player);
}

View File

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TweetDuck Audio Library")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TweetDuck Audio Library")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e9e1fd1b-f480-45b7-9970-be2ecfd309ac")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E9E1FD1B-F480-45B7-9970-BE2ECFD309AC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TweetLib.Audio</RootNamespace>
<AssemblyName>TweetLib.Audio</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ResolveComReferenceSilent>True</ResolveComReferenceSilent>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="AudioPlayer.cs" />
<Compile Include="Utils\NativeCoreAudio.cs" />
<Compile Include="Utils\PlaybackErrorEventArgs.cs" />
<Compile Include="Impl\SoundPlayerImplFallback.cs" />
<Compile Include="Impl\SoundPlayerImplWMP.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="WMPLib">
<Guid>{6BF52A50-394A-11D3-B153-00C04F79FAA6}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -4,7 +4,7 @@
using System.IO;
using System.Runtime.InteropServices;
namespace TweetDuck.Core.Utils{
namespace TweetLib.Audio.Utils{
static class NativeCoreAudio{
private const int EDATAFLOW_RENDER = 0;
private const int EROLE_MULTIMEDIA = 1;

View File

@ -1,7 +1,7 @@
using System;
namespace TweetDuck.Core.Notification.Sound{
sealed class PlaybackErrorEventArgs : EventArgs{
namespace TweetLib.Audio.Utils{
public sealed class PlaybackErrorEventArgs : EventArgs{
public string Message { get; }
public bool Ignore { get; set; }