runtime
Package runtime is the Go API of the Drift media plugin (github.com/go-drift/drift/plugins/media/plugin in drift.yaml): video in the widget tree and audio players without a view.
import media "github.com/go-drift/drift/plugins/media/runtime"
// In a State's InitState:
s.video, err = media.NewVideoPlayerController()
if err != nil { ... }
core.UseDisposable(s, s.video)
core.UseListenable(s, s.video.State())
s.video.Load(url)
// In Build:
media.VideoPlayer{Controller: s.video, Height: 225}
Controller methods are safe from any goroutine and do not wait for playback. A controller publishes playback as state, State and Progress, which change on the UI thread (so core.UseListenable rebuilds on them), and failures as the Errors stream.
Variables
Playback failures, the same on both platforms. Errors streams them as errors matching these under errors.Is.
var (
// ErrSource indicates the media could not be loaded (network, URL,
// format, container parsing).
ErrSource = errors.New("media could not be loaded")
// ErrDecoder indicates the media could not be decoded or rendered
// (codec, audio track, DRM).
ErrDecoder = errors.New("media could not be decoded")
// ErrPlaybackFailed indicates any other playback failure.
ErrPlaybackFailed = errors.New("media playback failed")
)
type AudioPlayerController
AudioPlayerController controls a native audio player without a view; build the UI around it. Several can play at once. Call Dispose to release the native player. Subscribe to State, Progress and Errors before Load so no change is missed.
type AudioPlayerController struct {
// contains filtered or unexported fields
}
func NewAudioPlayerController
func NewAudioPlayerController() *AudioPlayerController
NewAudioPlayerController creates a controller; its native player is created on first use, so creation itself cannot fail.
func (*AudioPlayerController) Dispose
func (c *AudioPlayerController) Dispose()
Dispose releases the native player. Later calls return platform.ErrDisposed; Dispose itself is idempotent.
func (*AudioPlayerController) Load
func (c *AudioPlayerController) Load(url string) error
Load loads url for playback; Play starts it.
func (*AudioPlayerController) Pause
func (c *AudioPlayerController) Pause() error
Pause pauses playback.
func (*AudioPlayerController) Play
func (c *AudioPlayerController) Play() error
Play starts or resumes playback.
func (*AudioPlayerController) SeekTo
func (c *AudioPlayerController) SeekTo(position time.Duration) error
SeekTo seeks to position.
func (*AudioPlayerController) SetLooping
func (c *AudioPlayerController) SetLooping(looping bool) error
SetLooping sets whether playback restarts at the end.
func (*AudioPlayerController) SetPlaybackSpeed
func (c *AudioPlayerController) SetPlaybackSpeed(rate float64) error
SetPlaybackSpeed sets the playback rate (1 is normal speed).
func (*AudioPlayerController) SetVolume
func (c *AudioPlayerController) SetVolume(volume float64) error
SetVolume sets the volume, from 0 to 1.
func (*AudioPlayerController) Stop
func (c *AudioPlayerController) Stop() error
Stop stops playback and returns to the start; the media stays loaded.
type PlaybackState
PlaybackState is the playback state of a video or audio player.
type PlaybackState int
const (
// PlaybackStateIdle: no media loaded, or stopped.
PlaybackStateIdle PlaybackState = iota
// PlaybackStateBuffering: waiting for media data before playing.
PlaybackStateBuffering
// PlaybackStatePlaying: playing.
PlaybackStatePlaying
// PlaybackStateCompleted: reached the end of the media.
PlaybackStateCompleted
// PlaybackStatePaused: paused; Play resumes.
PlaybackStatePaused
)
func (PlaybackState) String
func (s PlaybackState) String() string
type Progress
Progress is how far playback has got.
type Progress struct {
// Position is the playback position.
Position time.Duration
// Duration is the media's duration.
Duration time.Duration
// Buffered is how far the media is buffered.
Buffered time.Duration
}
type VideoPlayer
VideoPlayer embeds a native video player view with built-in platform controls.
The native player provides standard controls (play/pause, seek bar, time display) on both platforms. No Drift overlay is needed.
Create a VideoPlayerController with core.UseDisposable and pass it to this widget:
s.video, err = media.NewVideoPlayerController()
...
core.UseDisposable(s, s.video)
core.UseListenable(s, s.video.State())
s.video.Load(url)
// in Build:
media.VideoPlayer{Controller: s.video, Height: 225}
Width and Height set explicit dimensions. Use layout widgets such as widgets.Expanded to fill available space.
type VideoPlayer struct {
core.RenderObjectBase
// Controller provides the native video player surface and playback control.
Controller *VideoPlayerController
// Width of the video player in logical pixels.
Width float64
// Height of the video player in logical pixels.
Height float64
// HideControls hides the native transport controls (play/pause, seek bar,
// time display). Use this when building custom Drift widget controls on
// top of the video surface.
HideControls bool
}
func (VideoPlayer) CreateRenderObject
func (v VideoPlayer) CreateRenderObject(ctx core.BuildContext) layout.RenderObject
CreateRenderObject creates the render object for this widget.
func (VideoPlayer) UpdateRenderObject
func (v VideoPlayer) UpdateRenderObject(ctx core.BuildContext, renderObject layout.RenderObject)
UpdateRenderObject updates the render object with new widget properties.
type VideoPlayerController
VideoPlayerController controls a native video player (ExoPlayer on Android, AVPlayer on iOS) shown by a VideoPlayer widget. It creates the native view at once, so its methods work right after construction; manage its lifetime with core.UseDisposable. Subscribe to State, Progress and Errors before Load so no change is missed.
type VideoPlayerController struct {
// contains filtered or unexported fields
}
func NewVideoPlayerController
func NewVideoPlayerController() (*VideoPlayerController, error)
NewVideoPlayerController creates a controller and its native player. It fails if the native player cannot be created, for example when the media plugin is missing from drift.yaml.
func (*VideoPlayerController) Dispose
func (c *VideoPlayerController) Dispose()
Dispose releases the native player. Later calls return platform.ErrDisposed; Dispose itself is idempotent.
func (*VideoPlayerController) Load
func (c *VideoPlayerController) Load(url string) error
Load loads url for playback; Play starts it.
func (*VideoPlayerController) Pause
func (c *VideoPlayerController) Pause() error
Pause pauses playback.
func (*VideoPlayerController) Play
func (c *VideoPlayerController) Play() error
Play starts or resumes playback.
func (*VideoPlayerController) SeekTo
func (c *VideoPlayerController) SeekTo(position time.Duration) error
SeekTo seeks to position.
func (*VideoPlayerController) SetLooping
func (c *VideoPlayerController) SetLooping(looping bool) error
SetLooping sets whether playback restarts at the end.
func (*VideoPlayerController) SetPlaybackSpeed
func (c *VideoPlayerController) SetPlaybackSpeed(rate float64) error
SetPlaybackSpeed sets the playback rate (1 is normal speed).
func (*VideoPlayerController) SetShowControls
func (c *VideoPlayerController) SetShowControls(show bool) error
SetShowControls shows or hides the native controls.
func (*VideoPlayerController) SetVolume
func (c *VideoPlayerController) SetVolume(volume float64) error
SetVolume sets the volume, from 0 to 1.
func (*VideoPlayerController) Stop
func (c *VideoPlayerController) Stop() error
Stop stops playback and returns to the start; the media stays loaded.
func (*VideoPlayerController) ViewID
func (c *VideoPlayerController) ViewID() int64
ViewID returns the native view's ID, 0 once disposed.
Generated by gomarkdoc