我有几页,其中一页是图书馆页面。在那个库页面上,我得到从我网站上的 php 文件反序列化的歌曲,并且该 php 文件从 mysql 数据库请求歌曲标题、艺术家姓名等。
当我尝试将歌曲添加到 itemGridView 时,一切正常。当我从 itemGridView(Windows phone 10 和 Windows 10 上的背景)中选择一首歌曲时,一切正常。
问题是,当我尝试从轨道切换时,传输控件卡住了,2 秒后它消失了。我做错什么了吗?我只是从背景样本中复制了所有内容。
这是我将歌曲添加到 gridView 时的代码:
private HttpClient httpClient;
public start()
{
this.InitializeComponent();
httpClient = new HttpClient();
// Limit the max buffer size for the response so we don't get overwhelmed
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
this.NavigationCacheMode = NavigationCacheMode.Required;
Instance = this;
Loaded += Start_Loaded;
initializeSongs();
backgroundAudioTaskStarted = new AutoResetEvent(false);
itemGridView.ItemsSource = Songs;
}
async void initializeSongs()
{
try
{
HttpResponseMessage response = await httpClient.GetAsync("http://www.myproductsofficial.eu/testt.php");
string array = await response.Content.ReadAsStringAsync();
string json = array;
MessageDialog mes = new MessageDialog(json);
await mes.ShowAsync();
var xdd = JsonConvert.DeserializeObject<List<RootObject>>(json);
foreach (var xd in xdd)
{
var song = new SongModel();
song.Title = xd.SongName;
song.Artist = xd.ArtistName;
song.Listens = xd.Listens;
song.AlbumArtUri = new Uri(xd.Thumbnail);
song.MediaUri = new Uri(xd.MediaLink);
Songs.Add(song);
}
foreach (var song in Songs)
{
var bitmap = new BitmapImage();
bitmap.UriSource = song.AlbumArtUri;
albumArtCache[song.AlbumArtUri.ToString()] = bitmap;
}
}
catch (Exception e1)
{
MessageDialog mes = new MessageDialog(e1.Message);
mes.ShowAsync();
}
}
private void itemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var song = itemGridView.SelectedItem as SongModel;
Debug.WriteLine("Clicked item from App: " + song.MediaUri.ToString());
// Start the background task if it wasn't running
if (!IsMyBackgroundTaskRunning || MediaPlayerState.Closed == BackgroundMediaPlayer.Current.CurrentState)
{
// First update the persisted start track
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.TrackId, song.MediaUri.ToString());
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.Position, new TimeSpan().ToString());
// Start task
StartBackgroundAudioTask();
}
else
{
// Switch to the selected track
MessageService.SendMessageToBackground(new TrackChangedMessage(song.MediaUri));
}
if (MediaPlayerState.Paused == BackgroundMediaPlayer.Current.CurrentState)
{
BackgroundMediaPlayer.Current.Play();
}
}
我检查了应用程序是否进入暂停模式和恢复模式。我还检查了背景音频的声明选项卡,入口点是:BackgroundAudioTask.MyBackgroundAudioTask
项目截图:http: //prntscr.com/81mrlq BackgroundAudioTask:Windows 运行时组件 BackgroundAudioShared:类库(DLL)