Skip to content

Media Devices

How to list the cameras and microphones of the browser with wnd.MediaDevices().List and show them in a list. The browser asks for permission first.

package main

import (
	"context"
	"fmt"
	"github.com/worldiety/option"
	"go.wdy.de/nago/application"
	"go.wdy.de/nago/pkg/std"
	"go.wdy.de/nago/presentation/core"
	"go.wdy.de/nago/presentation/ui"
	"go.wdy.de/nago/presentation/ui/alert"
	"go.wdy.de/nago/presentation/ui/list"
	"go.wdy.de/nago/web/vuejs"
	"time"
)

func main() {
	application.Configure(func(cfg *application.Configurator) {
		cfg.SetApplicationID("de.worldiety.tutorial_63")

		cfg.Serve(vuejs.Dist())
		cfg.SetDecorator(cfg.NewScaffold().Decorator())

		option.MustZero(cfg.StandardSystems())
		std.Must(std.Must(cfg.UserManagement()).UseCases.EnableBootstrapAdmin(time.Now().Add(time.Hour), "%6UbRsCuM8N$auy"))

		cfg.RootViewWithDecoration(".", func(wnd core.Window) core.View {
			mediaDevices := core.AutoState[[]core.MediaDevice](wnd)

			core.OnAppear(wnd, "list-devices", func(ctx context.Context) {
				wnd.MediaDevices().List(core.MediaDeviceListOptions{WithVideo: true, WithAudio: true}).Observe(func(t []core.MediaDevice, err error) {
					if err != nil {
						alert.ShowBannerError(wnd, err)
						return
					}

					mediaDevices.Update(t)
					fmt.Println("got media devices", t)
				})
			})

			return list.List(
				ui.ForEach(mediaDevices.Get(), func(dev core.MediaDevice) core.View {
					return list.Entry().Headline(dev.Label()).SupportingText(string(dev.ID()))
				})...,
			).Caption(ui.Text("media devices demo"))
		})
	}).Run()
}

View on GitHub ↗ · run it with go run go.wdy.de/nago/example/cmd/tutorial-63-mediadevices@latest