Skip to content
Permalink
Browse files

Einfacher API-Zugriff und Verarbeitung in eine Song-Klasse.

  • Loading branch information...
timweiss committed Feb 18, 2017
1 parent e04eb54 commit bcd2ff12e0cc979bf1968f4af59e65d1f2e13004
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using LunaticPlayer.Classes;

namespace LunaticPlayer.GSRadioAPI
{
public class ApiClient
{
public const string StreamUrl = "http://stream.gensokyoradio.net:8000/stream/1/";
public const string ApiUrl = "https://gensokyoradio.net/xml/";

public StructuredApiData CurrentStructuredApiData { get; private set; }

public Song PlayingSong()
{
return new Song()
{
Title = CurrentStructuredApiData.SongInfo["TITLE"],
Year = Convert.ToInt32(CurrentStructuredApiData.SongInfo["YEAR"]),
Duration = TimeSpan.FromSeconds(Convert.ToInt32(CurrentStructuredApiData.SongTimes["DURATION"])),
PlayedDuration = TimeSpan.FromSeconds(Convert.ToInt32(CurrentStructuredApiData.SongTimes["PLAYED"])),
StartTime = DateTime.Now.Add(-TimeSpan.FromSeconds(Convert.ToInt32(CurrentStructuredApiData.SongTimes["PLAYED"]))),
AlbumName = CurrentStructuredApiData.SongInfo["ALBUM"],
ArtistName = CurrentStructuredApiData.SongInfo["ARTIST"],
CircleName = CurrentStructuredApiData.SongInfo["CIRCLE"],
ApiSongId = Convert.ToInt32(CurrentStructuredApiData.MiscInfo["SONGID"]),
ApiAlbumId = Convert.ToInt32(CurrentStructuredApiData.MiscInfo["ALBUMID"]),
AlbumArtFilename = CurrentStructuredApiData.MiscInfo["ALBUMART"],
CirleArtFilename = CurrentStructuredApiData.MiscInfo["CIRCLEART"]
};
}

public async Task FetchRawApiData()
{
string rawXmlResult = "";

using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(ApiUrl))
using (HttpContent content = response.Content)
{
rawXmlResult = await content.ReadAsStringAsync();

XmlDocument xmlResult = new XmlDocument();
xmlResult.LoadXml(rawXmlResult);

var node = xmlResult.DocumentElement;

var apiData = new StructuredApiData();

List<string> sourceNodes = new List<string>() { "SERVERINFO", "SONGINFO", "SONGTIMES", "MISC" };

foreach (var item in sourceNodes)
{
var itemNode = node.SelectSingleNode("//" + item);
var entries = itemNode.ChildNodes;

foreach (XmlNode entry in entries)
{
switch (item)
{
case "SERVERINFO":
if (entry.InnerText == null)
{
apiData.ServerInfo.Add(entry.Name, "0");
break;
}
apiData.ServerInfo.Add(entry.Name, entry.InnerText);
break;
case "SONGINFO":
if (entry.InnerText == null)
{
apiData.SongInfo.Add(entry.Name, "0");
break;
}
apiData.SongInfo.Add(entry.Name, entry.InnerText);
break;
case "SONGTIMES":
if (entry.InnerText == null)
{
apiData.SongTimes.Add(entry.Name, "0");
break;
}
apiData.SongTimes.Add(entry.Name, entry.InnerText);
break;
case "MISC":
if (entry.InnerText == null)
{
apiData.MiscInfo.Add(entry.Name, "0");
break;
}
apiData.MiscInfo.Add(entry.Name, entry.InnerText);
break;
default:
break;
}
}
}

CurrentStructuredApiData = apiData;
}
}
}

public class StructuredApiData
{
public Dictionary<string, string> ServerInfo;
public Dictionary<string, string> SongInfo;
public Dictionary<string, string> SongTimes;
public Dictionary<string, string> MiscInfo;

public StructuredApiData()
{
ServerInfo = new Dictionary<string, string>();
SongInfo = new Dictionary<string, string>();
SongTimes = new Dictionary<string, string>();
MiscInfo = new Dictionary<string, string>();
}
}
}
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LunaticPlayer.Classes
{
public class Song
{
public string Title { get; set; }
public int Year { get; set; }
public string ArtistName { get; set; }
public string CircleName { get; set; }
public string AlbumName { get; set; }

public DateTime StartTime { get; set; }
public DateTime EndTime => StartTime + Duration;

public TimeSpan Duration { get; set; }
public TimeSpan PlayedDuration { get; set; }

public TimeSpan EndDuration => EndTime - DateTime.Now;

public int ApiSongId { get; set; }
public int ApiAlbumId { get; set; }

public string AlbumArtFilename { get; set; }
public string CirleArtFilename { get; set; }
}
}


// Startuhrzeit: DateTime.Now - Played
@@ -0,0 +1,48 @@
<?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>{432B7143-97FC-4E49-9D42-4E8C4D3085BA}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LunaticPlayer.GSRadioAPI</RootNamespace>
<AssemblyName>LunaticPlayer.GSRadioAPI</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApiClient.cs" />
<Compile Include="Classes\Song.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("LunaticPlayer.GSRadioAPI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LunaticPlayer.GSRadioAPI")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("432b7143-97fc-4e49-9d42-4e8c4d3085ba")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26206.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LunaticPlayer", "LunaticPlayer\LunaticPlayer.csproj", "{54C1D563-C319-478E-805B-DA04CB7D90A2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LunaticPlayer.GSRadioAPI", "LunaticPlayer.GSRadioAPI\LunaticPlayer.GSRadioAPI.csproj", "{432B7143-97FC-4E49-9D42-4E8C4D3085BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
{54C1D563-C319-478E-805B-DA04CB7D90A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54C1D563-C319-478E-805B-DA04CB7D90A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54C1D563-C319-478E-805B-DA04CB7D90A2}.Release|Any CPU.Build.0 = Release|Any CPU
{432B7143-97FC-4E49-9D42-4E8C4D3085BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{432B7143-97FC-4E49-9D42-4E8C4D3085BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{432B7143-97FC-4E49-9D42-4E8C4D3085BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{432B7143-97FC-4E49-9D42-4E8C4D3085BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LunaticPlayer"
StartupUri="MainWindow.xaml">
StartupUri="PlayerInterface.xaml">
<Application.Resources>

</Application.Resources>
@@ -54,20 +54,22 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="MainWindow.xaml">
<Page Include="PlayerInterface.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<Compile Include="PlayerInterface.xaml.cs">
<DependentUpon>PlayerInterface.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Player\RadioPlayer.cs" />
<Compile Include="Player\SongManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@@ -93,5 +95,11 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LunaticPlayer.GSRadioAPI\LunaticPlayer.GSRadioAPI.csproj">
<Project>{432b7143-97fc-4e49-9d42-4e8c4d3085ba}</Project>
<Name>LunaticPlayer.GSRadioAPI</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

This file was deleted.

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace LunaticPlayer.Player
{
class RadioPlayer
{
private MediaPlayer player;

public bool Muted => player.Volume == 0.0;

public double Volume => player.Volume;

public RadioPlayer()
{
player = new MediaPlayer();
}

public void PlayFromUrl(string url)
{
player.Open(new Uri(url));
player.Play();
player.Volume = 0.5;
}

public void Stop()
{
player.Stop();
}

//TODO: Lautstärke richtig einstellen
public void SetVolume(double volume)
{
player.Volume = volume;
}

public void ToggleMute(double volume)
{
if (player.Volume == 0.0)
{
player.Volume = volume / 10;
}
else
{
player.Volume = 0.0;
}
}
}
}

0 comments on commit bcd2ff1

Please sign in to comment.
You can’t perform that action at this time.