Video and learning in WINUI3 + MAUI

Pirasath Luxchumykanthan 1 Reputation point
2022-11-04T19:19:01.073+00:00

Okay.
Hallo everyone..
I new to MAUI
But okay let me try.
Teach me if im wrong.

var cams= await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

https://learn.microsoft.com/en-us/uwp/api/windows.devices.enumeration.deviceinformation?view=winrt-22621
This is how i can get a list of cameras

make setting for MediaCapture
var MediaCaptureSettings = new MediaCaptureInitializationSettings() { VideoDeviceId = [Any cams id] };

https://learn.microsoft.com/en-us/uwp/api/windows.media.capture.mediacaptureinitializationsettings?view=winrt-22621

Make a stream for MediaCapture
var RandomAccessStream = new InMemoryRandomAccessStream();
If you can explain me InMemoryRandomAccessStream will be nice.

MediaEncodingProfile mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

var mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(MediaCaptureSettings );
await mediaCapture.StartRecordToStreamAsync(mediaEncodingProfile , RandomAccessStream );

Im lost.....

oka i made some samles from video but it only work when i made a bug.. in the code.
Any help out there.. that can explain me..?

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
512 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,441 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pirasath Luxchumykanthan 1 Reputation point
    2022-11-05T11:50:13.58+00:00

    namespace Camera.Software;
    public partial class Video
    {
    public Video() => _ = Constructor();
    private async Task Constructor()
    {
    Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    //choice .mp4 easy to work whit
    Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".mp4");
    var b = await sampleFile.OpenStreamForWriteAsync();
    var c = await sampleFile.OpenStreamForReadAsync();
    //find video devices i can also find audio but i only choice to find video
    var videoCaptures = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    //if i need Audio i can place it AudioDeviceId then i have audio to
    MediaCaptureInitializationSettings mediaCaptureSettings = new () { VideoDeviceId = videoCaptures.First().Id };
    //well as i say there many format i can work easy whit mp4
    var mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
    InMemoryRandomAccessStream inMemoryRandomAccessStream = new();
    MediaCapture mediaCapture = new();
    await mediaCapture.InitializeAsync(mediaCaptureSettings);
    await mediaCapture.StartRecordToStreamAsync(mediaEncodingProfile, inMemoryRandomAccessStream);
    //need to lower the sening to server so i dont send alot of data
    int LastPosition = 0;
    while (true) {
    using var memoryStream = new MemoryStream();
    inMemoryRandomAccessStream.GetInputStreamAt(0).AsStreamForRead().CopyTo(memoryStream);
    //why send all to server
    var a = memoryStream.ToArray().Skip(LastPosition).ToArray();
    LastPosition += a.Count();
    //yes from her i can stream the data to what to ever server and so on
    if(a.Length> 0)
    await b.WriteAsync(a);
    //make sure not to limit the stream like small restart
    if (inMemoryRandomAccessStream.Position >= 2000000000)
    {
    //yes a quick restart
    await mediaCapture.StopRecordAsync();
    LastPosition = 0;
    inMemoryRandomAccessStream.Dispose();
    inMemoryRandomAccessStream = new();
    await mediaCapture.StartRecordToStreamAsync(mediaEncodingProfile, inMemoryRandomAccessStream);
    continue;
    }
    }
    }

    }

    0 comments No comments