Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone Media Player (MediaElement) and DownloadProgress (URL,Website)



Clound SSD Virtual Server

Windows Phone Media Player (MediaElement) and DownloadProgress (URL,Website)

Windows Phone Media Player (MediaElement) and DownloadProgress (URL,Website) การเขียนโปรแกรม App บน Windows Phone ได้ถูกออกแบบมาให้ง่ายต่อการพัฒนาโปรแกรม เช่นการเล่นไฟล์ Media Player ซึ่งในบทความก่อนหน้านี้ได้ Review การใช้งาน Control ที่ชื่อว่า MediaElement ไปบ้างแล้ว และในตัวอย่างนี้เราจะมาใช้ความสามารถของ MediaElement ที่จะเล่นไฟล์ Media ที่อยู่บน URL(Website) และการใช้งาน Event ที่มีชื่อว่า DownloadProgressChanged ที่จะควบคุมและแสดงสถานะในการดาวน์โหลด Media Source ที่อยู่บนเว็บไซต์

Windows Phone MediaElement DownloadProgress

Windows Phone and MediaElement DownloadProgress


            <MediaElement Name="MediaElement1" 
                          Source="https://www.thaicreate.com/Media/movie.wmv" />

ปกติแล้ว Controls MediaElement สามารถที่จะเรียก Media Source ที่อยู่ใน URL ได้ในทันที แต่ถ้าเราต้องการแสดงสถานะการ Download ข้อมูลว่าในขณะที่เรากำลังเล่นไฟล์อยู่นั้น มีการดาวน์โหลดข้อมูลเป็นอย่างไร ซึ่งวิธีการง่าย ๆ เพียงใช้ Event ที่มีชื่อว่า DownloadProgressChanged="eventhandler" ก็จะสามารถแสดงตัวเลขซึ่งจะเป็นสถานะการ Download ได้ทันที โดยสามารถนำค่านี้แสดงผลบน ProgressBar ได้

            <MediaElement Name="MediaElement1" 
                          Source="https://www.thaicreate.com/Media/movie.wmv"
			  DownloadProgressChanged="MediaElement1_DownloadProgressChanged"
			  />


		    Private Sub MediaElement1_DownloadProgressChanged(sender As Object, e As System.Windows.RoutedEventArgs)
			ProgressBar1.Value = MediaElement1.DownloadProgress * 100
		    End Sub

หรือจะเขียนเป็น Event Handler บน Code Behind ก้ได้เช่นเดียวกัน

    Public Sub New()
        InitializeComponent()

        Dim url As String = "http://wwww.thaicreate.com/wp/vdo.mp4"
        AddHandler MediaElement1.DownloadProgressChanged, AddressOf Me.MediaElement1_DownloadProgressChanged
        MediaElement1.Source = New Uri(url, UriKind.Absolute)

    End Sub

    Private Sub MediaElement1_DownloadProgressChanged(sender As Object, e As System.Windows.RoutedEventArgs)
        ProgressBar1.Value = MediaElement1.DownloadProgress * 100
    End Sub


เพิ่มเติม
นอกจากนี้ยังมี Event อื่น ๆ ที่จะแสดงสถานะของ MediaElement เช่น

Private Sub Video_BufferingProgressChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        BufferProgress.Value = (VideoMediaElement.BufferingProgress * 100)
        OffsetValue.Value = (VideoMediaElement.DownloadProgressOffset * 100)
End Sub


Example ตัวอย่างการใช้ MediaElement และ DownloadProgress แสดงสถานะการดาวน์โหลด ProgressBar แบบง่าย ๆ

Windows Phone MediaElement DownloadProgress

     <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <MediaElement Height="172" HorizontalAlignment="Left" Margin="97,97,0,0" Name="MediaElement1" VerticalAlignment="Top" Width="250" />
            <ProgressBar Height="42" HorizontalAlignment="Left" Margin="8,313,0,0" Name="ProgressBar1" VerticalAlignment="Top" Width="442" />
        </Grid>

    </Grid>

XAML Layout Design

VB.NET
Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()

        Dim url As String = "http://wwww.thaicreate.com/wp/vdo.mp4"
        AddHandler MediaElement1.DownloadProgressChanged, AddressOf Me.MediaElement1_DownloadProgressChanged
        MediaElement1.Source = New Uri(url, UriKind.Absolute)

    End Sub

    Private Sub MediaElement1_DownloadProgressChanged(sender As Object, e As System.Windows.RoutedEventArgs)
        ProgressBar1.Value = MediaElement1.DownloadProgress * 100
    End Sub

End Class









C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;

namespace PhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            string url = "http://wwww.thaicreate.com/wp/vdo.mp4";
           MediaElement1.DownloadProgressChanged += this.MediaElement1_DownloadProgressChanged;
            MediaElement1.Source = new Uri(url, UriKind.Absolute);

        }

        private void MediaElement1_DownloadProgressChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            ProgressBar1.Value = (MediaElement1.DownloadProgress * 100);
        }

    }
}

Code ที่เป็น VB.NET และ C# ที่จะควบคุมการทำงานของโปรแกรม

Screenshot ทดสอบบน Emulator

Windows Phone MediaElement DownloadProgress

ในขณะที่กำลังเล่นไฟล์ Media ด้วย MediaElement จะมีการโหลดข้อมูลจาก Server มาเป็นระยะ ๆ โดยจะโหลดไปด้วยและเล่นไปด้วย

เพิ่มติม
ในกรณีที่ต้องการแสดงสถานะของการเล่นไฟล์ Media ด้วยว่าได้เล่นถึงตำแหน่งไหนแล้ว และแสดงสถานะบน Slider หรือ ProgressBar สามารถอ่านได้ที่บทความนี้

Windows Phone Slider Progress and Media Player (Slider MediaElement)

Windows Phone Slider Progress and Media Player (Slider MediaElement)








.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-04 21:38:57 / 2017-03-25 22:03:21
  Download : Download  Windows Phone Media Player (MediaElement) and DownloadProgress (URL,Website)
 Sponsored Links / Related

 
Windows Phone Slider Progress and Media Player (Slider MediaElement)
Rating :

 
Windows Phone Image Source From URL(Website) Download and ProgressBar
Rating :

 
Windows Phone Open URL(Website) Web Browser (WebBrowser,IsIndeterminate Progress)
Rating :

 
Windows Phone and ProgressBar Using Thread/Multiple Thread (Silverlight)
Rating :

 
Windows Phone SystemTray and ProgressIndicator
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่