2016-06-06 11 views
0

나는 이와 같이 BackgroundMediaPlayer에 재생 목록을 추가하려고합니다. 그래서 다음과 같습니다Mediaplayer 형식의 개체를 IMediaPlayerSource2 형식으로 캐스팅 할 수 없습니다.

try 
{ 
    Progr.IsActive = true; 
    ErrorMessage.Visibility = Visibility.Collapsed; 

    //This fills the ObservableCollection timeLine with Tracks 
    await UserAuthed.PopulateTimelineTracksAsync(timeLine); 

    //This should create the Playlist 
    CreatePlaylist(timeLine); 

    Progr.IsActive = false; 
} 

catch (Exception ex) 
{ 
    Progr.IsActive = false; 
    ErrorMessage.Visibility = Visibility.Visible; 
} 


private void CreatePlaylist(ObservableCollection<Collection> TrackCollection) 
{ 
    foreach (var item in TrackCollection) 
    { 
     MediaSource ms = MediaSource.CreateFromUri(new Uri(item.origin.uri)); 
     ms.CustomProperties.Add("Title", item.origin.title); 
     ms.CustomProperties.Add("Artist", item.origin.user.username); 
     MPL.Items.Add(new MediaPlaybackItem(ms)); 
    } 

    BackgroundMediaPlayer.Current.Source = MPL; 
} 

그러나 BackgroundMediaPlayer.Current.Source = MPL;에서 그는 {"Unable to cast object of type 'Windows.Media.Playback.MediaPlayer' to type 'Windows.Media.Playback.IMediaPlayerSource2'."}

이 예외의 의미는 무엇입니까 실패? 어떻게 해결할 수 있습니까?

편집 : MPL은 MediaPlaybackList입니다.

+1

컴파일 타임 또는 실행 시간에 실패합니까? 'MPL'의 유형은 무엇입니까? –

+0

'MPL'은'MediaPlaybackList' 유형일 수 있습니다. 그렇지? – Saadi

+0

예, 죄송합니다, MPL은 MediaPlaybackList입니다. – Flauschcoder

답변

0

이와 함께, 코드를 업데이트하십시오 :

여기
private void CreatePlaylist(ObservableCollection<Collection> TrackCollection) 
{ 
    // Make a new list 
    MPL = new MediaPlaybackList(); 
    foreach (var item in TrackCollection) 
    { 
     MediaSource ms = MediaSource.CreateFromUri(new Uri(item.origin.uri)); 
     ms.CustomProperties.Add("Title", item.origin.title); 
     ms.CustomProperties.Add("Artist", item.origin.user.username); 
     MPL.Items.Add(new MediaPlaybackItem(ms)); 
    } 

    BackgroundMediaPlayer.Current.Source = MPL; 
} 

는 참조 용 GitHub의 link 완료됩니다. 마지막에 CreatePlaybackList 함수를 찾을 수 있습니다.

감사합니다!

+0

시도했지만 여전히 작동하지 않습니다 :/ – Flauschcoder

+0

'item.origin.uri'에서 문제가있을 수 있습니다. 연결 상태가 좋습니까? uri에 액세스 할 수 있습니까? 권한을 추가 하시겠습니까? – Saadi

+0

당신이 말한대로, 이것이 문제가 될 수 있습니다. Soundcloud-API를 사용하고 있으며 API가 스트림 링크로 http : //xxxxxxxxx.mp3 링크를 제공하지 않습니다. 어쩌면 이것이 문제 일 수 있습니다. – Flauschcoder