Music player can now handle unicode filenames and m3u8 playlists, issue #261

--HG--
branch : develop
feature/pipeline-tools
kervala 9 years ago
parent a0f8660d0d
commit 9322fa87b3

@ -196,162 +196,176 @@ public:
InitMouseWithCursor (true); InitMouseWithCursor (true);
Driver->showCursor (true); Driver->showCursor (true);
if (false) //supportUnicode()) bool oggSupported = false;
{ bool mp3Supported = false;
}
else
{
bool oggSupported = false;
bool mp3Supported = false;
for(uint i = 0; i < extensions.size(); ++i) for(uint i = 0; i < extensions.size(); ++i)
{
if (extensions[i] == "ogg")
{ {
if (extensions[i] == "ogg") oggSupported = true;
{
oggSupported = true;
}
else if (extensions[i] == "mp3")
{
mp3Supported = true;
}
} }
else if (extensions[i] == "mp3")
{
mp3Supported = true;
}
}
std::vector<std::string> filters; std::vector<std::string> filters;
// supported formats // supported formats
filters.push_back("All Supported Files"); filters.push_back("All Supported Files"); // TODO: translate
std::string filter; std::string filter;
if (mp3Supported) filter += "*.mp3;*.mp2;*.mp1;"; if (mp3Supported) filter += "*.mp3;*.mp2;*.mp1;";
if (oggSupported) filter += "*.ogg;"; if (oggSupported) filter += "*.ogg;";
filter += "*.m3u"; filter += "*.m3u;*.m3u8";
filters.push_back(filter); filters.push_back(filter);
// mp3 format // mp3 format
if (mp3Supported) if (mp3Supported)
{ {
filters.push_back("MPEG Audio Files (*.mp3;*.mp2;*.mp1)"); filters.push_back("MPEG Audio Files (*.mp3;*.mp2;*.mp1)");
filters.push_back("*.mp3;*.mp2;*.mp1"); filters.push_back("*.mp3;*.mp2;*.mp1");
} }
// ogg format // ogg format
if (oggSupported) if (oggSupported)
{ {
filters.push_back("Vorbis Files (*.ogg)"); filters.push_back("Vorbis Files (*.ogg)");
filters.push_back("*.ogg"); filters.push_back("*.ogg");
} }
// playlist // playlist
filters.push_back("Playlist Files (*.m3u)"); filters.push_back("Playlist Files (*.m3u;*.m3u8)");
filters.push_back("*.m3u"); filters.push_back("*.m3u;*.m3u8");
// all files // all files
filters.push_back("All Files (*.*)"); filters.push_back("All Files (*.*)");
filters.push_back("*.*"); filters.push_back("*.*");
filters.push_back(""); filters.push_back("");
static char szFilter[1024] = { '\0' }; static wchar_t szFilter[1024] = { '\0' };
uint offset = 0; uint offset = 0;
for(uint i = 0; i < filters.size(); ++i) for(uint i = 0; i < filters.size(); ++i)
{ {
strcpy(szFilter + offset, filters[i].c_str()); wcscpy(szFilter + offset, utf8ToWide(filters[i]));
// move offset to string length + 1 for \0 // move offset to string length + 1 for \0
offset += filters[i].length() + 1; offset += filters[i].length() + 1;
} }
// Filename buffer
wchar_t buffer[1024];
buffer[0]=0;
OPENFILENAMEW ofn;
memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = Driver ? Driver->getDisplay():NULL;
ofn.hInstance = HInstance;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 0;
ofn.lpstrFile = buffer;
ofn.nMaxFile = sizeof(buffer);
ofn.lpstrTitle = (wchar_t*)NLMISC::CI18N::get("uiPlaySongs").c_str();
ofn.Flags = OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING|OFN_EXPLORER;
if (Driver)
Driver->beginDialogMode();
if (GetOpenFileNameW (&ofn))
{
bool useUtf8 = false;
// Filename buffer // Skip the directory name
char buffer[65535]; const wchar_t *bufferPtr = buffer;
buffer[0]=0;
// Multi filename ?
OPENFILENAME ofn; string path;
memset (&ofn, 0, sizeof(OPENFILENAME)); if (ofn.nFileOffset>wcslen(buffer))
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = Driver ? Driver->getDisplay():NULL;
ofn.hInstance = HInstance;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 0;
ofn.lpstrFile = buffer;
ofn.nMaxFile = sizeof(buffer);
ofn.lpstrTitle = "Play songs";
ofn.Flags = OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING|OFN_EXPLORER;
if (Driver)
Driver->beginDialogMode();
if (GetOpenFileName (&ofn))
{ {
// Skip the directory name // Backup the path and point to the next filename
const char *bufferPtr = buffer; path = wideToUtf8(buffer);
path += "\\";
bufferPtr += wcslen(bufferPtr)+1;
}
// Multi filename ? // Get selected files and playlists
string path; std::vector<std::string> filenames;
if (ofn.nFileOffset>strlen(buffer)) std::vector<std::string> playlists;
while (*bufferPtr)
{
// Concat the directory name with the filename
std::string ext = toLower(CFile::getExtension(wideToUtf8(bufferPtr)));
if (ext == "m3u" || ext == "m3u8")
{ {
// Backup the path and point to the next filename playlists.push_back (path + wideToUtf8(bufferPtr));
path = buffer;
path += "\\";
bufferPtr+=strlen(bufferPtr)+1;
} }
else
// Get selected files and playlists
std::vector<std::string> filenames;
std::vector<std::string> playlists;
while (*bufferPtr)
{ {
// Concat the directory name with the filename filenames.push_back (path + wideToUtf8(bufferPtr));
if (toLower(CFile::getExtension(bufferPtr)) == "m3u")
playlists.push_back (path+bufferPtr);
else
filenames.push_back (path+bufferPtr);
bufferPtr+=strlen(bufferPtr)+1;
} }
// Sort songs by filename bufferPtr += wcslen(bufferPtr) + 1;
sort (filenames.begin(), filenames.end()); }
// Sort songs by filename
sort (filenames.begin(), filenames.end());
static uint8 utf8Header[] = { 0xefu, 0xbbu, 0xbfu };
// Add playlist
uint i;
for (i=0; i<playlists.size(); i++)
{
// Get the path of the playlist
string basePlaylist = CFile::getPath (playlists[i]);
FILE *file = nlfopen (playlists[i], "r");
bool useUtf8 = CFile::getExtension(playlists[i]) == "m3u8";
// Add playlist if (file)
uint i;
for (i=0; i<playlists.size(); i++)
{ {
// Get the path of the playlist char line[512];
string basePlaylist = CFile::getPath (playlists[i]); while (fgets (line, 512, file))
FILE *file = fopen (playlists[i].c_str(), "r");
if (file)
{ {
char line[512]; // Not a comment line
while (fgets (line, 512, file)) string lineStr = trim(std::string(line));
{
// Not a comment line
string lineStr = trim (std::string(line));
if (lineStr[0] != '#')
filenames.push_back (basePlaylist+lineStr);
}
fclose (file);
}
}
// Build the songs array // id a UTF-8 BOM header is present, parse as UTF-8
std::vector<CMusicPlayer::CSongs> songs; if (!useUtf8 && lineStr.length() >= 3 && memcmp(line, utf8Header, 3) == 0)
for (i=0; i<filenames.size(); i++) useUtf8 = true;
{
CMusicPlayer::CSongs song; if (!useUtf8) lineStr = ucstring(line).toUtf8();
song.Filename = filenames[i];
SoundMngr->getMixer()->getSongTitle(filenames[i], song.Title); if (lineStr[0] != '#')
songs.push_back (song); filenames.push_back (CPath::makePathAbsolute(lineStr, basePlaylist));
}
fclose (file);
} }
}
MusicPlayer.playSongs(songs); // Build the songs array
std::vector<CMusicPlayer::CSongs> songs;
for (i=0; i<filenames.size(); i++)
{
CMusicPlayer::CSongs song;
song.Filename = filenames[i];
SoundMngr->getMixer()->getSongTitle(filenames[i], song.Title);
songs.push_back (song);
} }
if (Driver) MusicPlayer.playSongs(songs);
Driver->endDialogMode();
} }
if (Driver)
Driver->endDialogMode();
// Restore mouse // Restore mouse
InitMouseWithCursor (wasHardware); InitMouseWithCursor (wasHardware);
Driver->showCursor (wasHardware); Driver->showCursor (wasHardware);

Loading…
Cancel
Save