Skip to content

Commit

Permalink
Audio Processing working in .net core / Ubuntu. Needs detections buil…
Browse files Browse the repository at this point in the history
…t to determine what audio player to use.
  • Loading branch information
TWhidden committed Sep 17, 2018
1 parent dea6fb0 commit ac81c13
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public override void FileRequestReceived(ProtocolMessage message)
Directory.CreateDirectory(pathOnly);
}

File.WriteAllBytes(_fileDownloadContainer.DestinationPath,
Convert.FromBase64String(message.MessageParts[ProtocolMessage.FILEBYTES]));
var fileBytes = Convert.FromBase64String(message.MessageParts[ProtocolMessage.FILEBYTES]);

Console.WriteLine($"Writing {fileBytes.Length:N} bytes to file {_fileDownloadContainer.DestinationPath}");
File.WriteAllBytes(_fileDownloadContainer.DestinationPath, fileBytes);
}
_tcs.SetResult(true);
}
Expand All @@ -58,6 +60,8 @@ public override void FileRequestReceived(ProtocolMessage message)

protected override void NewConnectionEstablished()
{
Console.WriteLine($"Requesting Missing File '{_fileDownloadContainer.FileName}'");

var dic = new Dictionary<string, string>
{
{ProtocolMessage.FILEDOWNLOAD, _fileDownloadContainer.FileName},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
#if !CORE
using Windows.ApplicationModel.Core;
Expand All @@ -15,12 +16,25 @@ public class AudioInstanceController : IAudioInstanceController
#if !CORE
private MediaElement _mediaElement;
#endif
private Process _externalPlayerProcess;

public event EventHandler<IAudioRequestController> Complete;

public async void PlayMediaUri(IAudioRequestController c, Uri uri)
{
#if !CORE
#if CORE
Console.WriteLine($"Audio File Play: {uri.AbsolutePath}");

_externalPlayerProcess = new Process()
{
StartInfo = new ProcessStartInfo("play", uri.AbsolutePath),

};
var result = _externalPlayerProcess.Start();

Console.WriteLine($"Start Result: {result}");

#else
_currentRequest = c;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
Expand All @@ -29,7 +43,7 @@ public async void PlayMediaUri(IAudioRequestController c, Uri uri)
_mediaElement.Play();
});
#endif

}
#if !CORE
public void SetMediaElement(MediaElement mediaElement)
Expand All @@ -43,16 +57,19 @@ public void SetMediaElement(MediaElement mediaElement)

public async void StopPlayback()
{
#if CORE
_externalPlayerProcess?.Close();
_externalPlayerProcess?.Dispose();

#if !CORE
#else
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
_mediaElement.Stop();
InvokeOnComplete(_currentRequest);
});
#endif

}

#if !CORE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public void StopAllAudio()

public async Task<IAudioRequestController> RequestAndPlay(string fileName)
{
var newReuqest = _resolverService.Resolve<IAudioRequestController>();
newReuqest.FileName = fileName;
var playerController = await TryPlayAudio(newReuqest);
var newRequest = _resolverService.Resolve<IAudioRequestController>();
newRequest.FileName = fileName;
var playerController = await TryPlayAudio(newRequest);
if (playerController != null)
{
newReuqest.OnStop += ((s, e) => { playerController.StopPlayback(); });
return newReuqest;
newRequest.OnStop += ((s, e) => { playerController.StopPlayback(); });
return newRequest;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HolidayShowEndpointUniversalApp.Containers;
using HolidayShowLibUniversal.Controllers;
Expand All @@ -18,7 +19,7 @@ public AudioRequestController(IResolverService resolverService)
{
_resolverService = resolverService;
#if CORE
_rootStoragePath = HolidayShowClient.Core.Program.StoragePath;
_rootStoragePath = Path.Combine(HolidayShowClient.Core.Program.StoragePath, StoragePathFolder);
#else
var applicationData = Windows.Storage.ApplicationData.Current;
var localFolder = applicationData.LocalFolder.Path;
Expand All @@ -38,7 +39,7 @@ public void Stop()
}

/// <summary>
/// The Filename requested for playback. This shouldnt be the path. Just the filename.
/// The Filename requested for playback. This shouldn't be the path. Just the filename.
/// </summary>
public string FileName { get; set; }

Expand All @@ -47,18 +48,24 @@ public void Stop()
/// </summary>
/// <summary>
/// Verify that the file exists, and pull the file from the server if its not available
/// Then returns the path of the file. If the file can not be found or aquired, it will return an empty string.
/// Then returns the path of the file. If the file can not be found or acquired, it will return an empty string.
/// </summary>
/// <returns></returns>
public async Task<Uri> FileReady()
{
// Find out if the file exists.
var audioPath = Path.Combine(_rootStoragePath, FileName);
if (File.Exists(audioPath)) return new Uri(audioPath);
var audioPath = Path.Combine(_rootStoragePath, Regex.Replace(FileName, "[^a-zA-Z0-9.\\-]", "_"));
if (File.Exists(audioPath))
{
Console.WriteLine($"Audio File Exists: {audioPath}");
return new Uri(audioPath);
}

Console.WriteLine($"Audio File DOES NOT Exist: {audioPath}");

var fd = new FileDownloadContainer(FileName, audioPath);

// Download the file from the serfer
// Download the file from the server
var fileDownloader = _resolverService.Resolve<FileDownloadClient>(fd);
if (!await fileDownloader.FileFinsihed())
{
Expand Down

0 comments on commit ac81c13

Please sign in to comment.