【问题标题】:Move to next track with background audio使用背景音频移至下一曲目
【发布时间】:2012-11-29 14:15:55
【问题描述】:

我正在使用 C#/XAML 实现背景音频 Windows 8 Store App,并且我设法在后台运行音频,使用带有 BackgroundCapableMedia 的 MediaElement。根据那个blog post,我应该可以播放播放列表了。

但是当曲目完成时,我找不到如何移动到下一个曲目。如果我使用mediaElement.MediaEnded,当应用程序在后台时不会调用事件处理程序。

【问题讨论】:

    标签: windows-8 windows-runtime windows-store-apps background-audio


    【解决方案1】:

    基本上,您必须制作自己的播放列表并自己实现逻辑。您的播放列表可以只是一个集合,您可以获取集合中的下一首曲目并播放它。我的代码中的一个示例:

    internal ObservableCollection<StoryViewModel> Playlist { get; set; }
    
    void me_MediaEnded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        Debug.WriteLine("me_MediaEnded");
    
        // Zero or one tracks only, so just end...
        if (Playlist.Count <= 1)
        {
            return;
        }
        else
        {
            // We have more tracks we need to play
            Playlist.RemoveAt(0);
            PlayPlaylist();
        }
    }
    
    internal void PlayPlaylist()
    {
        if (Playlist.Count == 0)
            return;
    
        // Set the MediaControls metadata
        MediaControl.ArtistName = _svm.ProgramTitle ?? "NPR";
        MediaControl.TrackName = _svm.Title;
    
        // This centralized dispatcher object is updated by each page to ensure it is current
        _dispatcher.RunAsync(
            CoreDispatcherPriority.Normal, () =>
            {
                // Set the MediaElement to the audio and play
                Me.Source = _svm.Mp3Uri;
                Me.Play();
            });
    }
    

    【讨论】:

    • 也许我做错了什么,但就像我提到的那样,这对我不起作用,因为除非应用程序在前台,否则不会调用 MediaEnded 函数。
    猜你喜欢
    • 1970-01-01
    • 2019-03-26
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多