-
Notifications
You must be signed in to change notification settings - Fork 5
/
media_information.go
168 lines (142 loc) · 3.39 KB
/
media_information.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"log"
vlc "github.com/adrg/libvlc-go/v3"
)
func main() {
// Initialize libVLC. Additional command line arguments can be passed in
// to libVLC by specifying them in the Init function.
if err := vlc.Init("--no-video", "--quiet"); err != nil {
log.Fatal(err)
}
defer vlc.Release()
// Create a new list player.
lp, err := vlc.NewListPlayer()
if err != nil {
log.Fatal(err)
}
defer func() {
lp.Stop()
lp.Release()
}()
// Create a new media list.
list, err := vlc.NewMediaList()
if err != nil {
log.Fatal(err)
}
defer list.Release()
// Add media to list.
media, err := vlc.NewMediaFromPath("localpath/test.mp3")
if err != nil {
log.Fatal(err)
}
// Parse media metadata asynchronously.
err = media.ParseWithOptions(0, vlc.MediaParseLocal, vlc.MediaParseNetwork)
if err != nil {
log.Fatal(err)
}
// Add media to media list.
err = list.AddMedia(media)
if err != nil {
log.Fatal(err)
}
// Set player media list.
if err = lp.SetMediaList(list); err != nil {
log.Fatal(err)
}
// Retrieve player event manager.
manager, err := lp.EventManager()
if err != nil {
log.Fatal(err)
}
// Create event handler.
quit := make(chan struct{})
eventCallback := func(event vlc.Event, userData interface{}) {
switch event {
case vlc.MediaListPlayerPlayed:
log.Println("Player end reached")
close(quit)
case vlc.MediaListPlayerNextItemSet:
// Retrieve underlying player.
p, err := lp.Player()
if err != nil {
log.Println(err)
break
}
// Retrieve currently playing media.
media, err := p.Media()
if err != nil {
log.Println(err)
break
}
// Get media location.
location, err := media.Location()
if err != nil {
log.Println(err)
break
}
log.Println("Media location:", location)
case vlc.MediaParsedChanged:
// Retrieve media item from user data.
media, ok := userData.(*vlc.Media)
if !ok {
break
}
parseStatus, err := media.ParseStatus()
if err != nil {
log.Println(err)
break
}
log.Println("Media parse status:", parseStatus)
if parseStatus != vlc.MediaParseDone {
break
}
// Get media title and artist metadata.
title, err := media.Meta(vlc.MediaTitle)
if err != nil {
log.Println(err)
break
}
artist, err := media.Meta(vlc.MediaArtist)
if err != nil {
log.Println(err)
break
}
log.Println("Media title:", title)
log.Println("Media artist:", artist)
}
}
// Register events with the event manager.
events := []vlc.Event{
vlc.MediaListPlayerPlayed,
vlc.MediaListPlayerNextItemSet,
}
var eventIDs []vlc.EventID
for _, event := range events {
eventID, err := manager.Attach(event, eventCallback, nil)
if err != nil {
log.Fatal(err)
}
eventIDs = append(eventIDs, eventID)
}
// De-register attached events.
defer manager.Detach(eventIDs...)
// Register media parsed event with the media event manager.
mEventManager, err := media.EventManager()
if err != nil {
log.Fatal(err)
}
// Pass media object as user data in order to retrieve it in the callback
// function. This is useful in order to avoid keeping references to the
// media file.
eventID, err := mEventManager.Attach(vlc.MediaParsedChanged, eventCallback, media)
if err != nil {
log.Fatal(err)
}
defer mEventManager.Detach(eventID)
// Start playing the media list.
if err = lp.Play(); err != nil {
log.Fatal(err)
}
<-quit
}