0

Im Realy stuck at this. No one on MSDN foruns was able to help me so far.

I have a MVVM app, an edited AppStudio App. Im adding a podcast feature on the app. The feature works perfectly exept for the fact that the podcast audio never plays on Background.

I have declared on the MediaElement that is a background capable media. Here is the XAML of my view

<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:

<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

user3720810
  • 3
  • 1
  • 2

2 Answers2

0

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. What happens to them when the switch application? ListView will be active? Local files are played in the backgrounds, because the default player playing their, click on the title track of media control(UVC panel). I will open default player, not your application.

I think this should help you.

How to play audio in the background (XAML)

Overview: Background audio (Windows Phone Store apps)

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

IceFog
  • 310
  • 2
  • 4
  • well, how can i know if the list view is active? My audio is an MP3 File on a remote server (this case on SoundCloud). when i start to play the audio the UVC is empty (dont know why). In list view, if i go back to home the audio stops but the time keeps running, when im back on the app the audio its still running, but when i press back on the list view and go back to main page the audio stops completely. About the samples i have read them but i dont know how to call Play(), Pause() or Stop() methods on a MVVM app. – user3720810 Jun 09 '14 at 11:24
  • Download this example, and based on this example rewrite your application. This shall be your fastest way [link](http://code.msdn.microsoft.com/wpapps/Background-Task-Sample-9209ade9) – IceFog Jun 09 '14 at 18:03
0

Beware that currently Windows Phone and Windows Store apps work differently for background audio.

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.

You need to follow the Windows Phone specific instructions on MSDN. This will point you a background audio code sample that is very useful to understand.

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43