Blog article 1: C # + DirectX loop playback without black screen replacement

Source: Internet
Author: User

The Code logic of C # + DirectX audiovideoplayback circulating on the network for video players is basically:

 // Source code of csdn yalan old project <br/> private void playstream () <br/>{< br/> filename = pl [currentindex]. tostring (). tolower (); <br/> int height; <br/> int width; <br/> try <br/>{< br/> If (primaryvideoplaying) <br/>{< br/> Height = panel1.height; <br/> width = panel1.width; <br/> primaryvideoplaying = false; <br/> If (audio1! = NULL) <br/>{< br/> audio1.dispose (); <br/> audio1 = NULL; <br/>}< br/> If (video1! = NULL) <br/>{< br/> video1.dispose (); <br/> video1 = NULL; <br/>}< br/> video1 = video. fromfile (filename); <br/> audio1 = video1.audio; <br/> audio1.volume = 0; // maximum volume <br/> audio1.balance = 0; // audio channel balance <br/> video1.hidecursor (); <br/> lbl_playerstatus.text = filename; <br/> panel1.height = height; <br/> panel1.width = width; <br/> video1.owner = Panel1; <br/> video1.play (); <br/> timer1.start (); <BR/>}< br/> else <br/>{< br/> Height = panel1.height; <br/> width = panel1.width; <br/> primaryvideoplaying = true; <br/> If (audio! = NULL) <br/>{< br/> audio. Dispose (); <br/> audio = NULL; <br/>}< br/> If (video! = NULL) <br/>{< br/> video. dispose (); <br/> Video = NULL; <br/>}< br/> Video = video. fromfile (filename); <br/> audio = video. audio; <br/> audio. volume = 0; // The maximum volume <br/> audio. balance = 0; // audio channel balance <br/> video. hidecursor (); <br/> lbl_playerstatus.text = filename; <br/> panel1.height = height; <br/> panel1.width = width; <br/> video. owner = Panel1; <br/> video. play (); <br/> timer. start (); <br/>}< br />}< Br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. tostring (); <br/>}</P> <p> // video playback timer <br/> private void timer_tick (Object sender, eventargs e) <br/>{< br/> If (video. currentposition> = video. duration) <br/>{< br/> timer. stop (); <br/> GC. collect (); <br/> // release resources after playing the video <br/> try <br/> {<br/> video. stop (); <br/> If (audio! = NULL) <br/> audio. Dispose (); <br/> audio = NULL; <br/> If (video! = NULL) <br/> video. dispose (); <br/> Video = NULL; <br/>}< br/> catch <br/> {<br/> try <br/> {<br/> Video = NULL; <br/>}< br/> catch {}< br/>}</P> <p> currentindex = (currentindex + 1) % Pl. count; <br/> If (currentindex = 0) <br/>{< br/> getplaylist (); <br/>}< br/> else <br/>{< br/> playstream (); <br/>}< br/> private void timereffectick (Object sender, eventargs e) <br/>{< br/> If (video1.currentposition >=video1.duration) <br/>{< br/> timer1.stop (); <br/> GC. collect (); <br/> // release resources after playing the video <br/> try <br/> {<br/> video1.stop (); <br/> If (audio1! = NULL) <br/> audio1.dispose (); <br/> audio1 = NULL; <br/> If (video1! = NULL) <br/> video1.dispose (); <br/> video1 = NULL; <br/>}< br/> catch <br/> {<br/> try <br/> {<br/> video1 = NULL; <br/>}< br/> catch {}< br/>}</P> <p> currentindex = (currentindex + 1) % Pl. count; <br/> If (currentindex = 0) <br/>{< br/> getplaylist (); <br/>}< br/> else <br/>{< br/> playstream (); <br/>}< br/>

 

 

After testing, it is found that although two video and audio instances are initialized for the code modification, there is a black screen interval between the player list (my project uses a generic list <string> array to store the video path) and the video path, especially when playing a network stream, the black screen interval varies with the size of the video file on the network.

After analysis, there are two reasons for the interval of changing the black screen: 1. Time consumed to clean up the garbage; 2. Time consumed to initialize the video file on the video instance after video O1 is played.

 

After improvement, the following code is significantly better than the code on the network, so that no black screen is available for the change:

Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. data. sqlclient; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> using Microsoft. directX. audiovideoplayback; </P> <p> namespace stlive player test <br/>{< br/> Public partial class form1: form <br/> {<br/> Public form1 () <br/> {<br/> initi Alizecomponent (); <br/> form1.checkforillegalcrossthreadcils = false; <br/>}</P> <p> // DirectX <br/> private video video1 = NULL; // broadcaster <br/> private audio audio1 = NULL; <br/> private video video2 = NULL; // broadcast preparation <br/> private audio audio2 = NULL; <br/> private int currentindex = 0; // playback position of the playlist <br/> private bool primaryvideoplaying = true; // determine the caster preparation <br/> private list <string> filename = new list <Str Ing> (); // video path <br/> private int height, width; <br/> private string _ filename = string. empty; <br/> private void form1_load (Object sender, eventargs e) <br/>{< br/> using (sqlconnection conn = new sqlconnection ("Data Source = 192.168.10.250; initial catalog = stlivead; user id = sa; Password = sa; ") <br/>{< br/> Conn. open (); <br/> using (sqlcommand cmd = new sqlcommand ("select * From playlist order by I D ASC ", Conn) <br/>{< br/> sqldatareader DR = cmd. executereader (); <br/> while (dr. read () <br/>{< br/> filename. add (Dr ["filepath"]. tostring (); <br/>}< br/> playstream1 (filename [currentindex]. tostring (); <br/> currentindex = (currentindex + 1) % filename. count; <br/> playstream2 (filename [currentindex]. tostring (); <br/>}</P> <p> private void playstream1 (string _ filename) <br/ >{< Br/> Height = panel1.height; <br/> width = panel1.width; <br/> If (audio1! = NULL) <br/>{< br/> audio1.dispose (); <br/> audio1 = NULL; <br/>}< br/> If (video1! = NULL) <br/>{< br/> video1.dispose (); <br/> video1 = NULL; <br/>}< br/> video1 = video. fromfile (_ filename); <br/> audio1 = video1.audio; <br/> audio1.volume = 0; // maximum volume <br/> audio1.balance = 0; // audio channel balance <br/> video1.hidecursor (); <br/> panel1.height = height; <br/> panel1.width = width; <br/> video1.owner = Panel1; <br/> If (primaryvideoplaying) <br/>{< br/> panel1.visible = true; <br/> panel2.v Isible = false; <br/> video1.play (); <br/> timer1.start (); <br/> label1.text = "Video 1 "; <br/>}< br/> else <br/>{< br/> panel1.visible = false; <br/> panel2.visible = true; <br/> video1.play (); <br/> video1.pause (); <br/>}</P> <p> private void playstream2 (string _ filename) <br/>{< br/> Height = panel2.height; <br/> width = panel2.width; <br/> If (audio2! = NULL) <br/>{< br/> audio2.dispose (); <br/> audio2 = NULL; <br/>}< br/> If (video2! = NULL) <br/>{< br/> video2.dispose (); <br/> video2 = NULL; <br/>}< br/> video2 = video. fromfile (_ filename); <br/> audio2 = video2.audio; <br/> audio2.volume = 0; // maximum volume <br/> audio2.balance = 0; // audio channel balance <br/> video2.hidecursor (); <br/> panel2.height = height; <br/> panel2.width = width; <br/> video2.owner = panel2; <br/> If (primaryvideoplaying = false) <br/>{< br/> panel1.visible = false; <br/> panel2.visible = true; <br/> video2.play (); <br/> timer2.start (); <br/> label1.text = "video 2 "; <br/>}< br/> else <br/>{< br/> panel1.visible = true; <br/> panel2.visible = false; <br/> video2.play (); <br/> video2.pause (); <br/>}</P> <p> private void timerstmtick (Object sender, eventargs E) <br/>{< br/> If (video1.currentposition> = video1.duration) <br/>{< br/> timer1.stop (); <br/> video1.stop (); <br/> primaryvideoplaying = false; <br/> panel1.visible = false; <br/> panel2.visible = true; <br/> video2.play (); <br/> timer2.start (); <br/> label1.text = "video 2"; <br/> GC. collect (); <br/> currentindex = (currentindex + 1) % filename. count; <br/> playstream1 (filename [currentindex]. tostring (); <br/>}</P> <p> private void timer2_tick (Object sender, eventargs E) <br/>{< br/> If (video2.currentposition> = video2.duration) <br/>{< br/> timer2.stop (); <br/> video2.stop (); <br/> primaryvideoplaying = true; <br/> panel1.visible = true; <br/> panel2.visible = false; <br/> video1.play (); <br/> timer1.start (); <br/> label1.text = "Video 1"; <br/> GC. collect (); <br/> currentindex = (currentindex + 1) % filename. count; <br/> playstream2 (filename [currentindex]. tostring (); <br/>}</P> <p >}< br/>

 

 

The code above also constructs two video instances, but it performs the playback of the next video file immediately after the playback of an instance is completed. When the next video file is played, a new video is created to wait for playback, therefore, the black screen interval disappears.

 

I have not tried multithreading yet. In theory, multithreading is better than the new code I posted. The logic is basically the same. If you need it, try to change it to multithreading for testing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.