Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / XAML

Play AVI files in Silverlight 5 using MediaElement and MediaStreamSource - P/Invoke

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Sep 2011CPOL5 min read 40.2K   1.7K   5  
This code demostrates how to use Silverlight 5 with OOB+elevated trust to play a local video (.avi). Uses P/Invoke support for native code
Imports System.Runtime.InteropServices

Public Class cAVIDefs

    <DllImport("avifil32.dll", PreserveSig:=True)> _
    Public Shared Function AVIFileOpen(ByRef ppfile As Int32, ByVal szFile As String, ByVal uMode As Int32, ByVal pclsidHandler As Int32) As Int32
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIFileGetStream(ByVal pfile As Int32, ByRef ppavi As IntPtr, ByVal fccType As Int32, ByVal lParam As Int32) As Int32
    End Function

    <DllImport("avifil32.dll", PreserveSig:=True)> _
    Public Shared Function AVIStreamStart(ByVal pavi As Int32) As Int32
    End Function

    <DllImport("avifil32.dll", PreserveSig:=True)> _
    Public Shared Function AVIStreamLength(ByVal pavi As Int32) As Int32
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIStreamInfo(ByVal pAVIStream As Int32, ByRef psi As AVISTREAM_INFO, ByVal lSize As Int32) As Int32
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIStreamGetFrameOpen(ByVal pAVIStream As IntPtr, ByRef bih As BITMAPINFOHEADER) As Int32
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIStreamGetFrameClose(ByVal pGetFrameObj As Integer) As Integer
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIStreamGetFrame(ByVal pGetFrameObj As Int32, ByVal lPos As Int32) As Int32
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIStreamRelease(ByVal aviStream As IntPtr) As Integer
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Function AVIFileRelease(ByVal pfile As Integer) As Integer
    End Function

    <DllImport("avifil32.dll")> _
    Public Shared Sub AVIFileExit()
    End Sub

    <DllImport("gdi32.dll")> _
    Public Shared Function SelectObject(ByVal hdc As Int32, ByVal hObject As Int32) As Int32
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function GetDC(ByVal hwnd As Int32) As Int32
    End Function

    <DllImport("gdi32.dll")> _
    Public Shared Function CreateCompatibleDC(ByVal hdc As Int32) As Int32
    End Function

    <DllImport("gdi32.dll")> _
    Public Shared Function DeleteDC(ByVal hdc As Int32) As Int32 'BOOL
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function ReleaseDC(ByVal hwnd As Int32, ByVal hdc As Int32) As Int32
    End Function

    <DllImport("winmm.dll")> _
    Shared Function timeGetTime() As Int32
    End Function

    Public Structure BITMAPFILEHEADER '14 bytes
        Public bfType As Integer
        Public bfSize As Int32
        Public bfReserved1 As Integer
        Public bfReserved2 As Integer
        Public bfOffBits As Int32
    End Structure

    Public Structure RGBQUAD
        Public Red As Byte
        Public Green As Byte
        Public Blue As Byte
        Public Reserved As Byte
    End Structure

    Public Structure BITMAP
        Public bmType As Int32
        Public bmWidth As Int32
        Public bmHeight As Int32 '
        Public bmWidthBytes As Int32
        Public bmPlanes As Int16
        Public bmBitsPixel As Int16
        Public bmBits As Int32
    End Structure

    Public Structure BITMAPINFO
        Public bmiHeader As BITMAPINFOHEADER
        Public bmiColors As RGBQUAD
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
    Public Structure BITMAPINFOHEADER
        <MarshalAs(UnmanagedType.U4)> Public biSize As Int32
        Public biWidth As Int32
        Public biHeight As Int32
        Public biPlanes As Int16
        Public biBitCount As Int16
        <MarshalAs(UnmanagedType.U4)> Public biCompression As Int32
        <MarshalAs(UnmanagedType.U4)> Public biSizeImage As Int32
        Public biXPelsPerMeter As Int32
        Public biYPelsPerMeter As Int32
        <MarshalAs(UnmanagedType.U4)> Public biClrUsed As Int32
        <MarshalAs(UnmanagedType.U4)> Public biClrImportant As Int32
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
    Public Structure RECT
        <MarshalAs(UnmanagedType.U4)> Public left As Int32
        <MarshalAs(UnmanagedType.U4)> Public top As Int32
        <MarshalAs(UnmanagedType.U4)> Public right As Int32
        <MarshalAs(UnmanagedType.U4)> Public bottom As Int32
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
    Public Structure AVISTREAM_INFO
        <MarshalAs(UnmanagedType.U4)> Public fccType As Int32
        <MarshalAs(UnmanagedType.U4)> Public fccHandler As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwFlags As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwCaps As Int32
        <MarshalAs(UnmanagedType.U2)> Public wPriority As Int16
        <MarshalAs(UnmanagedType.U2)> Public wLanguage As Int16
        <MarshalAs(UnmanagedType.U4)> Public dwScale As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwRate As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwStart As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwLength As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwInitialFrames As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwSuggestedBufferSize As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwQuality As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwSampleSize As Int32
        Public rcFrame As RECT
        <MarshalAs(UnmanagedType.U4)> Public dwEditCount As Int32
        <MarshalAs(UnmanagedType.U4)> Public dwFormatChangeCount As Int32
        <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.U2, SizeConst:=64)> _
        Public szName As Int16()
    End Structure

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other Norconsult Telematics, KSA
Canada Canada
I have been a Developer for many years. I have worked as Senior Developer in Kenya, Canada and Saudi Arabia. I enjoy coding and I am looking forward to more challenges with new Technologies

I am currently IT Manager at Norconsult Telematics, Saudi Arabia.

Comments and Discussions