1. /**
    2. @author CuiZhouwei
    3. @date 2022/7/29
    4. **/
    5. package main
    6. import (
    7. "bufio"
    8. "fmt"
    9. "os"
    10. "strconv"
    11. "strings"
    12. "xushiwei-book/music/library"
    13. "xushiwei-book/music/play"
    14. )
    15. var lib *library.MusicManager
    16. var id int = 0
    17. func main() {
    18. fmt.Println(`Enter following commands to control the player:
    19. lib list --View the existing music lib
    20. lib add <name><artis><source><type> --Add a music to the music lib,the music type can use MP3 or WAV
    21. lib remove <name> --Remove the specified music by it name from the music lib
    22. play <name> --Play the specified music by it name
    23. q | e --quit or exit
    24. `)
    25. lib = library.NewMusicManager()
    26. r := bufio.NewReader(os.Stdin)
    27. for {
    28. fmt.Println("Enter command ->")
    29. rawline, _, _ := r.ReadLine()
    30. line := string(rawline)
    31. if line == "q" || line == "e" {
    32. break
    33. }
    34. tokens := strings.Split(line, " ")
    35. if tokens[0] == "lib" {
    36. handleCommands(tokens)
    37. } else if tokens[0] == "play" {
    38. handlePlay(tokens)
    39. } else {
    40. fmt.Println("Unrecognized command", tokens[0])
    41. }
    42. }
    43. }
    44. func handleCommands(tokens []string) {
    45. if len(tokens) < 2 {
    46. fmt.Println(`
    47. Enter following commands to control the player:
    48. lib list -- View the existing music lib
    49. lib add <name><artist><source><type> -- Add a music to the music lib ,the music type can use MP3 or WAV
    50. lib remove name -- Remove the specified music by it name from the lib
    51. `)
    52. return
    53. }
    54. switch tokens[1] {
    55. case "list":
    56. fmt.Println("序号 id 歌名 作者 路径 类型")
    57. for i := 0; i < lib.Len(); i++ {
    58. e, err := lib.Get(i)
    59. if err != nil {
    60. fmt.Println("get music err")
    61. }
    62. fmt.Printf("%-4d %-8s %-10s %-12s %-20s %-5s\n", i+1, e.Id, e.Name, e.Artist, e.Source, e.Type)
    63. }
    64. case "add":
    65. if len(tokens) == 6 {
    66. id++
    67. lib.Add(&library.Music{
    68. Id: strconv.Itoa(id),
    69. Name: tokens[2],
    70. Artist: tokens[3],
    71. Source: tokens[4],
    72. Type: tokens[5],
    73. })
    74. fmt.Printf("add music %s success\n", tokens[2])
    75. } else {
    76. fmt.Println("USAGE:lib add <name><artist><source><type>")
    77. }
    78. case "remove":
    79. if len(tokens) == 3 {
    80. fmt.Println("music_id 歌名 作者 路径 类型")
    81. _, e := lib.Find(tokens[2])
    82. if e == nil {
    83. fmt.Println("this music not in lib,add it to lib")
    84. return
    85. }
    86. fmt.Printf("%-8s %-10s %-12s %-20s %-5s\n", e.Id, e.Name, e.Artist, e.Source, e.Type)
    87. lib.RemoveByName(tokens[2])
    88. } else {
    89. fmt.Println("USAGE:lib remove <name>")
    90. }
    91. default:
    92. fmt.Println("unrecongized lib command:", tokens[1])
    93. }
    94. }
    95. func handlePlay(tokens []string) {
    96. if len(tokens) != 2 {
    97. fmt.Println("USAGE:play <name>")
    98. return
    99. }
    100. _, e := lib.Find(tokens[1])
    101. if e == nil {
    102. fmt.Println("The music ", tokens[1], "does not exist.")
    103. return
    104. }
    105. play.PlayAll(e.Source, e.Type, e.Name)
    106. }