示例#1
0
/*
** Called by iTunes event handler on track change.
**
*/
void PlayerITunes::OnTrackChange()
{
	IITTrack* track;
	HRESULT hr = m_iTunes->get_CurrentTrack(&track);
	if (SUCCEEDED(hr) && track)
	{
		BSTR tmpStr;
		long tmpVal;

		// Get metadata
		track->get_Artist(&tmpStr);
		tmpStr ? (m_Artist = tmpStr) : m_Artist.clear();

		track->get_Name(&tmpStr);
		tmpStr ? (m_Title = tmpStr) : m_Title.clear();

		track->get_Album(&tmpStr);
		tmpStr ? (m_Album = tmpStr) : m_Album.clear();

		track->get_Genre(&tmpStr);;
		tmpStr ? (m_Genre = tmpStr) : m_Genre.clear();

		track->get_Duration(&tmpVal);
		m_Duration = (UINT)tmpVal;

		// Rating is 0 - 100, divide to 0 - 5
		track->get_Rating(&tmpVal);
		tmpVal /= 20L;
		m_Rating = (UINT)tmpVal;

		track->get_TrackNumber(&tmpVal);
		m_Number = (UINT)tmpVal;

		track->get_Year(&tmpVal);
		m_Year = (UINT)tmpVal;

		IITPlaylist* playlist;
		hr = track->get_Playlist(&playlist);
		if (SUCCEEDED(hr))
		{
			ITPlaylistRepeatMode repeat;
			hr = playlist->get_SongRepeat(&repeat);
			if (SUCCEEDED(hr))
			{
				m_Repeat = repeat != ITPlaylistRepeatModeOff;
			}

			playlist->Release();
		}

		IITFileOrCDTrack* file;
		hr = track->QueryInterface(&file);
		if (SUCCEEDED(hr))
		{
			file->get_Location(&tmpStr);
			file->Release();
			if (tmpStr && wcscmp(tmpStr, m_FilePath.c_str()) != 0)
			{
				++m_TrackCount;
				m_FilePath = tmpStr;

				if (m_Measures & MEASURE_COVER)
				{
					m_CoverPath.clear();

					// Check for embedded art through iTunes interface
					IITArtworkCollection* artworkCollection;
					hr = track->get_Artwork(&artworkCollection);

					if (SUCCEEDED(hr))
					{
						long count;
						artworkCollection->get_Count(&count);

						if (count > 0)
						{
							IITArtwork* artwork;
							hr = artworkCollection->get_Item(1, &artwork);

							if (SUCCEEDED(hr))
							{
								_bstr_t coverPath = m_TempCoverPath.c_str();
								hr = artwork->SaveArtworkToFile(coverPath);
								if (SUCCEEDED(hr))
								{
									m_CoverPath = m_TempCoverPath;
								}

								artwork->Release();
							}
						}

						artworkCollection->Release();
					}
				}

				if (m_Measures & MEASURE_LYRICS)
				{
					FindLyrics();
				}
			}
		}

		track->Release();
	}
	else
	{
		ClearData(false);
	}
}
std::string CiTunes::GetCurrentSongTitle_iTunes()
{
	Init();

	using std::string;
	using std::wstring;
 
	IITTrack *iITrack = 0;

	// String operations done in a wstring, then converted for return
	wstring wstrRet;
	string strRet;
 
	try
	{
 		BSTR bstrURL = 0;
 
		if(m_iITunes)
		{
			ITPlayerState iIPlayerState;

			m_iITunes->get_CurrentTrack(&iITrack);
			m_iITunes->get_CurrentStreamURL((BSTR *)&bstrURL);	

			if(iITrack)
			{
				BSTR bstrTrack = 0;

				iITrack->get_Name((BSTR *)&bstrTrack);
				
				// Add song title
				if(bstrTrack)
					wstrRet += bstrTrack;

				iITrack->Release();
			}
			else
			{
				// Couldn't get track name
			}

			// Add url, if present
			if(bstrURL)
			{
				wstrRet += L" ";
				wstrRet += bstrURL;
			}

			m_iITunes->get_PlayerState(&iIPlayerState);

			// Add player state, if special
			switch(iIPlayerState)
			{
			case ITPlayerStatePlaying:
			default:
				break;
			case ITPlayerStateStopped:
				wstrRet += L" [stopped]";
				break;
			case ITPlayerStateFastForward:
				wstrRet += L" [fast]";
				break;
			case ITPlayerStateRewind:
				wstrRet += L" [rewind]";
				break;
			}

			/*m_iITunes->Release();*/			
		}
		else
		{
			// iTunes interface not found/failed
			wstrRet = L"";
		}

	}
	catch(...)
	{
 
			SAFE_RELEASE(iITrack);
			ReleaseiTunes();
	}
 

	// Convert the result from wstring to string
	size_t len = wstrRet.size();
	strRet.resize(len);
	for(size_t i = 0; i < len; i++)
		strRet[i] = static_cast<char>(wstrRet[i]);

	return strRet;
}