How to play the result of "mpc search"?

mpc play expects a position as argument, not a filename. A position in a playlist to be precise. You first have to add the file to a playlist, and then tell mpc to play that song from the playlist. But add will add the new song at the end of the playlist. To play the newly added song you have to know how long the playlist is.

Here are some suggestions how you can add a song and start playing it:

  1. Clear the playlist first:

    mpc clear
    mpc search title "when I was your man" | mpc add
    mpc play
    
  2. Insert as next and play next:

    mpc search title "when I was your man" | mpc insert
    mpc next
    
  3. Determine length of playlist to play last:

    mpc search title "when I was your man" | mpc add
    mpc play $(mpc playlist | wc -l)
    

If you look at what mpd play is supposed to do:

play <position>
  Starts playing the song-number specified. If none is specified, plays number 1.

you'll notice that it doesn't really make sense to specify a filename as position.

I'm not sure there's an easy way to do what you want with mpc because it doesn't support all commands MPD does (like addid).

If you don't want to keep your playlist, you can do

mpc clear; mpc search title "when I was your man" | mpc add; mpc play

which clears the playlist, adds the song(s) you want and plays the first song in the playlist.

The best I could come up with to play add a song to a playlist without clearing it and playing the song is

title="your song title here"
mpc search title $title  | mpc add
mpc playlist -f '%position% %title%' | grep -i $title | head -n1 | cut - -d " " -f 1 | xargs mpc play

There is a simpler way than the accepted answer (in case the song is already in your playlist):

mpc searchplay filename "$(mpc search title 'when I was your man')"

If no result is found, mpc will simply play the first song in your playlist.

If you don't want the output of mpc, just append 2>&1 >/dev/null.

Tags:

Mpd