如何使用类将歌曲添加到播放列表程序

时间:2014-11-10 02:19:24

标签: c++ visual-studio-2010 file class playlist

我试图修复我的" addSongtoPlaylist"功能。截至目前它还没有工作,我不确定我的陈述是否在功能上有误,但我现在已经被困在那一部分了。该程序使用GUI从歌曲列表中创建播放列表。我本来应该能够加载歌曲列表,添加,删除和播放表格中的歌曲。任何帮助都将非常感谢这一功能。

#include "globals.h"  //some global variables are included here
#include <cstdlib>  //standard c library
#include "mediaItem.h"  //the mediaItem class  (you need to write a class that extends this class)
#include "song.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


ifstream infile;
song mySong[MAX_SONGS];
int NumberOfSongs;
int myPlaylist[MAX_PLAYLIST];
int PlaylistSize; //counter for size of playlist in array
song *newPlaylist[MAX_PLAYLIST];

bool loadSongList(string filename)
{
    infile.open(filename);
    string tempArtist, tempTitle, tempLocation;
    int i = 0;

    if (infile.fail())
    {
        cout << "The file could not be opened." << endl;
        return false;
    }
    else
    {
        cout << "File opened successfully." << endl;

        infile >> NumberOfSongs;
        infile.ignore(1);

        while (!infile.eof())
        {
            getline(infile, tempArtist);
            getline(infile, tempTitle);
            getline(infile, tempLocation);
            mySong[i].setArtist(tempArtist);
            mySong[i].setTitle(tempTitle);
            mySong[i].setLocation(tempLocation);

            i++;
        }
        infile.close();

        return true;
    }
}

int getNumberOfSongsInSongList()
{
    return NumberOfSongs;
}

string getSongNameFromSongList(int songNum)
{
    string tempArtist, tempTitle;

    tempArtist = mySong[songNum].getArtist();
    tempTitle = mySong[songNum].getTitle();

    return tempArtist + " - " + tempTitle;
}

string getSongNameFromPlaylist(int playlistSpot)
{
    string tempArtist, tempTitle;

    tempArtist = mySong[myPlaylist[playlistSpot]].getArtist();
    tempTitle = mySong[myPlaylist[playlistSpot]].getTitle();

    return tempArtist + " - " + tempTitle;
}

void addSongToPlaylist(int songNum)
{
    mySong[myPlaylist[PlaylistSize]].setArtist(mySong[songNum].getArtist());
    mySong[myPlaylist[PlaylistSize]].setTitle(mySong[songNum].getTitle());
    mySong[myPlaylist[PlaylistSize]].setLocation(mySong[songNum].getLocation());

    mySong[myPlaylist[PlaylistSize]] = mySong[songNum];
    newPlaylist[PlaylistSize] = &mySong[songNum];
    PlaylistSize++;

}

int getNumberOfSongsInPlaylist()
{
    return PlaylistSize;
}

void moveSongDownInPlaylist(int playlistSpot)
{
    int temp;
    if (playlistSpot >= PlaylistSize - 1)
        return;

    temp = myPlaylist[playlistSpot + 1];
    myPlaylist[playlistSpot + 1] = myPlaylist[playlistSpot];
    myPlaylist[playlistSpot] = temp;
}

void removeSongFromPlaylist(int playlistSpot)
{
    for (int i = playlistSpot; i < PlaylistSize; i++)
        moveSongDownInPlaylist(i);

    myPlaylist[PlaylistSize - 1] = 0;
    PlaylistSize--;
}

void clearPlaylist()
{
    for (int i = 0; i < PlaylistSize; i++)
        myPlaylist[i] = 0;

    PlaylistSize = 0;
}

void moveSongUpInPlaylist(int playlistSpot)
{
    int temp;

    if (playlistSpot == 0)
        return;

    temp = myPlaylist[playlistSpot - 1];
    myPlaylist[playlistSpot - 1] = myPlaylist[playlistSpot];
    myPlaylist[playlistSpot] = temp;
}

void playSongFromPlaylist(int playlistSpot)
{
    mySong[myPlaylist[playlistSpot]].playMedia();
}

void pauseSongFromPlaylist(int playlistSpot)
{
    mySong[myPlaylist[playlistSpot]].pauseMedia();
}

void stopSongFromPlaylist(int playlistSpot)
{
    mySong[myPlaylist[playlistSpot]].stopMedia();
}

1 个答案:

答案 0 :(得分:2)

我怀疑您的addSongToPlaylist功能只需将songNum添加到myPLaylist的末尾。

myPlaylist看起来应该只是int而不是int *的数组。注意:目前你正在分配一个指针数组,但没有为指针分配内存。

列表中的每个int都是引用mySong中现有条目的歌曲编号。

我的建议:

  • myPlayList更改为int数组并修复所有解除引用它的代码(无处不在的*)。
  • 更改addSongToPLaylist只是将歌曲编号添加到数组的末尾,由(名称混淆)PlayListSize表示。

如果您遇到问题,请询问。

编辑: 由于myPlaylist需要song *myPlaylist[MAX_SONGS],您仍然可以执行与上述相同的操作,但不是将歌曲编号添加到列表的末尾,而是在{{{{{{{ 1}}数组。类似的东西:

mySong