- add error handeling
- add loop parameter - improve documentation
This commit is contained in:
		| @@ -14,6 +14,11 @@ paths: | ||||
|           description: A base64 encoded version of the file name | ||||
|           schema: | ||||
|             type: string | ||||
|         - in: query | ||||
|           name: loop | ||||
|           description: Defaults to false; if true, will loop the sound until stopped | ||||
|           schema: | ||||
|             type: boolean | ||||
|       responses: | ||||
|         '200': | ||||
|           description: OK | ||||
|   | ||||
| @@ -66,12 +66,13 @@ func BufferSound(file string) bool { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func PlaySound(file string, index int) int { | ||||
| func PlaySound(file string, index int, loop bool) int { | ||||
| 	playbacks[index] = playback{ | ||||
| 		File:     file, | ||||
| 		IsLoaded: false, | ||||
| 		Streamer: nil, | ||||
| 		Control:  nil, | ||||
| 		Loop:     loop, | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println("Playing sound: " + file) | ||||
| @@ -93,10 +94,15 @@ func PlaySound(file string, index int) int { | ||||
| 		IsLoaded: true, | ||||
| 		Streamer: streamer, | ||||
| 		Control:  ctrl, | ||||
| 		Loop:     loop, | ||||
| 	} | ||||
| 	speaker.Play(ctrl) | ||||
| 	<-done | ||||
| 	fmt.Println("Finished playing sound: " + file) | ||||
| 	delete(playbacks, index) | ||||
| 	if playbacks[index].Loop { | ||||
| 		playbacks[index].Control.Paused = true | ||||
| 		PlaySound(file, index, loop) | ||||
| 		delete(playbacks, index) | ||||
| 	} | ||||
| 	return 1 | ||||
| } | ||||
|   | ||||
| @@ -16,6 +16,7 @@ type playback struct { | ||||
| 	IsLoaded bool | ||||
| 	Streamer beep.Streamer | ||||
| 	Control  *beep.Ctrl | ||||
| 	Loop     bool | ||||
| } | ||||
|  | ||||
| type playbackWebReturn struct { | ||||
|   | ||||
							
								
								
									
										16
									
								
								webRoutes.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								webRoutes.go
									
									
									
									
									
								
							| @@ -25,7 +25,13 @@ func handlePlay(w http.ResponseWriter, r *http.Request) { | ||||
| 	var cnt = r.URL.Query().Get("file")                 // Retrieve the file name from the query string | ||||
| 	bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") | ||||
| 		return | ||||
| 	} | ||||
| 	loop := r.URL.Query().Get("loop") // Retrieve the loop value from the query string | ||||
| 	loopBool := false | ||||
| 	if loop == "true" { | ||||
| 		loopBool = true | ||||
| 	} | ||||
|  | ||||
| 	t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists | ||||
| @@ -57,7 +63,7 @@ func handlePlay(w http.ResponseWriter, r *http.Request) { | ||||
| 	var currIndex = len(playbacks)                              // Create a new index for the playback | ||||
| 	fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex) // Return a JSON object to the user | ||||
|  | ||||
| 	go PlaySound(string(bytArr[:]), currIndex) // Play the sound | ||||
| 	go PlaySound(string(bytArr[:]), currIndex, loopBool) // Play the sound | ||||
|  | ||||
| } | ||||
|  | ||||
| @@ -74,7 +80,8 @@ func handleBufferAll(w http.ResponseWriter, r *http.Request) { | ||||
| 	var temp []string | ||||
| 	files, err := ioutil.ReadDir("./sounds/") // Read the directory | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") | ||||
| 		return | ||||
| 	} | ||||
| 	// Loop through the files and add the file name to the temp array | ||||
| 	// Also triggers the buffer process for the file | ||||
| @@ -109,7 +116,8 @@ func handleBuffer(w http.ResponseWriter, r *http.Request) { | ||||
| 	var cnt = r.URL.Query().Get("file")                 // Retrieve the file name from the query string | ||||
| 	bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists | ||||
|   | ||||
		Reference in New Issue
	
	Block a user