简体   繁体   English

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

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

Im Realy stuck at this. 我真的坚持这一点。 No one on MSDN foruns was able to help me so far. 到目前为止,MSDN论坛上没有人能够帮助我。

I have a MVVM app, an edited AppStudio App. 我有一个MVVM应用程序,一个经过编辑的AppStudio应用程序。 Im adding a podcast feature on the app. 我在应用程序上添加了播客功能。 The feature works perfectly exept for the fact that the podcast audio never plays on Background. 由于播客音频从未在Background上播放,因此该功能可以完美发挥作用。

I have declared on the MediaElement that is a background capable media. 我已经在MediaElement上声明这是具有背景功能的媒体。 Here is the XAML of my view 这是我认为的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>

Here is the XAML on the detail page: 这是详细信息页面上的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>

Following its all the code behind of the detail page: 遵循详细页面后面的所有代码:

    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);
            }
        }




    }
}

Can some one please help me? 有人可以帮帮我吗? Im realy stuck at this. 我真的坚持这一点。 Thanks 谢谢

What can I say ... First of all it's a MediaElement object that is created in the application, is the same as ListView or triggers that are connected. 我能说什么...首先,它是在应用程序中创建的MediaElement对象,与ListView或连接的触发器相同。 What happens to them when the switch application? 当切换应用程序时,它们会发生什么? ListView will be active? ListView将处于活动状态? Local files are played in the backgrounds, because the default player playing their, click on the title track of media control(UVC panel). 本地文件在后台播放,因为默认播放器正在播放它们,所以请单击媒体控件(UVC面板)的标题轨道。 I will open default player, not your application. 我将打开默认播放器,而不是您的应用程序。

I think this should help you. 我认为这应该对您有帮助。

How to play audio in the background (XAML) 如何在后台播放音频(XAML)

Overview: Background audio (Windows Phone Store apps) 概述:背景音频(Windows Phone Store应用)

Background task sample 后台任务样本

Well, at least as a result, I finishing my own player, which without a problem playing as well as local and stream tracks :3 好吧,至少我完成了自己的播放器,播放该播放器以及本地轨道和流轨道时没有问题:3

Beware that currently Windows Phone and Windows Store apps work differently for background audio. 请注意,当前Windows Phone和Windows Store应用程序对背景音频的工作方式有所不同。

For background audio on the phone you mustn't use MediaElement declared in XAML: instead you need to use a background audio task to do the audio work. 对于电话上的背景音频,您不能使用XAML中声明的MediaElement :相反,您需要使用背景音频任务来完成音频工作。

You need to follow the Windows Phone specific instructions on MSDN . 您需要遵循MSDN上Windows Phone的特定说明 This will point you a background audio code sample that is very useful to understand. 这将为您提供一个非常有用的背景音频代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM