package com.ruoyi.utils;

import com.ruoyi.common.utils.StringUtils;
import javazoom.jl.player.Player;
import org.springframework.util.ResourceUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;

public class MP3PlayerUtils {
    private String filename;
    private Player player;

    public MP3PlayerUtils(String filename) {
        this.filename = filename;
    }

    public MP3PlayerUtils() {
    }

    public void play() {
        try {
            BufferedInputStream buffer = new BufferedInputStream(
                    new FileInputStream(filename));
            player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    public void stop(){
        player.close();
    }

    public static void playAudio(){
        String filenamename= StringUtils.EMPTY;
        try {
            File file= ResourceUtils.getFile("classpath:config/alarms.mp3");
            filenamename = file.getAbsolutePath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
//        "/Users/admin/Documents/git_project/mingxue/voluntarydata/voluntarydata-admin/src/main/resources/config/alarms.mp3";
        MP3PlayerUtils mp3 = new MP3PlayerUtils(filenamename);
        mp3.play();
        try {
            TimeUnit.MINUTES.sleep(1);
            mp3.stop();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        playAudio();
    }
}
