Created
December 4, 2014 22:22
-
-
Save JonDouglas/7072f27543921c06b5be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using UpgradeTest.Renderers; | |
using UpgradeTest.WinPhone.Renderers; | |
using Windows.Media.Capture; | |
using Windows.Media.MediaProperties; | |
using Windows.Storage; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.WinPhone; | |
[assembly: ExportRenderer(typeof(VideoView), typeof(VideoViewRenderer))] | |
namespace UpgradeTest.WinPhone.Renderers | |
{ | |
public class VideoViewRenderer : ViewRenderer<VideoView, MediaElement> | |
{ | |
MediaCapture dev; | |
StorageFile outputFile; | |
bool _isRecording = false; | |
bool _isPlaying = false; | |
VideoView _control; | |
private MediaElement newControl; | |
protected override void OnElementChanged(ElementChangedEventArgs<VideoView> e) | |
{ | |
newControl = Application.Current.Resources["GlobalMedia"] as MediaElement; | |
_control = e.NewElement; | |
_control.Status = "IDLE"; | |
base.OnElementChanged(e); | |
_control.StopAction = async () => | |
{ | |
if (_isPlaying) | |
{ | |
newControl.Stop(); | |
_isPlaying = false; | |
} | |
else if (_isRecording) | |
{ | |
await dev.StopRecordAsync(); | |
dev.Dispose(); | |
_isRecording = false; | |
} | |
_control.Status = "STOPPED"; | |
}; | |
_control.PlayAction = () => | |
{ | |
_isPlaying = true; | |
_control.Status = "Playing"; | |
newControl.Source = new Uri(outputFile.Path, UriKind.Absolute); | |
newControl.Play(); | |
}; | |
_control.RecordAction = () => | |
{ | |
_control.Status = "Recording"; | |
StartRecording(_control.FileSource); | |
}; | |
var mediaElement = new MediaElement(); | |
mediaElement.AutoPlay = false; | |
mediaElement.Height = 200; | |
mediaElement.Width = 200; | |
base.SetNativeControl(mediaElement); | |
newControl.CurrentStateChanged += Control_CurrentStateChanged; | |
} | |
void Control_CurrentStateChanged(object sender, RoutedEventArgs e) | |
{ | |
if (newControl.CurrentState == System.Windows.Media.MediaElementState.Stopped || newControl.CurrentState == System.Windows.Media.MediaElementState.Closed) | |
{ | |
_isPlaying = false; | |
} | |
_control.Status = newControl.CurrentState.ToString(); | |
} | |
public async void StartRecording(string filename) | |
{ | |
_isRecording = true; | |
filename += ".wav"; | |
dev = new MediaCapture(); | |
var settings = new MediaCaptureInitializationSettings(); | |
settings.StreamingCaptureMode = StreamingCaptureMode.Audio; | |
settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other; | |
settings.AudioProcessing = Windows.Media.AudioProcessing.Default; | |
await dev.InitializeAsync(settings); | |
StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; | |
try | |
{ | |
outputFile = await applicationFolder.GetFileAsync(filename); | |
await outputFile.DeleteAsync(); | |
} | |
catch | |
{ | |
} | |
outputFile = await applicationFolder.CreateFileAsync(filename); | |
MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Medium); | |
await dev.StartRecordToStorageFileAsync(recordProfile, outputFile); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment