Windows phone 8 如果用户导航到其他应用程序或主屏幕,请暂停Windowos phone 8中的Mediaelement

Windows phone 8 如果用户导航到其他应用程序或主屏幕,请暂停Windowos phone 8中的Mediaelement,windows-phone-8,windows-phone,Windows Phone 8,Windows Phone,我开发了一个应用程序,它使用MediaElement和远程url播放视频。一切正常,视频播放也很好 但我面临的问题是,如果用户正在播放视频,并且用户触摸了手机上的windows按钮。然后我的应用程序转到后台,并显示主屏幕。现在,在主屏幕上,用户触摸后退按钮。我的应用程序被带到前台,视频从一开始就开始加载。我是否可以暂停mediaelement,这样当用户返回我的应用程序时,视频就会恢复。 还有一件事是我不能使用MediaLauncher,因为我想在用户与MediaControl(如播放/暂停)交

我开发了一个应用程序,它使用MediaElement和远程url播放视频。一切正常,视频播放也很好

但我面临的问题是,如果用户正在播放视频,并且用户触摸了手机上的windows按钮。然后我的应用程序转到后台,并显示主屏幕。现在,在主屏幕上,用户触摸后退按钮。我的应用程序被带到前台,视频从一开始就开始加载。我是否可以暂停mediaelement,这样当用户返回我的应用程序时,视频就会恢复。

还有一件事是我不能使用MediaLauncher,因为我想在用户与MediaControl(如播放/暂停)交互时记录一些事件

请大家在这个场景中指导我


谢谢。

您可以通过WMAppManifest.xml中DefaultTask元素的DefaultTask元素失活策略属性的ActivationPolicy属性恢复应用程序,并将值设置为“resume”。对于此任务,您需要直接编辑应用程序清单,而不是使用清单编辑器。为此,右键单击WMAppManifest.xml,单击打开方式,然后选择xml(文本)编辑器

可以为XAML应用程序、Direct3D应用程序和Direct3D with XAML应用程序启用For Resume。以下示例显示DefaultTask元素如何查找XAML应用程序和Direct3D应用程序

<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/>

<DefaultTask Name="_default" ImagePath="PhoneDirect3DApp1.exe" ImageParams=""  ActivationPolicy="Resume"/>

如果这对你没有帮助,你可以手动播放并停止你的视频文件,就像下面的代码一样

XAML

<Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.90*"/>
            <RowDefinition Height="0.10*"/>
        </Grid.RowDefinitions>
        <SSME:SmoothStreamingMediaElement x:Name="video" Grid.Row="0" />
        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel Orientation="Horizontal" Grid.Row="1">

            <Button   x:Name="PlayButton" Width="150" Click="PlayButton_Click"  Loaded="PlayButton_Loaded"/>
            <Button   x:Name="StopButton" Content="Stop" Width="100" Click="StopButton_Click" />

            <TextBlock x:Name="status"/>
            <TextBlock x:Name="currentBitrate"/>

        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
    </Grid>

C#代码:

 public partial class VIdeoStraming : PhoneApplicationPage
    {
        string VideoUrl,StreamingUrl;
        public VIdeoStraming()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            VideoUrl = this.NavigationContext.QueryString["parameter0"];
            string Manifest="/Manifest";
            StreamingUrl = VideoUrl + Manifest;                     
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            //Monitor the state of the content to determine the right action to take on this button being clicked
            //and then change the text to reflect the next action
            switch (video.CurrentState)
            {
                case SmoothStreamingMediaElementState.Playing:
                    video.Pause();
                    PlayButton.Content = "Play";
                    break;
                case SmoothStreamingMediaElementState.Stopped:
                case SmoothStreamingMediaElementState.Paused:
                    video.Play();
                    PlayButton.Content = "Pause";
                    break;
            }
        }

        private void PlayButton_Loaded(object sender, RoutedEventArgs e)
        {
            switch (video.AutoPlay)
            {
                case false:
                    PlayButton.Content = "Play";
                    break;
                case true:
                    PlayButton.Content = "Pause";
                    break;
            }
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {

            //This should simply stop the playback

                video.Stop();
                //We should also reflect the chang on the play button
                PlayButton.Content = "Play";

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            video.CurrentStateChanged += new RoutedEventHandler(video_CurrentStateChanged);
            video.PlaybackTrackChanged += new EventHandler<Microsoft.Web.Media.SmoothStreaming.TrackChangedEventArgs>(video_PlaybackTrackChanged);
            //video.SmoothStreamingSource = new Uri("http://64.120.251.114:1945/live/sharedobjects/layoutvideo/mp4:1311370468970.MP4/Manifest");
            video.SmoothStreamingSource = new Uri(StreamingUrl);
            video.ManifestReady += new EventHandler<EventArgs>(video_ManifestReady);
        }

        //when use in mobile device
        void video_ManifestReady(object sender, EventArgs e)
        {
            SmoothStreamingMediaElement ssme = sender as SmoothStreamingMediaElement;

            if (ssme == null)
            {
                return;
            }

            // Select the highest band of tracks which all have the same resolution.
            // maxMobileBitrate depends on the encoding settings
            const ulong maxMobileBitrate = 1000000;
            foreach (SegmentInfo segment in ssme.ManifestInfo.Segments)
            {
                foreach (StreamInfo streamInfo in segment.AvailableStreams)
                {
                    if (MediaStreamType.Video == streamInfo.Type)
                    {
                        List<TrackInfo> widestBand = new List<TrackInfo>();
                        List<TrackInfo> currentBand = new List<TrackInfo>();
                        ulong lastHeight = 0;
                        ulong lastWidth = 0;
                        ulong index = 0;

                        foreach (TrackInfo track in streamInfo.AvailableTracks)
                        {
                            index += 1;

                            string strMaxWidth;
                            string strMaxHeight;
                            // If can't find width/height, choose only the top bitrate.
                            ulong ulMaxWidth = index;
                            // If can't find width/height, choose only the top bitrate.
                            ulong ulMaxHeight = index;
                            // V2 manifests require "MaxWidth", while v1 manifests used "Width".
                            if (track.Attributes.TryGetValue("MaxWidth", out strMaxWidth) ||
                                track.Attributes.TryGetValue("Width", out strMaxWidth))
                            {
                                ulong.TryParse(strMaxWidth, out ulMaxWidth);
                            }

                            if (track.Attributes.TryGetValue("MaxHeight", out strMaxHeight) ||
                                track.Attributes.TryGetValue("Height", out strMaxHeight))
                            {
                                ulong.TryParse(strMaxHeight, out ulMaxHeight);
                            }

                            if (ulMaxWidth != lastWidth ||
                                ulMaxHeight != lastHeight)
                            {
                                // Current band is now finished, check if it is the widest.
                                // If same size, current band preferred over previous
                                // widest, because it will be of higher bitrate.
                                if (currentBand.Count >= widestBand.Count)
                                {
                                    //  A new widest band:
                                    widestBand = currentBand;
                                    currentBand = new List<TrackInfo>();
                                }
                            }

                            if (track.Bitrate > maxMobileBitrate)
                            {
                                break;
                            }

                            // Current track always gets added to current band.
                            currentBand.Add(track);
                            lastWidth = ulMaxWidth;
                            lastHeight = ulMaxHeight;
                        }

                        if (0 == widestBand.Count &&
                            0 == currentBand.Count)
                        {
                            // Lowest bitrate band is > maxMobileBitrate.
                            widestBand.Add(streamInfo.AvailableTracks[0]);
                        }
                        else if (currentBand.Count >= widestBand.Count)
                        {
                            // Need to check the last band which was constructed.
                            Debug.Assert(currentBand.Count > 0);
                            widestBand = currentBand; // Winner by default.
                        }

                        Debug.Assert(widestBand.Count >= 1);
                        streamInfo.RestrictTracks(widestBand);
                    }
                }
            }

        }
        void video_PlaybackTrackChanged(object sender, Microsoft.Web.Media.SmoothStreaming.TrackChangedEventArgs e)
        {
            currentBitrate.Text = e.NewTrack.Bitrate.ToString();
        }

        void video_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            status.Text = video.CurrentState.ToString();
        }

        private void imghdrleft_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void imghdrright_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Planet41VIew/Settings.xaml", UriKind.RelativeOrAbsolute));
        }
    }
公共部分类录像:PhoneApplicationPage
{
字符串VideoUrl,StreamingUrl;
公共录像带
{
初始化组件();
}
受保护的覆盖无效OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
基地。导航到(e);
VideoUrl=this.NavigationContext.QueryString[“parameter0”];
字符串Manifest=“/Manifest”;
StreamingUrl=视频URL+清单;
}
私有void播放按钮(对象发送者,路由目标e)
{
//监视内容的状态,以确定单击此按钮时要采取的正确操作
//然后更改文本以反映下一个操作
开关(视频.CurrentState)
{
case SmoothStreamingMediaElementState.播放:
video.Pause();
PlayButton.Content=“Play”;
打破
案例SmoothStreamingMediaElementState。已停止:
案例SmoothStreamingMediaElementState。已暂停:
video.Play();
PlayButton.Content=“暂停”;
打破
}
}
已加载专用void播放按钮(对象发送器,路由目标e)
{
开关(视频自动播放)
{
案例错误:
PlayButton.Content=“Play”;
打破
大小写正确:
PlayButton.Content=“暂停”;
打破
}
}
私有无效停止按钮\单击(对象发送者,路由目标)
{
//这应该只是停止播放
video.Stop();
//我们还应该在播放按钮上反映变化
PlayButton.Content=“Play”;
}
已加载专用void PhoneApplicationPage_(对象发送方,路由目标)
{
video.CurrentStateChanged+=新路由EventHandler(video\u CurrentStateChanged);
video.PlaybackTrackChanged+=新事件处理程序(video\u PlaybackTrackChanged);
//video.SmoothStreamingSource=新Uri(“http://64.120.251.114:1945/live/sharedobjects/layoutvideo/mp4:1311370468970.MP4/Manifest");
video.SmoothStreamingSource=新Uri(StreamingUrl);
video.ManifestReady+=新事件处理程序(video_ManifestReady);
}
//在移动设备中使用时
void video_ManifestReady(对象发送方,事件参数e)
{
SmoothStreamingMediaElement ssme=发送方作为SmoothStreamingMediaElement;
if(ssme==null)
{
返回;
}
//选择所有具有相同分辨率的轨迹的最高波段。
//MaxMobileBrate取决于编码设置
const ulong maxMobileBitrate=1000000;
foreach(ssme.ManifestInfo.Segments中的SegmentInfo段)
{
foreach(段中的StreamInfo StreamInfo.AvailableStreams)
{
如果(MediaStreamType.Video==streamInfo.Type)
{
列表宽度带=新列表();
List currentBand=新列表();
ulong lastHeight=0;
ulong lastWidth=0;
乌龙指数=0;
foreach(streamInfo.AvailableTracks中的TrackInfo轨迹)
{
指数+=1;
字符串strMaxWidth;
字符串strMaxHeight;
//如果找不到宽度/高度,请仅选择最高比特率。
ulong ulMaxWidth=索引;
//如果找不到宽度/高度,请仅选择最高比特率。
ulong ulMaxHeight=指数;
//V2清单需要“MaxWidth”,而v1清单使用“Width”。