如何在WP7中播放嵌入式视频 - Phonegap?

时间:2012-02-17 09:47:14

标签: html5 windows-phone-7 cordova html5-video

我需要在WP7 phonegap应用程序中播放嵌入式视频文件。文件(dizzy.mp4)与以下布局一起位于www文件夹中

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>PhoneGap WP7</title>
    <link rel="stylesheet" href="master.css" type="text/css" />
    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
    <script type="text/javascript" charset="utf-8" src="jquery-1.6.4.min.js"></script>
</head>
<body>
    <video onclick="play()">
        <source src="http://html5demos.com/assets/dizzy.mp4" type="video/mp4" />
    </video>
    <video onclick="play()">
        <source src="./dizzy.mp4" type="video/mp4" />
    </video>
</body>
</html>

如果单击第一个视频元素,则从Internet下载视频文件,一切正常。但是点击第二个(本地视频)后,只会出现一个带有“打开...”标签的视频播放器屏幕。两个视频都是相同的视频文件。

该应用程序在模拟器和真实设备(带有WF7.5 Mango的Nokia Lumnia 710)上运行,结果是相同的。

我尝试为视频文件设置不同的构建操作:内容,资源,嵌入式资源。它没有帮助。

如何让它发挥作用?

更新here描述了类似的问题。它是WP7的错误吗?

2 个答案:

答案 0 :(得分:3)

这是一种解决方法。以下代码是Phonegap命令,用于实现视频回放功能。

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using WP7GapClassLib.PhoneGap;
using WP7GapClassLib.PhoneGap.Commands;
using WP7GapClassLib.PhoneGap.JSON;

namespace PhoneGap.Extension.Commands
{

    /// <summary>
    /// Implements video play back functionality.
    /// </summary>
    public class Video : BaseCommand
    {

        /// <summary>
        /// Video player object
        /// </summary>
        private MediaElement _player;

        [DataContract]
        public class VideoOptions
        {
            /// <summary>
            /// Path to video file
            /// </summary>
            [DataMember(Name = "src")]
            public string Src { get; set; }
        }

        public void Play(string args)
        {
            VideoOptions options = JsonHelper.Deserialize<VideoOptions>(args);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    _Play(options.Src);

                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
                }
                catch (Exception e)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
                }
            }); 
        }

        private void _Play(string filePath)
        {
            // this.player is a MediaElement, it must be added to the visual tree in order to play
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (frame != null)
            {
                PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
                if (page != null)
                {
                    Grid grid = page.FindName("LayoutRoot") as Grid;
                    if (grid != null && _player == null)
                    {
                        _player = new MediaElement();
                        grid.Children.Add(this._player);
                        _player.Visibility = Visibility.Visible;
                    }
                }
            }

            Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
            if (uri.IsAbsoluteUri)
            {
                _player.Source = uri;
            }
            else
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoFile.FileExists(filePath))
                    {
                        using (
                            IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open,
                                                                                             isoFile))
                        {
                            _player.SetSource(stream);
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Source doesn't exist");
                    }
                }
            }

            _player.Play();
        }
    }

}

此处只有Play功能,但可以扩展为支持停止/暂停/关闭等。

在客户端注册此命令:

    <script type="text/javascript">

    function playVideo(src) {

        PhoneGap.exec(         //PhoneGap.exec = function(success, fail, service, action, args)
            null, //success
            null, //fail
            "Video", //service
            "Play", //action
            {src: src} //args
           ); 
    };
   </script>

播放文件:

<a href="#" class="btn" onClick="playVideo('/app/www/dizzy.mp4');">Play</a>  

注意路径'/app/www/dizzy.mp4'。

答案 1 :(得分:2)

我使用PhoneGap在Android平台上实现了播放音乐的功能,我的代码快照如下: HTML代码:

<a href="#" class="btn large" onclick="playAudio('/android_asset/www/music/noya_wyt.mp3');">Play Audio</a>

JavaScript代码:

 function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);

        // Play audio
        my_media.play();
 }

我认为您可以更改视频“src”路径,然后重试。如果该应用仍然不起作用,您可以尝试调用phonegap媒体API进行实施。