forked from adrg/libvlc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.go
74 lines (60 loc) · 1.37 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package vlc
// #cgo LDFLAGS: -lvlc
// #include <vlc/vlc.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
)
// MediaState represents the state of a media file.
type MediaState uint
// Media states.
const (
MediaIdle MediaState = iota
MediaOpening
MediaBuffering
MediaPlaying
MediaPaused
MediaStopped
MediaEnded
MediaError
)
// Media is an abstract representation of a playable media file.
type Media struct {
media *C.libvlc_media_t
}
// NewMediaFromPath creates a Media instance from the provided path.
func NewMediaFromPath(path string) (*Media, error) {
return newMedia(path, true)
}
// NewMediaFromURL creates a Media instance from the provided URL.
func NewMediaFromURL(url string) (*Media, error) {
return newMedia(url, false)
}
// Release destroys the media instance.
func (m *Media) Release() error {
if m.media == nil {
return nil
}
C.libvlc_media_release(m.media)
m.media = nil
return getError()
}
func newMedia(path string, local bool) (*Media, error) {
if instance == nil {
return nil, errors.New("Module must be initialized first")
}
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
var media *C.libvlc_media_t
if local {
media = C.libvlc_media_new_path(instance, cPath)
} else {
media = C.libvlc_media_new_location(instance, cPath)
}
if media == nil {
return nil, getError()
}
return &Media{media: media}, nil
}