Golang

 

 

In this tutorial, we’ll be creating a basic music player application using the Go programming language. This player will allow you to play audio files such as MP3s and WAVs. The goal is to familiarize you with Go’s interaction with external libraries and to give you hands-on experience building a simple desktop application.

Objective

The objective of this program is to:

  • Learn how to use Go for building a basic multimedia application.
  • Understand how to load and play audio files in Go.
  • Get familiar with external Go libraries for multimedia processing.

Go Music Player Code

package main

import (
    "fmt"
    "os"
    "log"
    "github.com/hajimehoshi/oto/v2"
    "github.com/hajimehoshi/go-mp3"
)

func main() {
    fmt.Println("Welcome to the Go Music Player!")

    // Load the MP3 file
    file, err := os.Open("your-audio-file.mp3")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    // Decode the MP3 file
    decoder, err := mp3.NewDecoder(file)
    if err != nil {
        log.Fatal(err)
    }

    // Initialize the audio player
    context, err := oto.NewContext(decoder.SampleRate(), 2, 2, 1024)
    if err != nil {
        log.Fatal(err)
    }
    defer context.Close()

    // Create a new player
    player := context.NewPlayer()
    defer player.Close()

    // Play the audio
    if _, err := decoder.DecodeTo(player); err != nil {
        log.Fatal(err)
    }

    fmt.Println("Now playing your music...")
    // Wait until the music is finished
    select {}
}

Explanation of the Program Structure

The program consists of the following key parts:

  • Importing Libraries: The program uses external Go libraries hajimehoshi/oto for audio playback and hajimehoshi/go-mp3 for decoding MP3 files. These libraries provide the necessary functionality to handle audio files in a Go application.
  • Loading the MP3 File: The program opens the specified MP3 file (replace “your-audio-file.mp3” with the actual file name). It uses os.Open to open the file for reading.
  • Decoding the MP3 File: The mp3.NewDecoder function is used to decode the MP3 file and get audio data.
  • Creating Audio Context and Player: The oto.NewContext initializes the audio context, which represents the system’s sound output. The NewPlayer method creates a player that will output the audio.
  • Playing the Audio: The decoder.DecodeTo(player) method sends the decoded audio data to the player to be played.
  • Infinite Loop: The select {} at the end of the program ensures the application continues running so the audio can be played to completion. Without it, the program would exit immediately after starting the audio.

How to Run the Program

  1. Install Go on your system (if you haven’t already) by following the instructions on the official Go installation guide.
  2. Install the required libraries using the Go command line:
    go get github.com/hajimehoshi/oto/v2
    go get github.com/hajimehoshi/go-mp3
  3. Save the Go program code in a file, for example music_player.go.
  4. Place an MP3 file in the same directory as the program and update the code with the file name.
  5. Run the Go program by executing:
    go run music_player.go
  6. Enjoy your music!
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)