背景音频不工作

时间:2015-08-05 19:43:47

标签: c# uwp

我有几页,其中一个页面是图书馆页面。在该库页面上,我从我的网站上的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)

1 个答案:

答案 0 :(得分:0)

在这里回答一个老问题。通过Win10周年更新(版本14393),我们大大简化了背景媒体播放的模型。

MSDN doc: https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/background-audio

示例应用: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundMediaPlayback

谢谢, Stefan Wick - Windows开发人员平台