Jump to content
  • 0
Sign in to follow this  
obeeey

How to connect zenity with mplayer?

Question

Hello 🙂

I have to create a simple music player using mplayer package and I wanted to connect it with zenity. I don't know which part of code is most essential, but the main problem that I'm having right now is that when I choose certain option (e.g. Play Random)  then the zenity window disappears and I can only input "q" in terminal to make the zenity window with options visible again.

My question is, how to leave dialog  window "on top" when executing some option?

And some code:

function Menu {

	menu=("Music directory ($DIR)" "Play random" "Next" "Before" "Search by title" "Radio" "Close")

	odp=`zenity --list --column=Menu "${menu[@]}" --height 350 --width 330`

	case "$odp" in

		"Music directory ($DIR)") input=1;;

		"Play random") input=2;;

		"Next") input=3;;

		"Before") input=4;;

		"Search by title") input=5;;

		"Radio") input=6;;

		"Close") input=7;;

	esac

}


function main {

	Menu

	while [ $input -ne 7 ]; do

		case "$input" in

		"1") ChangeDir;;

		"2") PlayRandom;;

		"3") ;;

		"4") ;;

		"5") PlayExact;;

		"6") PlayRadio;;

		esac

		Menu

	done
}


function PlayRandom {

	DO=$DIR

	DO+="*"

	TASK=`mplayer -shuffle $DO` 
  
}

 

jak.png

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hi,

You should run Zenity in an infinite loop and ask for user input in every iteration. This is the only way to re open Zenity menu once you've selected an option from the list.

Here is an example:



menu=("Message A" "Message B" "Exit")

while [ "$CONTINUE" == "y" ]
do
	input=`zenity --list --column=Menu "${menu[@]}" --height 350 --width 330`
	
	case "$input" in
		"Message A") msg="Hello World" ;;
		"Message B") msg="Welcome to LinuxHint!" ;;
		"Exit") 
			msg="Thank you for using this app" 
			CONTINUE="n" ;;
	esac
	
	if [ -n "$msg" ]
	then
		zenity --info --title "Message"  --text "$msg"
	fi
done

 

image.png.8091a9f1dacaeafe686f2b29f6692d9f.png

 

 

 

Here, is the result

w2qC20O5gQ.thumb.gif.ac22227668e3f65edd04131ba9ba8021.gif

 

 

I hope this helps.

Shahriar Shovon

Share this post


Link to post
Share on other sites
  • 0

Thank you very much @Shahriar Shovon for your answer 🙂

Unfortunately I may be doing something wrong, when trying to combine evoking function with zenity window it just opens first window and I have to close it to execute the function. 

This is how I tried to do this:

CONTINUE="y"
  
function Menu {
while [ "$CONTINUE" == "y" ]
do
	menu=("Music directory ($DIR)" "Play random" "Next" "Before" "Search by title" "Radio" "Close")

	odp=`zenity --list --column=Menu "${menu[@]}" --height 350 --width 330`

	case "$odp" in

		"Music directory ($DIR)") input=1;;

		"Play random") input=2;;

		"Next") input=3;;

		"Before") input=4;;

		"Search by title") input=5;;

		"Radio") input=6;;

		"Close") input=7
			 CONTINUE="n";;
	esac
	if [ $input == 6 ]
	zenity --info --title "Radio" --text "Enter 'q' to quit"
	then PlayRadio
	elif [ $input == 2 ]
    zenity --info --title "Random" --text "Test"
	then PlayRandom
	fi
done
}

 

Share this post


Link to post
Share on other sites
  • 0
On 5/14/2020 at 12:58 AM, obeeey said:

Thank you very much @Shahriar Shovon for your answer 🙂

Unfortunately I may be doing something wrong, when trying to combine evoking function with zenity window it just opens first window and I have to close it to execute the function. 

This is how I tried to do this:


CONTINUE="y"
  
function Menu {
while [ "$CONTINUE" == "y" ]
do
	menu=("Music directory ($DIR)" "Play random" "Next" "Before" "Search by title" "Radio" "Close")

	odp=`zenity --list --column=Menu "${menu[@]}" --height 350 --width 330`

	case "$odp" in

		"Music directory ($DIR)") input=1;;

		"Play random") input=2;;

		"Next") input=3;;

		"Before") input=4;;

		"Search by title") input=5;;

		"Radio") input=6;;

		"Close") input=7
			 CONTINUE="n";;
	esac
	if [ $input == 6 ]
	zenity --info --title "Radio" --text "Enter 'q' to quit"
	then PlayRadio
	elif [ $input == 2 ]
    zenity --info --title "Random" --text "Test"
	then PlayRandom
	fi
done
}

 

I think you have to run the PlayRadio function in the background first, and then run zenity. 

You should be able to run PlayRadio function in the background as follows:

PlayRadio &

Share this post


Link to post
Share on other sites
  • 0

@Shahriar Shovonthank you, it worked! Now the music plays in the background and the zenity window appears.

But... of course, another problem, is there a simple way to end playing music when the user closes zenity window which informes about played music?

Share this post


Link to post
Share on other sites
  • 0
4 hours ago, obeeey said:

@Shahriar Shovonthank you, it worked! Now the music plays in the background and the zenity window appears.

But... of course, another problem, is there a simple way to end playing music when the user closes zenity window which informes about played music?

Great!

 

You should be able to kill all the mplayer processes if zenity is closed.

The following command should find all the mplayer processes and kill them for you.

$ pkill mplayer

Share this post


Link to post
Share on other sites
  • 0
18 hours ago, obeeey said:

@Shahriar Shovonthank you again 🙂

All my questions have been answererd... topic can be closed, thanks again!

Great. You're welcome. 

 

Stay safe and Have a great day.

Shariar Shovon

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...