-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (146 loc) · 4.02 KB
/
main.go
File metadata and controls
164 lines (146 loc) · 4.02 KB
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
package main
import (
"context"
"errors"
"fmt"
"path/filepath"
"runtime"
"time"
"github.com/limiu82214/ePhotoPlayer/internal"
"github.com/limiu82214/ePhotoPlayer/internal/model"
"github.com/limiu82214/ePhotoPlayer/internal/utils"
"fyne.io/fyne/v2/app"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
)
func main() {
runtime.LockOSThread() // for key fyne
var system internal.System
switch runtime.GOOS {
case "darwin":
fmt.Println("SYS: Running on macOS")
system = model.NewMac()
case "linux":
fmt.Println("SYS: Running on Linux")
system = model.NewRaspberry3B()
default:
fmt.Printf("未知作業系統: %s\n", runtime.GOOS)
}
addedChan, removedChan, err := system.MonitorVolumes()
if err != nil {
fmt.Println("Error monitoring volumes:", err)
return
}
// 第一次直接拿可以拿到的
firstChan := make(chan string)
addedChan = utils.MixStrChan(addedChan, firstChan)
paths, err := system.GetVolumePaths()
if err != nil {
fmt.Println("Error getting volume paths:", err)
return
}
if len(paths) > 0 {
firstChan <- paths[0]
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mainApp := app.New()
// 設定一個固定的 targetSampleRate,例如 44100 Hz
targetSampleRate := beep.SampleRate(44100)
// 初始化 speaker 使用固定的採樣率
speaker.Init(targetSampleRate, targetSampleRate.N(time.Second/10))
defer speaker.Close()
var op *internal.AppOperator
var opCtx context.Context = context.Background()
var currentVolumePath string
go func() {
time.Sleep(1 * time.Second)
for {
select {
case addedPath := <-addedChan:
fmt.Println("SYS: USB added:", addedPath)
if currentVolumePath == "" {
currentVolumePath = addedPath
var imagePaths, mp3Paths []string
for range 5 {
imagePaths, mp3Paths, err = getPath(system, currentVolumePath)
if err != nil {
fmt.Println("Error getting paths:", err)
time.Sleep(1 * time.Second)
} else {
break
}
}
if err != nil {
continue
}
op = internal.NewAppOperator(ctx, mainApp)
opCtx = op.GetCtx()
op.SetImagePath(imagePaths)
op.SetMp3Path(mp3Paths)
go op.RunSlideShow()
go op.RunMusicPlayer()
}
case removedPath := <-removedChan:
fmt.Println("SYS: USB removed:", removedPath)
if removedPath == currentVolumePath {
op.Close()
op = nil
opCtx = context.Background()
currentVolumePath = ""
}
case <-opCtx.Done():
fmt.Println("SYS: OP closed")
op.Close()
op = nil
opCtx = context.Background()
currentVolumePath = ""
}
}
}()
// app
fmt.Println("SYS: Running app")
mainApp.NewWindow("ePhotoPlayer keepAlive").Show()
mainApp.Run()
fmt.Print("SYS: App closed")
}
func getPath(system internal.System, volumePath string) (imagePaths, mp3Paths []string, err error) {
fmt.Println("SYS: Getting USB volume paths")
// volume
paths, err := system.GetVolumePaths()
if err != nil {
return nil, nil, fmt.Errorf("Error getting volume paths: %w", err)
}
if len(paths) == 0 {
return nil, nil, errors.New("No removable volumes found")
}
// path
basePath := paths[0]
photosPath := filepath.Join(basePath, "photos")
mp3Path := filepath.Join(basePath, "music")
imagePaths, err = system.ListFilePaths(photosPath)
if err != nil {
return nil, nil, fmt.Errorf("Error listing files: %w", err)
}
imagePaths = utils.FilterImagePaths(imagePaths)
utils.ShuffleStrings(imagePaths)
fmt.Print("SYS: Image paths counts ", len(imagePaths))
if len(imagePaths) > 0 {
fmt.Println(" - first path:", imagePaths[0])
} else {
fmt.Println(" - no image files found")
}
mp3Paths, err = system.ListFilePaths(mp3Path)
if err != nil {
return nil, nil, fmt.Errorf("Error listing files: %w", err)
}
mp3Paths = utils.FilterMp3Paths(mp3Paths)
utils.ShuffleStrings(mp3Paths)
fmt.Print("SYS: MP3 paths counts ", len(mp3Paths))
if len(mp3Paths) > 0 {
fmt.Println(" - first path:", mp3Paths[0])
} else {
fmt.Println(" - no mp3 files found")
}
return imagePaths, mp3Paths, nil
}