簡體   English   中英

在Windows Phone 8.1的MVVM上播放背景音頻

[英]Play Background Audio on MVVM on windows phone 8.1

我真的堅持這一點。 到目前為止,MSDN論壇上沒有人能夠幫助我。

我有一個MVVM應用程序,一個經過編輯的AppStudio應用程序。 我在應用程序上添加了播客功能。 由於播客音頻從未在Background上播放,因此該功能可以完美發揮作用。

我已經在MediaElement上聲明這是具有背景功能的媒體。 這是我認為的XAML

<DataTemplate x:Key="PodCast1DetailDetail">
    <ScrollViewer>
        <StackPanel>
            <TextBlock Margin="0,16" Text="{Binding Title, Converter={StaticResource TextPlainConverter}, ConverterParameter = 140}" Style="{StaticResource SubHeaderText}" />
            <MediaElement x:Name="PodCast" 
                Source="{Binding Enclosure}"
                PosterSource="{Binding PodcastImg}"
                CurrentStateChanged="Media_CurrentStateChanged"
                RateChanged="Media_RateChanged"
                AreTransportControlsEnabled ="True"
                AudioCategory="BackgroundCapableMedia"
                Stretch="UniformToFill"
                Width="340"
                Height="auto"
                MediaFailed="Media_MediaFailed"
                MediaOpened="Media_MediaOpened"
                MediaEnded="Media_MediaEnded"
                AutoPlay="False"
                HorizontalAlignment="Stretch"/>



                <!--<Image Source="{Binding ImageUrl, Converter={StaticResource ThumbnailConverter}, ConverterParameter=300}" Stretch="Uniform" />-->
            <TextBlock Margin="0,12" Style="{StaticResource ItemContentText}" Text="{Binding Content, Converter={StaticResource TextPlainConverter}}" />
            <!--<controls:WebControl Html="{Binding Content}" Width="auto" Height="1000" Foreground="{StaticResource AppForegroundColor}" />-->
        </StackPanel>
    </ScrollViewer>
</DataTemplate>

這是詳細信息頁面上的XAML:

<FlipView x:Name="FlipViewPodCast" Grid.Row="1" TabIndex="1"
        DataContext="{Binding NextCastModel}"
          ItemsSource="{Binding Items}"
          ItemTemplate="{StaticResource NextCast1DetailDetail}"
          SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
          ItemContainerStyle="{StaticResource FlipItemStyle}">
     </FlipView>

遵循詳細頁面后面的所有代碼:

    using System;
using System.Diagnostics;
using System.Threading;
using System.Net.NetworkInformation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel.DataTransfer;
using Windows.Media;
using Windows.Media.Playback;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using AppStudio.Services;
using AppStudio.ViewModels;


namespace AppStudio.Views
{
    public sealed partial class PodCastDetail : Page
    {
        private NavigationHelper _navigationHelper;

        private DataTransferManager _dataTransferManager;




        public PodCastDetail()
        {
            this.InitializeComponent();
            _navigationHelper = new NavigationHelper(this);


        }

        public PodCastViewModel PodCastModel { get; private set; }

        public NavigationHelper NavigationHelper
        {
            get { return _navigationHelper; }
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            _dataTransferManager = DataTransferManager.GetForCurrentView();
            _dataTransferManager.DataRequested += OnDataRequested;

            _navigationHelper.OnNavigatedTo(e);

            PodCastModel = NavigationServices.CurrentViewModel as PodCastViewModel;
            if (PodCastModel != null)
            {
                PodCastModel.ViewType = ViewTypes.Detail;
            }
            DataContext = this;


        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _navigationHelper.OnNavigatedFrom(e);
            _dataTransferManager.DataRequested -= OnDataRequested;

        }

        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (PodCastModel != null)
            {
                PodCastModel.GetShareContent(args.Request);
            }
        }




    }
}

有人可以幫幫我嗎? 我真的堅持這一點。 謝謝

我能說什么...首先,它是在應用程序中創建的MediaElement對象,與ListView或連接的觸發器相同。 當切換應用程序時,它們會發生什么? ListView將處於活動狀態? 本地文件在后台播放,因為默認播放器正在播放它們,所以請單擊媒體控件(UVC面板)的標題軌道。 我將打開默認播放器,而不是您的應用程序。

我認為這應該對您有幫助。

如何在后台播放音頻(XAML)

概述:背景音頻(Windows Phone Store應用)

后台任務樣本

好吧,至少我完成了自己的播放器,播放該播放器以及本地軌道和流軌道時沒有問題:3

請注意,當前Windows Phone和Windows Store應用程序對背景音頻的工作方式有所不同。

對於電話上的背景音頻,您不能使用XAML中聲明的MediaElement :相反,您需要使用背景音頻任務來完成音頻工作。

您需要遵循MSDN上Windows Phone的特定說明 這將為您提供一個非常有用的背景音頻代碼示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM