0% found this document useful (0 votes)
14 views9 pages

Music Playlist Creator

The document outlines a music playlist manager program that allows users to add, remove, and play songs using arrays and file handling. It includes an algorithm for loading a playlist from a file, displaying a menu, and processing user choices to add songs or display the playlist. The source code is provided in C, demonstrating functions for saving/loading playlists, adding songs, and displaying song details.

Uploaded by

hemakothand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Music Playlist Creator

The document outlines a music playlist manager program that allows users to add, remove, and play songs using arrays and file handling. It includes an algorithm for loading a playlist from a file, displaying a menu, and processing user choices to add songs or display the playlist. The source code is provided in C, demonstrating functions for saving/loading playlists, adding songs, and displaying song details.

Uploaded by

hemakothand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MUSIC PLAYLIST CREATOR

AIM:
Build a playlist manager to add, remove, and play songs. Use
arrays and file handling to store playlist details.

ALGORTITHM:
Initialize:
• Load playlist from playlist.txt.
Display Menu:
• Show options: Add Song, Display Playlist, Exit.
Process Choice:
• Add Song:
 Get title, URL, and file path.
 Download song and save details if successful.
• Display Playlist:
 Print all song details.
• Exit:
 Save playlist to playlist.txt and quit.
Repeat:
• Loop until user chooses Exit.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_SONGS 100
#define MAX_LEN 100

// Modified Song structure (with title, filepath, and URL)


typedef struct {
char title[MAX_LEN];
char filepath[MAX_LEN]; // Path to the music file
char url[MAX_LEN]; // URL of the song
} Song;

Song playlist[MAX_SONGS];
int song_count = 0;

// Function to save playlist to file


void save_playlist_to_file(const char *filename) {
FILE *file = fopen(filename, "w");
if (!file) {
printf("Error saving playlist!\n");
return;
}
for (int i = 0; i < song_count; i++) {
fprintf(file, "%s;%s;%s\n", playlist[i].title, playlist[i].filepath, playlist[i].url);

}
fclose(file);
}

// Function to load playlist from file


void load_playlist_from_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) return;

song_count = 0;
while (fscanf(file, "%[^;];%[^;];%[^\n]\n", playlist[song_count].title,
playlist[song_count].filepath, playlist[song_count].url) != EOF) {
song_count++;
}
fclose(file);
}

// Function to add a song to the playlist


void add_song() {
if (song_count >= MAX_SONGS) {
printf("Playlist is full!\n");
return;
}
char url[MAX_LEN], filepath[MAX_LEN], command[MAX_LEN * 2];

// Prompt for song title


printf("Enter song title: ");
getchar(); // consume newline left by previous input
fgets(playlist[song_count].title, MAX_LEN, stdin);
playlist[song_count].title[strcspn(playlist[song_count].title, "\n")] = 0;

// remove newline

// Prompt for song URL


printf("Enter URL to download the music file: ");
fgets(url, MAX_LEN, stdin);
url[strcspn(url, "\n")] = 0; // remove newline
strcpy(playlist[song_count].url, url); // Store the URL

// Prompt for the file path to save the downloaded song


printf("Enter filepath to save the downloaded file (e.g., ./song.mp3): ");

fgets(filepath, MAX_LEN, stdin);


filepath[strcspn(filepath, "\n")] = 0; // remove newline

// Form the download command (Linux/Mac: wget, Windows:


PowerShell Invoke-WebRequest)
#ifdef _WIN32
snprintf(command, sizeof(command), "powershell Invoke-
WebRequest -Uri \"%s\" -OutFile \"%s\"", url, filepath);
#else
snprintf(command, sizeof(command), "wget \"%s\" -O \"%s\"", url, filepath);
#endif

// Attempt to download the file


printf("Downloading file...\n");
if (system(command) == 0) {
// If the download is successful, store the filepath in the playlist
strcpy(playlist[song_count].filepath, filepath);
song_count++;
save_playlist_to_file("playlist.txt");
printf("Song added and downloaded successfully!\n");
} else {
printf("Failed to download the song.\n");
}
}

// Function to display the playlist


void display_playlist() {
if (song_count == 0) {
printf("Playlist is empty!\n");
return;
}
printf("Current Playlist:\n");
for (int i = 0; i < song_count; i++) {
printf("%d. %s\n Filepath: %s\n URL: %s\n", i + 1, playlist[i].title,
playlist[i].filepath, playlist[i].url);
}
}
int main() {
load_playlist_from_file("playlist.txt");
int choice;
while (1) {
printf("\nPlaylist Manager\n");
printf("1. Add Song\n");
printf("2. Display Playlist\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear input buffer to prevent issues with fgets
switch (choice) {
case 1: add_song(); break;
case 2: display_playlist(); break;
case 3:
save_playlist_to_file("playlist.txt");
printf("Exiting...\n");
system("pause"); // Pause for Windows users, remove for
Linux/macOS
exit(0); // Exit the program properly
default: printf("Invalid choice!\n");
}
}
getch();
return 0;
}
OUTPUT:
Playlist Manager
1. Add Song
2. Display Playlist
3. Exit
Enter your choice: 1

Enter song title: Shape of You


Enter URL to download the music file:
https://example.com/shape_of_you.mp3
Enter filepath to save the downloaded file (e.g., ./song.mp3):
./shape_of_you.mp3
Downloading file...
Song added and downloaded successfully!

Playlist Manager
1. Add Song
2. Display Playlist
3. Exit
Enter your choice: 2

Current Playlist:
1. Shape of You
Filepath: ./shape_of_you.mp3
URL: https://example.com/shape_of_you.mp3
Playlist Manager
1. Add Song
2. Display Playlist
3. Exit
Enter your choice: 3
Exiting...

KEY NOTES:
If the song's URL is invalid or the download fails, the
program will display:

Downloading file...
Failed to download the song.

SAMPLE VIDEO:
LINK:https://drive.google.com/file/d/
1p1K733mGr6hKM6KZYDVJcIwKHs7TrMfA/view?usp=drive_link

You might also like