7. // Create NOK Imaging SDK effects pipeline and run it
var imageStream = new BitmapImageSource(image.AsBitmap());
using (var effect = new FilterEffect(imageStream))
{
// Define the filters list
var filter = new AntiqueFilter();
effect.Filters = new[] { filter };
// Render the filtered image to a WriteableBitmap.
var renderer = new WriteableBitmapRenderer(effect, editedBitmap);
editedBitmap = await renderer.RenderAsync();
editedBitmap.Invalidate();
// Show image in Xaml Image control
Image.Source = editedBitmap;
}
13. // Create MediaCapture and init
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();
// Assign to Xaml CaptureElement.Source and start preview
PreviewElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
// Create MediaCapture and init
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();
// Interop between new MediaCapture API and SL 8.1 to show
// the preview in SL XAML as Rectangle.Fill
var previewSink = new MediaCapturePreviewSink();
var videoBrush = new VideoBrush();
videoBrush.SetSource(previewSink);
PreviewElement.Fill = videoBrush;
// Find the supported preview size
var vdc = mediaCapture.VideoDeviceController;
var availableMediaStreamProperties =
vdc.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
// More LINQ selection happens here in the demo to find closest format
var previewFormat = availableMediaStreamProperties.FirstOrDefault();
// Start Preview stream
await vdc.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview,
previewFormat);
await mediaCapture.StartPreviewToCustomSinkAsync(
new MediaEncodingProfile { Video = previewFormat }, previewSink);
14. public async Task CapturePhoto()
{
// Create photo encoding properties as JPEG and set the size that should be used for capturing
var imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
imgEncodingProperties.Width = 640;
imgEncodingProperties.Height = 480;
// Create new unique file in the pictures library and capture photo into it
var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("photo.jpg",
CreationCollisionOption.GenerateUniqueName);
await mediaCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties, photoStorageFile);
}
16. public async Task StartVideoRecording()
{
// Create video encoding profile as MP4
var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
// Create new unique file in the videos library and record video!
var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("video.mp4",
CreationCollisionOption.GenerateUniqueName);
await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
}
public async Task StopVideoRecording()
{
await mediaCapture.StopRecordAsync();
// Start playback in MediaElement
var videoFileStream = await videoFile.OpenReadAsync();
PlaybackElement.SetSource(videoFileStream, videoFile.ContentType);
}
21. // Wire up current screen as input for the MediaCapture
var screenCapture = ScreenCapture.GetForCurrentView();
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoSource = screenCapture.VideoSource,
AudioSource = screenCapture.AudioSource,
});
// Create video encoding profile as MP4
var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
// Create new unique file in the videos library and record video!
var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("screenrecording.mp4",
CreationCollisionOption.GenerateUniqueName);
await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
26. mediaComposition = new MediaComposition();
foreach (StorageFile videoFile in videoFiles)
{
// Create Clip and add to composition
var clip = await MediaClip.CreateFromFileAsync(videoFile);
mediaComposition.Clips.Add(clip);
// Create thumbnail to show as placeholder in an UI ListView
var thumbnail = await videoFile.GetThumbnailAsync(ThumbnailMode.VideosView);
var image = new BitmapImage();
image.SetSource(thumbnail);
// Add to a viewmodel used as ItemsSource for a ListView of clips
videoClips.Add(new VideoClip(clip, image, videoFile.Name));
}
27. // Trimming. Skip 1 second from the beginning of the clip and 2 from the end
var clipVm = videoClips.SelectedClip;
clipVm.Clip.TrimTimeFromStart = TimeSpan.FromSeconds(1);
clipVm.Clip.TrimTimeFromEnd = clipVm.Clip.OriginalDuration.Subtract(TimeSpan.FromSeconds(2));
// Add MP3 as background which was deployed together with the AppX package
var file = await Package.Current.InstalledLocation.GetFileAsync("mymusic.mp3");
var audio = await BackgroundAudioTrack.CreateFromFileAsync(file);
mediaComposition.BackgroundAudioTracks.Add(audio);
28. // Play the composed result including all clips and the audio track in a Xaml MediaElement
var w = (int) MediaElement.ActualWidth;
var h = (int) MediaElement.ActualHeight;
MediaElement.SetMediaStreamSource(mediaComposition.GeneratePreviewMediaStreamSource(w, h)));
// Create output file
var vidLib = KnownFolders.VideosLibrary;
var resultFile = await vidLib.CreateFileAsync("myComposition.mp4",
CreationCollisionOption.ReplaceExisting);
// Encode new composition as MP4 to the file
var mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
await mediaComposition.RenderToFileAsync(resultFile, MediaTrimmingPreference.Fast, mediaEncodingProfile);
35. // Attach event handler to background player to update UI
BackgroundMediaPlayer.Current.CurrentStateChanged += MediaPlayerStateChanged;
// Uri could also be ms-appx:/// for package-local tracks
BackgroundMediaPlayer.Current.SetUriSource(new Uri("http://foo.bar/my.mp3"));
// Starts play since MediaPlayer.AutoPlay=true by default
// Or trigger manually play when AutoPlay=false
BackgroundMediaPlayer.Current.Play();
// Pause.
BackgroundMediaPlayer.Current.Pause();
// Stop. There's no MediaPlayer.Stop() method
BackgroundMediaPlayer.Current.Pause();
BackgroundMediaPlayer.Current.Position = TimeSpan.FromSeconds(0);
36. private async void MediaPlayerStateChanged(MediaPlayer sender, object args)
{
// This event is called from a background thread, so dispatch to UI
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
switch (BackgroundMediaPlayer.Current.CurrentState)
{
case MediaPlayerState.Playing:
AppBarBtnPause.IsEnabled = true;
// Pass params on to background process, so it can update the UVC text there
BackgroundMediaPlayer.SendMessageToBackground(new ValueSet
{ {"Title", "My Super MP3"}, {"Artist", "Foo Bar"} });
break;
case MediaPlayerState.Paused:
AppBarBtnPause.IsEnabled = false;
break;
}
});
}
37. public void Run(IBackgroundTaskInstance taskInstance)
{
// Initialize SMTC object to talk with UVC
// This is intended to run after app is paused,
// therefore all the logic must be written to run in background process
systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
systemmediatransportcontrol.ButtonPressed += SystemControlsButtonPressed;
systemmediatransportcontrol.IsEnabled = true;
systemmediatransportcontrol.IsPauseEnabled = true;
systemmediatransportcontrol.IsPlayEnabled = true;
// Add handlers to update SMTC when MediaPlayer is used in foreground
BackgroundMediaPlayer.Current.CurrentStateChanged += BackgroundMediaPlayerCurrentStateChanged;
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceived;
}
42. 47
Windows Phone 8 Windows Phone 8.1
Silverlight
Windows Phone 8.1
WinRT
Windows 8.1
Windows.Phone.
Media.Capture
Yes No No No
Windows.
Media.Capture
No Yes Yes Yes
VariablePhoto-
SequenceCapture
No Yes Yes No
ScreenCapture No Yes Yes No
43. 48
Windows Phone 8 Windows Phone 8.1
Silverlight
Windows Phone 8.1
WinRT
Windows 8.1
Windows.Media.
Editing
No Yes Yes No
Windows.Media.
Transcoding
No Yes Yes Yes
44. 49
Windows Phone 8 Windows Phone 8.1
Silverlight
Windows Phone 8.1
WinRT
Windows 8.1
Windows.Media.
BackgroundPlayback
No No Yes No
Microsoft.Phone.
BackgroundAudio
Yes No No No
MediaElement
AudioCategory=
"BackgroundCapable
Media"
No No No Yes