製作SCORM課件離線播放器(C#)

hunkcai發表於2010-02-09

概述

SCORM標準的課程是e-learning比較常用的標準,但課程要放到LMS瀏覽才看到課件資料的互動很不方便,下面介紹.net寫的SCORM課件播放器,方便離線瀏覽SCORM課程或用於SCORM的課件測試。

主要實現

建立winform專案,向窗體新增web browser控制元件,新增窗體的Load,FormClosing事件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace ScormPlayer

{

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            webBrowser1.ObjectForScripting = this; //讓html訪問窗體的成員

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            string startupPath = System.Configuration.ConfigurationManager.AppSettings["StartupPath"];

            //課程的入口檔案,寫在配置檔案裡,方便修改

            string url = Path.Combine(Application.StartupPath, startupPath);

            //SCORM外殼頁面,主要初始化SCORM API物件,把課程的地址當做引數傳入

            string preview = Path.Combine(Application.StartupPath, "Preview.html?url=" + url);

            webBrowser1.Navigate(preview);

        }

        //關閉控制

        private bool _CanClose = false;

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (!_CanClose)

            {

                //觸發頁面的onunload方法,讓課件提交資料

                webBrowser1.Document.InvokeScript("eval", new object[] { "window.top.close()" });

                _CanClose = true;

            }

            e.Cancel = !_CanClose;

        }

    }

}

向窗體新增SCORM標準的API實現

        //SCORM API 物件

        public object API

        {

            get

            {

                return this;

            }

        }

 

        public string LMSInitialize(string value)

        {

            return "true";//沒特殊處理返回"true"即可

        }

 

        public string LMSFinish(string value)

        {

            //課件頁面unload時關閉窗體

            _CanClose = true;

            Close();

 

            return "true";//沒特殊處理返回"true"即可

        }

 

        public string LMSGetValue(string model)

        {

            Util.Debug("LMSGetValue(" + model + ")");

            //TODO:讀資料,新增自定義處理,可讀寫資料庫或讀寫檔案

            // string value;

            // ...

            // return value

            

            return "0";//返回指定的資料,這裡hard code返回"0"。

        }

 

        public string LMSSetValue(string model, string value)

        {

            Util.Debug("LMSSetValue(" + model + ", " + value + ")");

            //TODO:寫資料,新增自定義處理,可讀寫資料庫或讀寫檔案

 

            return "true";//沒特殊處理返回"true"即可

        }

 

        public string LMSCommit(string value)

        {

            return "true";//沒特殊處理返回"true"即可

        }

 

        public string LMSGetErrorString(string value)

        {

            return string.Empty;//沒特殊處理返回""即可

        }

 

        public string LMSGetLastError()

        {

            return "0";//沒特殊處理返回"0"即可

        }

 

        public string LMSGetDiagnostic(string value)

        {

            return string.Empty;//沒特殊處理返回""即可

        }

新增SCORM外殼頁面preview.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>課程學習</title>

<script language="javascript" type="text/javascript">

window.API = window.external.API; //建立SCORM API物件,window.external.API是Form1.API屬性

//獲取地址引數

function getParam(paramName) {

    var url = unescape(window.location.href);

    var allargs = url.split("?")[1];

    var args = allargs.split("&");

    for (var i = 0; i < args.length; i++) {

        var arg = args[i].split("=");

        if (arg[0] == paramName) {

            return arg[1];

        }

    }

    return "";

}

function loadUrl() {

    var url = getParam("url");//課程的地址

    if (url != "") {

        document.getElementById("content").src = url;

    }

}

</script>

<style>

html{height: 100%;}

body{height: 100%;margin: 0px;overflow: hidden;}

</style>

</head>

<body onload="loadUrl()">

<iframe id="content" name="content" width="100%" height="100%" frameborder="0" scrolling="no" src="">iframe>

<noframes>

</noframes>

</body>

</html>

配置檔案配置課程的入口檔案

xml version="1.0" encoding="utf-8" ?>

<configuration>

    <appSettings>

        <add key="StartupPath" value="Course3/index.html"/>

    <!--<add key="StartupPath" value="Course1/Player.html"/>

         <add key="StartupPath" value="Course2/lo/template.html"/>

         <add key="StartupPath" value="Course3/index.html"/>

         -->

    </appSettings>

</configuration>

進階

上面基本實現了SCORM標準,進一步完善,可以進行課程資料的儲存,上載課程資料到平臺,新增課程管理等功能,才算真正實現企業級的SCORM離線播放器。

相關文章