Android uses Textureview to play video

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="font-size:32px">1. Introduction</span></strong></p></p><p><p><span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; line-height:22px; text-align:justify"><span style="font-size:18px">If you want to display an online video or any data stream such as video or OpenGL scenes, you can do it with Android Textureview.</span></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; line-height:22px; text-align:justify"><span style="font-size:18px"><br></span></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; line-height:22px; text-align:justify"><span style="font-size:18px">1). Textureview's Brother Surfaceview.</span></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px">The video or OpenGL content of the application is often displayed in a special UI Control: Surfaceview. The way Surfaceview works is to create a new window that is placed after the app Window. This approach is very efficient because the Surfaceview window refreshes without the need to redraw the Application's window (the View drawing mechanism of the Android normal window is layered, and any child element or partial refresh causes the entire view structure to be redrawn all at once, so it is inefficient But the need for a common application interface is more than enough, but Surfaceview also has some very inconvenient limitations.</span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px">Because the content of Surfaceview is not on the application window, transformations (panning, zooming, rotating, and so On) cannot be used. It is also difficult to put in a ListView or scrollview, and you cannot use some of the UI controls ' features such as <code style="font-family:Consolas,‘Liberation Mono‘,Menlo,Courier,monospace; border:0px; margin:0px; padding:0.2em 0px; line-height:normal">View.setAlpha()</code> .</span></p></p><p style="font-size:14px; border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="font-size:14px; border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="font-size:14px; border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="font-size:14px; border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px">2). in order to solve this problem, Android 4.0 introduced TEXTUREVIEW.</span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"><span style="border:0px; margin:0px; padding:0px">Textureview</span> compared to surfaceview, Textureview does not create a separate surface for drawing, which allows it to perform some transformations, set transparency, and so on, like a normal view. In addition, the Textureview must be in the window where hardware acceleration is enabled.</span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"><br></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; Color:rgb (51,51,51); Font-family: ' Helvetica,tahoma,arial,sans-serif '; Text-align:justify "><span style=" font-size:18px "> problems encountered in the Project: 1. before using <span style=" color:rgb (51,51,51); font-family: ' Helvetica,tahoma,arial,sans-serif '; font-size:18px; line-height:22px; Text-align:justify ">surfaceview play video, from the picture switch to play video, there will be a black screen Phenomenon. </span></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; Color:rgb (51,51,51); Font-family: ' Helvetica,tahoma,arial,sans-serif '; Text-align:justify "><span style=" font-size:18px "><span style=" color:rgb (51,51,51); font-family: ' Helvetica,Tahoma , Arial,sans-serif '; font-size:18px; line-height:22px; Text-align:justify ">                            &NBS P    2.<span style="color:rgb (51,51,51); Font-family: ' Helvetica,tahoma,arial,sans-serif '; font-size:18px; line-height:22px; Text-align:justify ">surfaceview Flexibility without <span style=" Color:rgb (51,51,51); Font-family: ' Helvetica,tahoma,arial,sans-serif '; font-size:18px; line-height:22px; Text-align:justify ">textureview Good. </span></span></span></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:32px"><strong>2. Project Source code</strong></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:14px">Mainactivity.java file</span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"></p></p><pre name="code" class="java"><span style= "font-size:14px;" >package Com.example.textureviewvideo;import Java.io.file;import Java.io.fileoutputstream;import Java.io.inputstream;import Java.io.outputstream;import Android.app.activity;import Android.content.res.assetmanager;import Android.graphics.surfacetexture;import Android.media.AudioManager;import Android.media.mediaplayer;import Android.media.mediaplayer.onpreparedlistener;import Android.os.Bundle;import Android.os.environment;import Android.util.log;import Android.view.surface;import Android.view.TextureView;import Android.view.view;import Android.view.textureview.surfacetexturelistener;import Android.widget.ImageView;public Class Mainactivity extends Activity implements Surfacetexturelistener{//private Textureview textureview;private MediaPlayer mmediaplayer;private Surface surface;private ImageView videoimage; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Textureview textureview= (textureview) Findviewbyid (r.id.textureview); textureview.setsurfacetexturelistener (this);//set listener function Rewrite 4 methods//textureview=new textureviewtest (this);//setcontentview (textureview); videoimage= (ImageView) Findviewbyid ( r.id.video_image);} @Overridepublic void onsurfacetextureavailable (surfacetexture surfacetexture, int width,int Height) { System.out.println ("onsurfacetextureavailable onsurfacetextureavailable"); surface=new surface (surfacetexture); New Playervideo (). start ();//open a thread to play the video} @Overridepublic void onsurfacetexturesizechanged (surfacetexture surface, int width,int height) {System.out.println ("onsurfacetexturesizechanged onsurfacetexturesizechanged");} @Overridepublic boolean onsurfacetexturedestroyed (surfacetexture surfacetexture) {System.out.println (" onsurfacetexturedestroyed onsurfacetexturedestroyed "); surfacetexture=null;surface=null;mmediaplayer.stop (); Mmediaplayer.release (); return true;} @Overridepublic void onsurfacetextureupdated (surfacetexture surfacetexture) {//system.Out.println ("onsurfacetextureupdated onsurfacetextureupdated");} Private class Playervideo extends Thread{@Overridepublic void run () {try {file file=new file (environment.getexternalstor Agedirectory () + "/ansen.mp4"); If (!file.exists ()) {//file does not exist CopyFile (); } mmediaplayer= New MediaPlayer (); Mmediaplayer.setdatasource (file.getabsolutepath ()); Mmediaplayer.setsurface (surface); Mmediaplayer.setaudiostreamtype (audiomanager.stream_music); Mmediaplayer.setonpreparedlistener (new onpreparedlistener () {@Overridepublic void onprepared (MediaPlayer Mp) { Videoimage.setvisibility (view.gone); Mmediaplayer.start ();} }); Mmediaplayer.prepare (); } catch (Exception E) {e.printstacktrace (); }}}public interface playercontroller{public void play (); /** * If SDcard does not have a file, copy the past */private void CopyFile () {assetmanager assetmanager = this.getassets (); InputStream in = null; OutputStream out = null; Try {in = Assetmanager.open ("ansen.mp4"); String NewFileName = EnvironmenT.getexternalstoragedirectory () + "/ansen.mp4"; out = new FileOutputStream (newfilename); byte[] buffer = new byte[1024]; int read; While (read = In.read (buffer))! =-1) {out.write (buffer, 0, read); } in.close (); in = null; Out.flush (); Out.close (); out = null; } catch (Exception e) {log.e ("tag", e.getmessage ()); }}}</span></pre><p><p></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; font-size:14px; line-height:22px; text-align:justify">Textureview created when the display picture, and then initialize the player, pre-load the video, if the video file does not exist, from the assets under Copy to SDcard directory, video loading finished hidden pictures, My side of the picture by default is the Android project comes with the picture, You can display the desired picture according to your NEEDS.</span><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"><br></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><br></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px">Activity_main.xml Layout file</span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"></span></p></p><pre name="code" class="html"><pre name="code" class="html"><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" match_parent " > <textureview android:id= "@+id/textureview" android:layout_width= "wrap_content" Android : layout_height= "wrap_content"/> <imageview android:id= "@+id/video_image" android:layout_ Width= "match_parent" android:layout_height= "match_parent" android:src= "@drawable/ic_launcher"/> </RelativeLayout></pre></pre>Put a textureview with a imageview.<span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; font-size:18px; line-height:22px; text-align:justify"><span style="color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; font-size:18px; line-height:22px; text-align:justify">Textureview Initialize display ImageView</span> ...</span>Hides the ImageView when the video is playing, and does not show a black screen when switching Past.<br><br><p><p></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"><br></span></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:32px">3.</span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:32px"></span> <span style="font-size:14px">pictures displayed before Playback: pictures displayed after Playback:</span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:14px"></span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:14px"><br></span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:14px"><br></span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><strong><span style="font-size:14px"><br></span></strong></p></p><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><p style="border:0px; margin-top:8px; margin-bottom:8px; padding-top:0px; padding-bottom:0px; line-height:22px; color:rgb(51,51,51); font-family:‘Helvetica,Tahoma,Arial,sans-serif‘; text-align:justify"><span style="font-size:18px"><br></span></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android uses Textureview to play video</p></p></span>

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.