C
C#•4d ago
Protagonist

.NET MAUI Android Dashcam App

I am trying to make an Android dashcam application, and I have the preview setup working fine but I have an issue with my cameraView not adhering to a surfaceview or textureview. I need some help because I've been stuck on this for a long time. If anyone has any expertise regarding this, It would be greatly appreciated 😄 Am i doing something wrong?
5 Replies
Protagonist
ProtagonistOP•4d ago
using Dashy.ViewModels;
using Dashy.Interfaces;

namespace Dashy.Views;

public partial class MainPage : ContentPage
{
private readonly MainViewModel _viewModel;

public MainPage(MainViewModel viewModel, ICameraService cameraService)
{
InitializeComponent();
BindingContext = _viewModel = viewModel;

cameraService.Initialize(backCameraView);
}

protected override async void OnAppearing()
{
base.OnAppearing();
await _viewModel.InitializeCameraAsync();
}
}
using Dashy.ViewModels;
using Dashy.Interfaces;

namespace Dashy.Views;

public partial class MainPage : ContentPage
{
private readonly MainViewModel _viewModel;

public MainPage(MainViewModel viewModel, ICameraService cameraService)
{
InitializeComponent();
BindingContext = _viewModel = viewModel;

cameraService.Initialize(backCameraView);
}

protected override async void OnAppearing()
{
base.OnAppearing();
await _viewModel.InitializeCameraAsync();
}
}
using Dashy.Interfaces;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Threading.Tasks;

namespace Dashy.ViewModels
{
public partial class MainViewModel : BaseViewModel
{
private readonly ICameraService _cameraService;

[ObservableProperty]
private string _recordButtonText = "Start Recording";

[ObservableProperty]
private bool _isRecording;

[ObservableProperty]
private string _recordingStatus = "Idle";

public MainViewModel(ICameraService cameraService)
{
_cameraService = cameraService;
}

[RelayCommand]
private async Task ToggleRecordingAsync()
{
if (IsRecording)
{
RecordingStatus = "Stopping...";
await _cameraService.StopRecordingAsync();
IsRecording = false;
RecordingStatus = "Idle";
}
else
{
RecordingStatus = "Starting...";
await _cameraService.StartRecordingAsync();
IsRecording = true;
RecordingStatus = "Recording...";
}

RecordButtonText = IsRecording ? "Stop Recording" : "Start Recording";
}

public async Task InitializeCameraAsync()
{
await _cameraService.StartPreviewAsync();
}
}
}
using Dashy.Interfaces;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Threading.Tasks;

namespace Dashy.ViewModels
{
public partial class MainViewModel : BaseViewModel
{
private readonly ICameraService _cameraService;

[ObservableProperty]
private string _recordButtonText = "Start Recording";

[ObservableProperty]
private bool _isRecording;

[ObservableProperty]
private string _recordingStatus = "Idle";

public MainViewModel(ICameraService cameraService)
{
_cameraService = cameraService;
}

[RelayCommand]
private async Task ToggleRecordingAsync()
{
if (IsRecording)
{
RecordingStatus = "Stopping...";
await _cameraService.StopRecordingAsync();
IsRecording = false;
RecordingStatus = "Idle";
}
else
{
RecordingStatus = "Starting...";
await _cameraService.StartRecordingAsync();
IsRecording = true;
RecordingStatus = "Recording...";
}

RecordButtonText = IsRecording ? "Stop Recording" : "Start Recording";
}

public async Task InitializeCameraAsync()
{
await _cameraService.StartPreviewAsync();
}
}
}
Protagonist
ProtagonistOP•4d ago
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Dashy.Views.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:vm="clr-namespace:Dashy.ViewModels"
BackgroundColor="Black"
x:DataType="vm:MainViewModel">

<Grid>
<toolkit:CameraView
x:Name="backCameraView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>

<Label
Text="REC"
TextColor="Red"
FontSize="16"
FontAttributes="Bold"
HorizontalOptions="Start"
VerticalOptions="Start"
Margin="40,60"
IsVisible="{Binding IsRecording}"/>

<Label
Text="{Binding RecordingStatus}"
TextColor="White"
FontSize="14"
HorizontalOptions="Center"
VerticalOptions="Start"
Margin="0,100"/>

<Button
x:Name="recordButton"
Text="{Binding RecordButtonText}"
Command="{Binding ToggleRecordingCommand}"
HorizontalOptions="Center"
VerticalOptions="End"
Margin="20"
WidthRequest="80"
HeightRequest="80"
CornerRadius="40"/>
</Grid>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Dashy.Views.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:vm="clr-namespace:Dashy.ViewModels"
BackgroundColor="Black"
x:DataType="vm:MainViewModel">

<Grid>
<toolkit:CameraView
x:Name="backCameraView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>

<Label
Text="REC"
TextColor="Red"
FontSize="16"
FontAttributes="Bold"
HorizontalOptions="Start"
VerticalOptions="Start"
Margin="40,60"
IsVisible="{Binding IsRecording}"/>

<Label
Text="{Binding RecordingStatus}"
TextColor="White"
FontSize="14"
HorizontalOptions="Center"
VerticalOptions="Start"
Margin="0,100"/>

<Button
x:Name="recordButton"
Text="{Binding RecordButtonText}"
Command="{Binding ToggleRecordingCommand}"
HorizontalOptions="Center"
VerticalOptions="End"
Margin="20"
WidthRequest="80"
HeightRequest="80"
CornerRadius="40"/>
</Grid>
</ContentPage>
My goals is to get recording to work and save it on another page which i havent created yet, but main focus right now is for recording to work
Absent_Reality
Absent_Reality•3d ago
Not sure if this will help, but You could use Android MediaRecorder for the stream https://developer.android.com/media/platform/mediarecorder
Absent_Reality
Absent_Reality•3d ago
There is also a use of it for Maui in Gerald's screen recorder sample
Absent_Reality
Absent_Reality•3d ago
GitHub
Plugin.Maui.ScreenRecording/src/Plugin.Maui.ScreenRecording/ScreenR...
Plugin.Maui.ScreenRecording provides the ability to record the screen from within your app - jfversluis/Plugin.Maui.ScreenRecording

Did you find this page helpful?