Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #5 - Save volume of the mpv player #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ _station_list() {
jq -r '.[] | .name' <"$FAVORITE_PATH/$1.json"
}

# Function to get the saved volume or set it to default
get_saved_volume() {
if [ ! -f "$CONFIG_FILE" ]; then
echo "volume=100" > "$CONFIG_FILE"
fi
grep "volume" "$CONFIG_FILE" | cut -d '=' -f2
}

_play() {
echo
yellowprint "Press q to quit."
echo
mpv "$1" || {

# Get the saved volume from the config file
volume=$(get_saved_volume)

# Start mpv with saved volume level and the Lua script from the lib directory
mpv --volume="$volume" --script="./lib/save_volume.lua" "$1" || {
echo "Not able to play your station."
return 1
}
Expand Down
11 changes: 11 additions & 0 deletions lib/save_volume.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Save volume changes immediately to the config file
local config_file = os.getenv("HOME") .. "/.config/tera/tera_config.conf"

function save_volume()
local volume = mp.get_property("volume")
os.execute("mkdir -p " .. os.getenv("HOME") .. "/.config/tera")
os.execute("echo volume=" .. volume .. " > " .. config_file)
end

-- Register the function to listen for volume change events
mp.observe_property("volume", "native", save_volume)
6 changes: 6 additions & 0 deletions tera
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SCRIPT_DOT_DIR="$HOME/.config/tera"
# CACHE_DIR="$HOME/.cache"
FAVORITE_PATH="$SCRIPT_DOT_DIR/favorite"
TMP_PATH="$HOME/.cache/tera"
CONFIG_FILE="$SCRIPT_DOT_DIR/tera_config.conf"
FAVORITE_FILE="sample.json"
FAVORITE_FULL="${FAVORITE_PATH}/${FAVORITE_FILE}"
SCRIPT_NAME=$(basename "$0")
Expand Down Expand Up @@ -76,6 +77,11 @@ fi
if [ ! -d "$TMP_PATH" ]; then
mkdir -p "$TMP_PATH"
fi
# Ensure the configuration file exists, create it with default settings if not
if [ ! -f "$CONFIG_FILE" ]; then
mkdir -p "$(dirname "$CONFIG_FILE")"
echo "volume=100" > "$CONFIG_FILE" # Default volume set to 100%
fi
# TODO
# 1. If $FAVORITE_PATH has no file copy the sample.json
# 2. If $FAVORITE_PATH has one file and content is less than
Expand Down