forked from progrium/darwinkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
87 lines (72 loc) · 2.09 KB
/
main.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
package main
import (
"fmt"
"github.com/progrium/macdriver/cocoa"
"github.com/progrium/macdriver/core"
"github.com/progrium/macdriver/dispatch"
"github.com/progrium/macdriver/objc"
"os"
"runtime"
"time"
)
func init() {
runtime.LockOSThread()
}
func main() {
go func() {
Run()
os.Exit(0)
}()
app := cocoa.NSApp()
app.SetActivationPolicy(cocoa.NSApplicationActivationPolicyProhibited)
app.ActivateIgnoringOtherApps(true)
app.Run()
}
func Run() {
ok := make(chan bool)
dispatch.Async(dispatch.MainQueue(), func() {
//data, err := os.ReadFile("/Users/anhoder/Desktop/1.mp3")
//fmt.Println(err)
handlerCls := objc.NewClass("EventHandler", "NSObject")
handlerCls.AddMethod("outputDeviceChanged:", func(notification objc.Object) {
fmt.Println(notification)
})
objc.RegisterClass(handlerCls)
handler := objc.Get("EventHandler").Alloc().Init()
cls := objc.NewClass("SoundDelegate", "NSObject")
cls.AddMethod("sound:didFinishPlaying:", func(sound objc.Object, didFinishPlaying bool) {
fmt.Println("finish playing: ", didFinishPlaying)
core.NSNotificationCenter_defaultCenter().
AddObserver_selector_name_object_(handler, objc.Sel("outputDeviceChanged:"), core.String("test"), objc.Get("AVAudioSession").Get("sharedInstance"))
if didFinishPlaying {
ok <- true
}
})
objc.RegisterClass(cls)
delegate := objc.Get("SoundDelegate").Alloc().Init()
url := core.NSURL_fileURLWithPath_isDirectory_(core.String("/Users/anhoder/Desktop/1.mp3"), false)
s := cocoa.NSSound_InitWithURL(url, false)
s.Set("delegate:", delegate)
s.SetVolume_(0.8)
fmt.Println(s.Volume())
s.SetName_(core.String("test 111"))
go func() {
<-time.After(time.Second * 30)
s.SetVolume_(1)
fmt.Println(s.Volume())
}()
go func() {
for {
ticker := time.Tick(time.Second)
<-ticker
fmt.Println(s.Name(), s.CurrentTime(), s.Duration())
}
}()
//d := core.NSData_WithBytes(data, uint64(len(data)))
//s := cocoa.NSSound_InitWithData(d)
s.Play()
})
<-ok
//player := objc.Get("AVAudioPlayer").Alloc().Init()
//player = player.Send("initWithContentsOfURL:", url)
}