I've managed to setup code for a Windows Phone 8 Application that initializes and can start/stop recording video using an AudioVideoCaptureDevice. (saves it to an IRandomAccessStream)
//Initialize Camera Recording
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
captureDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution);
captureDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264;
captureDevice.AudioEncodingFormat = CameraCaptureAudioFormat.Aac;
captureDevice.RecordingFailed += captureDevice_RecordingFailed;
However, I cannot figure out how to hook this recording up to a VideoBrush to display the recording to the user. I want the user to be able to see the video they are recording as it is happening...
I know there is a tutorial that shows how to do this using the old APIs for Windows Phone 7 (CaptureSource, VideoDevice, etc.) but I specifically need to use the AudioVideoCaptureDevice to record. Anyone know how to display this video on screen?
Well, I was able to solve my problem.
Apparently there is a library in Microsoft.Devices that contains extensions for the VideoBrush class.
Therefore, in order to set the videobrush source to an AudioVideoCaptureDevice, you must first have:
using Microsoft.Devices;
at the top of your class in which your using the videobrush.
Hope this is able to help someone else.
You should be able to simply use VideoBrush.SetSource(captureDevice).
Related
I want to play a playlist in the resources folder for a windows forms project I'm working on (wav or mp3). The application will be completely offline. (actually a game project)
What I want to do is this:
I need to be able to stop or convert a music that I created in one form to another sound file, depending on the situation.
In this case, I couldn't figure out how to write a code block. There are many examples that describe how we can pause and start the music on single form, but I have not come across an example that covers the whole project and can be intervened from different forms.
I'm not sure if I explained exactly what I wanted to ask, but I can give an example as follows. Let's say I created 3 forms.
1st form and 2nd form game menus.
In these menus, the same music should continue to play.
But when I reach the 3rd form, this music should be cut and replaced with in-game music.
What I want to ask is: How can I control the music I created in form 1, in form 2 or form 3?
The reason I don't use the soundplayer class is because I have to play multiple sounds at once.
i.e., somehow I need to make the music player "static" so that I can control it from all forms, but I couldn't find the way around it.
var importer = new RawSourceWaveStream(Properties.Resources.sound, new WaveFormat());
var soundFx = new WaveOut();
soundFx.Init(importer);
soundFx.Play();
For example, in this way, I start playing the music in the form1. I need to give the command to change this music in the form3, but somehow I cannot reach the "soundfx" object that I defined there because I cannot make this process static.
I think that your intuition is valid. It is probably not the best way, but I inffer that it is a way that you would manage to do.
Have a public static class, that holds a private (or public) instance of you sound player. Have public static methods of the different things that you want to do with the audio player.
Control that class from all forms.
Since this is your intuition. I am curious of what have you so far in this way?
Anyhow, I have a few questions.
When you say "convert" the music, what do you mean?
What do you mean that you couldnt find a way around it?
Can you add some code of how you are controlling the sounds? With some code, people will be able to better support you.
It is recommended that you use MediaPlayer. From your description, MediaPlayer can better meet your needs. Compared with SoundPlayer, MediaPlayer has the following characteristics:
Multiple sounds can be played at the same time (create multiple MediaPlayer objects);
You can adjust the volume (Volume property);
You can use Play, Pause, Stop and other methods to control;
You can set the IsMuted property to True to achieve mute;
You can use the Balance property to adjust the balance of the left and right speakers;
The speed of audio playback can be controlled through the SpeedRatio property;
The length of the audio can be obtained through the NaturalDuration property, and the current playback progress can be obtained through the Position property;
Seek can be performed through the Position property;
Use MediaPlayer to play audio files as follows:
MediaPlayer player = new MediaPlayer();
player.Open(new Uri("BLOW.WAV", UriKind.Relative));
player. Play();
A MediaPlayer object can only play one file at a time. And the file is played asynchronously, you can also call Close to release the file.
For details, please refer to https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-7.0
When using it in VS, you need to perform the following steps first:
This way you can find MediaPlayer in the toolbar.
If you still have any questions please speak up.
I have created a UWP app to play certain tracks in the background. Basically by following this link: https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/ .
I want to set the repeat count for certain songs, so if a song has repeat count 10, that song is meant to be repeated 10 times before moving on to the next song in the playlist.
On the Windows phone 8.0 platform, the AudioPlayerAgent had the following event which indicated that the play state has changed. It was easy to override that event and add custom logic to repeat songs.
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackEnded:
// keep repeating the same track
player.Position = new TimeSpan(0, 0, (int)0);
// add custom logic here..
break;
}
NotifyComplete();
}
What would be an equivalent event in the UWP platform?
So far I have tried the following events on the UWP platform, but to no avail..
BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded;
BackgroundMediaPlayer.Current.MediaOpened += Current_MediaOpened;
With Windows 10, version 1607, a new single-process model has been introduced that greatly simplifies the process of enabling background audio.
Media continues to play when your app moves from the foreground to the background. This means that even after the user has minimized your app, returned to the home screen, or has navigated away from your app in some other way, your app can continue to play audio.
Starting with Windows 10, version 1607,the recommended best practice for playing media is to use the MediaPlayer class instead of MediaElement
Play a media file with MediaPlayer
_mediaPlayer = new MediaPlayer();
_mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
_mediaPlayer.Play();
MSDN: Play media in the background
Now your app can manage the playlist or loop settings and use the media player instance to call Play method again.
I'm using Background Audio Player on Windows Phone 8 and I'm trying to set an album art for my audio track. As I've read here and at MSDN the image MUST be at shared/media/ - ok I've checked the image is there and I try to do:
AudioTrack myTrack = new AudioTrack(track.source, track.Title, track.Artist, track.Album, new Uri("shared/media/Episode29.jpg", UriKind.Relative));
The music starts, but there is no image. Am I doing something wrong?
I've also run Background Audio Player Sample mentioned here at MSDN and I also see no image.
Is there a control to show an animated gif in a Windows Store (Metro) app in Windows 8? I am using C# and XAML with databinding.
The Image control doesn't support animated GIFs. You will need to extract the frames and animate them on your own timer.
You should take a look at this link which might help you regarding your question:
http://advertboy.wordpress.com/2012/05/08/animated-gifs-in-xamlc/
You cannot display an animated gif in a grid. However, you can display an animated gift in the webview controller.
Just for a note: you can use the BitmapDecoder class to read the GIF frames, create a storyboard an animated them.
I've got an example of an Windows 8 user control on my blog: http://www.henrikbrinch.dk/Blog/2013/02/21/Windows-8---GIF-animations--the-RIGHT-way
I managed to display gif's in my windows 8 metro app simply by converting the gifs to jpegs and then running an infinite loop.
The code is as follows :
public async void UpdateImage()
{
await Task.Delay(300);
var frame = this.Frame.CurrentSourcePageType.Name;
if (frame != ("CURRENTFRAME")) return;
if (count <= 4)
{
var img = (BitmapImage) Resources["bitmap" + count];
imgTap.Source = img;
UpdateLayout();
count++;
UpdateImage();
}
else
{
count = 1;
UpdateImage();
}
}
So basically I'm saving the converted jpegs in my page.resources and then naming them as bitmap1,bitmap2 .. etc .
I m checking whether the current frame is the frame which has to display the gif or else the gif code would run in the background for the entire time which is a waste of memory and CPU.
Just call this method on any of the Loaded method and it should run fine.
This is probably overkill, but you could try using a WebView to display the GIF. The WebView control introduces its own set of headaches, however, so unless you're willing to deal with said headaches, I would recommend avoiding it unless you absolutely have to use it.
If I am right then Silverlight doesn't support GIF and Metro Apps are based on Silverlight platform. Hence dont contain support for GIF images natively. You can however use 3rd party controls like http://www.componentone.com/SuperProducts/ImageSilverlight/.
I am Creating an windows application using c#.I have a button which should capture the image(the entire Desktop screen) and Save it in a folder .Also i need to show the preview of the image .
Graphics.CopyFromScreen Method
sample code:
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Save();
bmp.Save("D:\\file.jpg", ImageFormat.Bmp);
as for show the preview. IMO not that hard to write it on ur own.
There are different ways to perform what you bring here. Using the Screen class, there are a few simple samples I found on the Internet. Others are using Direct3D.
TeboScreen: Basic C# Screen Capture Application;
Capture a Screen Shot;
C# – Screen capture with Direct3D;
Capture DeskTop Screen;
Enhanced Desktop Recorder in .NET using C# and Windows Forms; (perhaps not suited for your question, but might get interesting if you plan further features.)
Capturing the Screen Image Using C#.
In short, the idea consists of getting the image of the desktop using the Screen class or your favorite way, store it into a Bitmap object and save this bitmap into a file.
As for displaying a preview, once your Bitmap instance is created, you simply need a PictureBox and set its Image property and show your form to the user so he may see the image.
Hope this helps! =)
You will need to do some importing of Interop dlls.
Take a look at the following example shows very well how to capture the screen shot and save to disk.
public void CaptureScreen(string fileName,ImageFormat imageFormat)
{
int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap);
GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8),
GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020);
SaveImageAs(hBitmap,fileName,imageFormat);
Cleanup(hBitmap,hdcSrc,hdcDest);
}
The above example taken from the website. All code by Perry Lee