在播放 ffplay 等程序时将控件保留在命令行中(构建 mp3 播放器)

问题描述 投票:0回答:1

我想用 zsh(或 bash)编写一个 mp3 播放器。我想要实现的第一个功能是按“n”键时转到下一首歌曲,但是,当我启动程序时,ffplay 接管 bash 脚本,按“n”键没有任何效果。我怎样才能做到这一点(使用除 ffplay 之外的其他程序也可以,目标是 mp3 播放器而不是 ffplay)

mp3_player(){
  # in th  command line, the user must be in a directory field only with mp3 files

  idx=$1 # first index given by the user
  files=(*) # make an array with all the files inside the dir
  key="" # set the value of the key to empty
  
  while :
  do
    echo ${files[$((idx))]}
    ffplay -autoexit ${files[$((idx))]}
    read -t 1 -k 1 key # for zsh
    #read -t 1 -n 1 key # for bash
  
    if [[ $key == "n" ]]
    then
      idx=$((idx+1))
      key=""
    fi
  done
}
bash command-line-interface zsh mp3 ffplay
1个回答
0
投票

通常,如果您想在后台运行程序并保持控制台响应,则需要通过在行末尾附加“&”来将其作为作业启动:

#!/bin/bash

#some script there ...

command="ffplay -autoexit ${files[$((idx))]}" #(from your first example)
job_pid="$($command &)"
kill -SIGUSR1 "${job_pid}" # assuming process at job_pid expects SIGUSR1 and wil do something with it...

这样做将在标准输出上打印新进程的 pid(在我的示例代码中,该 pid 将存储在 job_pid 变量中)。

某些程序允许您通过过程信号与它们进行简单的交互(例如herbe:https://github.com/dudik/herbe) 还有其他选项,例如这个:https://cmus.github.io/

© www.soinside.com 2019 - 2024. All rights reserved.