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

时间:2014-10-20 21:33:10

标签: c# xaml windows-phone-8 windows-8.1

我正在构建一些与播放传入的互联网广播流相关联的共享项目。 目前我遇到了在不同页面上播放此流的问题。 我做了一些后台任务并且工作正常我可以轻松地最小化我的应用程序但是当我移动到用户将读取RSS提要的不同页面时(Frame.Navigate包含RSS的一些DataContext),媒体元素停止播放。 / p>

我自己尝试找到一些解决方案,但我找不到任何最适合我的解决方案。

感谢您的帮助

 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>

1 个答案:

答案 0 :(得分:0)

似乎音乐停止的原因是因为您正在页面中创建流,然后离开页面,导致您刚分配的所有内容消失。

我建议您尝试在App.xaml.cs文件中创建/播放媒体播放器对象。 这应该适用于页面调用。