Fandom

Music Player Daemon Community Wiki

Hack:ralbum

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Simple C program that clears the current playlist, queues a random album and plays it. libmpd is required to compile, make sure you tell gcc to use it with -lmpd.

  • Source
#include <stdio.h>
#include <stdlib.h>
#include <libmpd/libmpd.h>
#include <string.h>

void playAlbum(MpdObj *mo, char* album);

int main(int argc, char **argv)
{
   int flag, i, random, count = 0;   
   
	MpdObj *mo;
	MpdData *data = NULL;

	mo = mpd_new_default();

	if(mpd_connect(mo))
	{
		mpd_free(mo);
		printf("Failed to connect\n");
		return 1;
	}
   
   MpdData *albums = NULL;
	
	srand(time(NULL));

	random = (int)(rand() % mpd_stats_get_total_albums(mo) );
	
	albums = mpd_database_get_albums(mo, NULL);
	for(i = 0; i < random; i++)
   {
      albums = mpd_data_get_next(albums);
   }

	playAlbum(mo, albums->tag);
	
	mpd_free(mo);
	exit(0);
}

void playAlbum(MpdObj *mo, char* album)
{
	mpd_playlist_clear(mo);
			
	MpdData *data = mpd_database_find(mo, MPD_TABLE_ALBUM, album, TRUE);
	while (data != NULL)
	{
		if(data->type == MPD_DATA_TYPE_SONG)
		{
			mpd_playlist_queue_add(mo,data->song->file);
		}
		data = mpd_data_get_next(data);
	}

	mpd_playlist_queue_commit(mo);
	mpd_player_play(mo);
}

Also on Fandom

Random Wiki