/**@author CuiZhouwei@date 2022/7/29**/package mainimport ( "bufio" "fmt" "os" "strconv" "strings" "xushiwei-book/music/library" "xushiwei-book/music/play")var lib *library.MusicManagervar id int = 0func main() { fmt.Println(`Enter following commands to control the player:lib list --View the existing music liblib add <name><artis><source><type> --Add a music to the music lib,the music type can use MP3 or WAVlib remove <name> --Remove the specified music by it name from the music lib play <name> --Play the specified music by it nameq | e --quit or exit`) lib = library.NewMusicManager() r := bufio.NewReader(os.Stdin) for { fmt.Println("Enter command ->") rawline, _, _ := r.ReadLine() line := string(rawline) if line == "q" || line == "e" { break } tokens := strings.Split(line, " ") if tokens[0] == "lib" { handleCommands(tokens) } else if tokens[0] == "play" { handlePlay(tokens) } else { fmt.Println("Unrecognized command", tokens[0]) } }}func handleCommands(tokens []string) { if len(tokens) < 2 { fmt.Println(` Enter following commands to control the player: lib list -- View the existing music lib lib add <name><artist><source><type> -- Add a music to the music lib ,the music type can use MP3 or WAV lib remove name -- Remove the specified music by it name from the lib `) return } switch tokens[1] { case "list": fmt.Println("序号 id 歌名 作者 路径 类型") for i := 0; i < lib.Len(); i++ { e, err := lib.Get(i) if err != nil { fmt.Println("get music err") } fmt.Printf("%-4d %-8s %-10s %-12s %-20s %-5s\n", i+1, e.Id, e.Name, e.Artist, e.Source, e.Type) } case "add": if len(tokens) == 6 { id++ lib.Add(&library.Music{ Id: strconv.Itoa(id), Name: tokens[2], Artist: tokens[3], Source: tokens[4], Type: tokens[5], }) fmt.Printf("add music %s success\n", tokens[2]) } else { fmt.Println("USAGE:lib add <name><artist><source><type>") } case "remove": if len(tokens) == 3 { fmt.Println("music_id 歌名 作者 路径 类型") _, e := lib.Find(tokens[2]) if e == nil { fmt.Println("this music not in lib,add it to lib") return } fmt.Printf("%-8s %-10s %-12s %-20s %-5s\n", e.Id, e.Name, e.Artist, e.Source, e.Type) lib.RemoveByName(tokens[2]) } else { fmt.Println("USAGE:lib remove <name>") } default: fmt.Println("unrecongized lib command:", tokens[1]) }}func handlePlay(tokens []string) { if len(tokens) != 2 { fmt.Println("USAGE:play <name>") return } _, e := lib.Find(tokens[1]) if e == nil { fmt.Println("The music ", tokens[1], "does not exist.") return } play.PlayAll(e.Source, e.Type, e.Name)}