C# 在不同页面上播放传入音频流

C# 在不同页面上播放传入音频流,c#,xaml,windows-phone-8,windows-8.1,C#,Xaml,Windows Phone 8,Windows 8.1,我正在建立一些共享项目,它与播放传入的互联网广播流相连接。 目前我在不同的页面上播放这个流时遇到了一个问题。 我做了一些后台任务,效果很好,我可以轻松地最小化我的应用程序,但当我移动到不同的页面,用户将阅读RSS提要(Frame.Navigate some DataContext,其中包含RSS)时,媒体元素停止播放 我试图自己找到一些解决办法,但我没有找到任何对我的生活最好的办法 谢谢你的帮助 public sealed partial class MainPage : Page {

我正在建立一些共享项目,它与播放传入的互联网广播流相连接。 目前我在不同的页面上播放这个流时遇到了一个问题。 我做了一些后台任务,效果很好,我可以轻松地最小化我的应用程序,但当我移动到不同的页面,用户将阅读RSS提要(Frame.Navigate some DataContext,其中包含RSS)时,媒体元素停止播放

我试图自己找到一些解决办法,但我没有找到任何对我的生活最好的办法

谢谢你的帮助

 public sealed partial class MainPage : Page
{
    SystemMediaTransportControls systemControls = SystemMediaTransportControls.GetForCurrentView();

    FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];
    FeedData nFeedData = new FeedData();

    public MainPage()
    {
        this.InitializeComponent();

        InitializeTransportControls();


    }

    private void Media_CurrentStateChanged(object sender, RoutedEventArgs e)
    {
        switch (Media.CurrentState)
        {
            case MediaElementState.Closed:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
                break;
            case MediaElementState.Paused:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
                break;
            case MediaElementState.Playing:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
                break;
            case MediaElementState.Stopped:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
                break;
            default:
                break;
        }
    } 


    private async void PlayButton_Click(object sender, RoutedEventArgs e)
    {

        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {


            Media.Play();

        });

    }



    private async void StopButton_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {


            Media.Stop();

        });
    }

    private async void PauseButton_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {


            Media.Pause();

        });
    }

    private void VolumeSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        Media.Volume = e.NewValue;

    }
    void InitializeTransportControls()
    {
        // Windows.Media 

        systemControls.ButtonPressed += systemControls_ButtonPressed;

        systemControls.IsPlayEnabled = true;
        systemControls.IsPauseEnabled = true;
    }
    // Handle ButtonPressed for systemControls 
    void systemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Pause:
                Media.Play();
                break;
            case SystemMediaTransportControlsButton.Play:
                Media.Pause();
                break;
            default:
                break;
        }
    }


    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {


        if (feedDataSource != null)
        {
            if (feedDataSource.Feeds.Count == 0)
            {
                await feedDataSource.GetFeedsAsync();
            }

            MenuSection.DataContext = feedDataSource.Feeds;

        }

    }
    private void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {



    }

    private void Contetnview_ItemClick(object sender, ItemClickEventArgs e)
    {

    }

    private void Contetnview_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {


       var x
            = ((GridView)sender).SelectedItem;

       var z = ((Grid)x).Tag.ToString();




        switch (z)
        {
            case "1":
                Frame.Navigate(typeof(ReadNewsPage), feedDataSource.Feeds[0]);

                break;
            case "2":
                Frame.Navigate(typeof(ReadNewsPage), feedDataSource.Feeds[3]);
                break;
            case "3":
                Frame.Navigate(typeof(ReadNewsPage), feedDataSource.Feeds[2]);
                break;
            case "4":
                Frame.Navigate(typeof(ReadNewsPage), feedDataSource.Feeds[1]);
                break;


        }
    }

}

 <Grid x:Name="ThisGris" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <MediaElement x:Name="Media" Source="http://5.201.13.191:80/live" HorizontalAlignment="Left" Height="206" Margin="107,372,0,0" VerticalAlignment="Top" Width="213" AudioCategory="BackgroundCapableMedia" AutoPlay="False" CurrentStateChanged="Media_CurrentStateChanged"/>
    <Hub x:Name="MainHubPage" Header="" Margin="0,3,0,-3" >

        <Hub.Background>
            <ImageBrush  />
        </Hub.Background>
        <HubSection Header="Radio" Width="402"  IsHeaderInteractive="True">

            <DataTemplate>
                <Grid Width="429" Height="565" d:IsLocked="True">
                    <Button x:Name="PlayButton" Content="Play" HorizontalAlignment="Left" Margin="118,352,0,0" VerticalAlignment="Top" Height="58" Width="83" Click="PlayButton_Click"  d:IsLocked="True"/>
                    <Button x:Name="StopButton" Content="Stop" HorizontalAlignment="Left" Margin="7,352,0,0" VerticalAlignment="Top" Height="58" Click="StopButton_Click" d:IsLocked="True"/>
                    <Button x:Name="PauseButton" Content="Pause" HorizontalAlignment="Left" Margin="236,352,0,0" VerticalAlignment="Top" Height="58" Click="PauseButton_Click" d:IsLocked="True"/>
                    <Slider x:Name="VolumeSlider" HorizontalAlignment="Left" Margin="53,295,0,0" VerticalAlignment="Top" Width="267"  Minimum="0" Value="50" Maximum="100" ValueChanged="VolumeSlider_ValueChanged" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Margin="10,129,0,0" TextWrapping="Wrap" FontSize="20" Text="Na antenie :" VerticalAlignment="Top" Height="40" Width="135" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Margin="10,174,0,0" TextWrapping="Wrap" FontSize="20" Text="Gramy :" VerticalAlignment="Top" Height="42" Width="135" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Margin="10,216,0,0" FontSize="20" TextWrapping="Wrap" Text="Wkrótce : " VerticalAlignment="Top" Height="42" Width="118" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Margin="150,216,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="42" Width="269" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Height="40" Margin="150,129,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="269" d:IsLocked="True"/>
                    <TextBlock HorizontalAlignment="Left" Margin="150,174,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="42" Width="293" d:IsLocked="True"/>
                </Grid>
            </DataTemplate>
        </HubSection>
        <HubSection x:Name="MenuSection" Width="606"  IsHeaderInteractive="True">
            <DataTemplate>
                <GridView x:Name="Contetnview"                             
                           DataContext="{Binding}"
                          Height="519" Width="606" SelectionChanged="Contetnview_SelectionChanged"  >
                    <Grid x:Name="Dowiadomosci" Height="135" Width="161" Tag="1" DataContext="{Binding }">
                        <TextBlock Text="Wiadomości"/>
                    </Grid>
                    <Grid x:Name="Dodupy" Height="135" Width="161" Tag="2" DataContext="{Binding Path=Feeds[0]}" >
                        <TextBlock Text="Muzyka"/>
                    </Grid>
                    <Grid Height="135" Width="161" Tag="3" DataContext="{Binding Path=Feeds[0]}">
                        <TextBlock Text="Kultura"/>
                    </Grid>
                    <Grid Height="135" Width="161"  Tag="4" DataContext="{Binding Source=feedDataSource, Path=Feeds[0]}">
                        <TextBlock Text="Sport"/>
                    </Grid>
                    <Grid Height="135" Width="161" Tag="5">
                        <TextBlock Text="O nas"/>
                    </Grid>
                    <Grid Height="135" Width="161" Tag="6">
                        <TextBlock Text="Audycja Autorskie"/>
                    </Grid>
                    <Grid Height="135" Width="161" Tag="7">
                        <TextBlock Text="Ekipa"/>
                    </Grid>
                    <Grid Height="135" Width="161" >
                        <TextBlock Text="Ramówka"/>
                    </Grid>
                </GridView>                                  
            </DataTemplate>
        </HubSection>
        <HubSection x:Name="Schedule" Width="606" Header="Ramówka"  IsHeaderInteractive="True" DataContext="{Binding Source=feedDataSource, Path=Feeds[0]}">
            <DataTemplate>
                <GridView x:Name="ScheduleContent"  Height="519" Width="606" ItemClick="Contetnview_ItemClick">

                    <Grid Height="135" Width="161" >
                        <TextBlock Text="Ramówka"></TextBlock>
                    </Grid>
                </GridView>
            </DataTemplate>
        </HubSection>
        <HubSection x:Name="Team" Width="606" Header="Ekipa"  IsHeaderInteractive="True" DataContext="{Binding Source=feedDataSource, Path=Feeds[0]}">
            <DataTemplate>
                <GridView x:Name="TeamContent"  Height="519" Width="606" ItemClick="Contetnview_ItemClick">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="135" Width="161" >
                                <Image>

                                </Image>
                            </Grid>

                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>

            </DataTemplate>
        </HubSection>
        <HubSection x:Name="AuthorShow" Width="606" Header="Audycje Autorskie"  IsHeaderInteractive="True" DataContext="{Binding Source=feedDataSource, Path=Feeds[0]}">
            <DataTemplate>
                <GridView x:Name="AuthorShowContent"  Height="519" Width="606" ItemClick="Contetnview_ItemClick">

                    <Grid Height="135" Width="161" >
                        <Image>

                        </Image>
                    </Grid>
                </GridView>
            </DataTemplate>
        </HubSection>          
        <HubSection x:Name="AboutUs" Width="606" Header="O nas"  IsHeaderInteractive="True" DataContext="{Binding Source=feedDataSource, Path=Feeds[0]}">
            <DataTemplate>
                <Grid x:Name="AboutUSContent"  Height="519" Width="606" >
                    <TextBlock Text="TutajONAS"></TextBlock>
                </Grid>
                </DataTemplate>
      </HubSection>
    </Hub>
</Grid>
公共密封部分类主页面:第页
{
SystemMediaTransportControls systemControls=SystemMediaTransportControls.GetForCurrentView();
FeedDataSource FeedDataSource=(FeedDataSource)App.Current.Resources[“FeedDataSource”];
FeedData nFeedData=新的FeedData();
公共主页()
{
this.InitializeComponent();
InitializeTransportControls();
}
私有无效媒体\u CurrentStateChanged(对象发送方,路由目标)
{
交换机(媒体当前状态)
{
案例MediaElementState。已关闭:
systemControls.PlaybackStatus=MediaPlaybackStatus.Closed;
打破
案例MediaElementState。已暂停:
systemControls.PlaybackStatus=MediaPlaybackStatus.暂停;
打破
case MediaElementState。正在播放:
systemControls.PlaybackStatus=MediaPlaybackStatus.Playing;
打破
案例MediaElementState。已停止:
systemControls.PlaybackStatus=MediaPlaybackStatus.Stopped;
打破
违约:
打破
}
} 
专用异步无效播放按钮\u单击(对象发送方,路由目标)
{
wait Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,()=>
{
媒体播放();
});
}
私有异步无效停止按钮\u单击(对象发送方,路由目标)
{
wait Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,()=>
{
Media.Stop();
});
}
专用异步无效暂停按钮单击(对象发送方,路由目标)
{
wait Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,()=>
{
媒体暂停();
});
}
私有void VolumeSlider_ValueChanged(对象发送方,RangeBaseValueChangedEventArgs e)
{
Media.Volume=e.NewValue;
}
void InitializeTransportControls()
{
//Windows.Media
systemControls.ButtonPressed+=systemControls\u ButtonPressed;
systemControls.IsPlayEnabled=true;
systemControls.IsPauseEnabled=true;
}
//按下系统控件的手柄按钮
按下作废系统控制按钮(SystemMediaTransportControls发送方,SystemMediaTransportControls按钮按下EventArgs)
{
开关(参数按钮)
{
案例系统MediaTransportControlsButton.暂停:
媒体播放();
打破
案例系统MediaTransportControlsButton.播放:
媒体暂停();
打破
违约:
打破
}
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
如果(feedDataSource!=null)
{
if(feedDataSource.Feeds.Count==0)
{
等待feedDataSource.GetFeedsAsync();
}
menusecoction.DataContext=feedDataSource.Feeds;
}
}
private void ItemListView_SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
}
私有void contentview\u ItemClick(对象发送者,ItemClickEventArgs e)
{
}
私有void contentview\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
变量x
=((GridView)发件人)。选择编辑项;
var z=((网格)x).Tag.ToString();
开关(z)
{
案例“1”:
Frame.Navigate(typeof(ReadNewsPage),feedDataSource.Feeds[0]);
打破
案例“2”:
导航(typeof(ReadNewsPage),feedDataSource.Feeds[3]);
打破
案例“3”:
导航(typeof(ReadNewsPage),feedDataSource.Feeds[2]);
打破
案例“4”:
Frame.Navigate(typeof(ReadNewsPage),feedDataSource.Feeds[1]);
打破
}
}
}