Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play videos using surfaceview+MediaPlayer in android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how android plays videos using surfaceview+MediaPlayer". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to play videos with android using surfaceview+MediaPlayer".

There are two main ways to play videos in Android:

Use its own player. Specify Action as ACTION_VIEW,Data and Uri,Type as its MIME type to use the VideoView that comes with android. This method is too simple to introduce the use of SurfaceView+MediaPlayer. The effect of this method is better, which is also introduced here.

SurfaceView has been available since android 1.0 and is very easy to use. Generally speaking, UI refresh needs to be done in UI thread, but surfaceview can do refresh in non-UI thread. This is very convenient, such as online playback, you do not need to write your own handler to achieve communication between the two threads, you can directly play video in non-UI threads.

Steps:

1. Call the player.setDataSource () method to set the resource to be played, which can be a file, a file path, or URL. 2. Calling MediaPlayer.setDisplay (holder) to set surfaceHolder,surfaceHolder can be obtained through the getHolder () method of surfaceview. 3. Call MediaPlayer.prepare () to prepare. 4. Call MediaPlayer.start () to play the video.

This is a rough step, but these alone are not enough.

You need to make sure that surfaceHolder is ready before the second step. So you need to set a callback for surfaceHolder

Call the addCallback () method. Callback has three callback functions, as follows:

SurfaceHolder.Callback {@ Override public void surfaceCreated (SurfaceHolder holder) {} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {} @ Override public void surfaceDestroyed (SurfaceHolder holder) {}}

SurfaceCreated () will be called back when the SurfaceHolder is created. Here, you can do some initialization operations. SurfaceDestroyed () will call back when the SurfaceHolder is destroyed. Here, you can do some operations to release resources to prevent memory leaks.

Generally, surfaceHolder is set for MediaPlayer in surfaceCreated.

@ Override public void surfaceCreated (SurfaceHolder holder) {player.setDisplay (holder);}

Paste the specific code below:

Public class VideoActivity extends Activity {private SurfaceView surfaceView; private MediaPlayer player; private SurfaceHolder holder; private ProgressBar progressBar; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.surfaceview_item); surfaceView = (SurfaceView) findViewById (R.id.surfaceView); progressBar= (ProgressBar) findViewById (R.id.progressBar); / / the video link may have failed String uri= "http://video.dispatch.tc.qq.com/77613075/x0021o8d3g3.mp4?sdtfrom=v1001&type=mp4&vkey=23289E4B8D0F4B6CF18703222DFD0038845D8F56A75EEC20D5D4FDE678093D9AB211EFD7F4C99E5A612A96A04F46CEEB483628CFFBEA493D3AADBFCB81A540F7A92193874192FA0F70D1099DF330B2B419D45736554CB9BB3435019C985F530C5960E4B20FEBD5FAED17DC9F1FCE1C73&platform=10902&fmt=auto&sp=350&guid=1175defd049d3301e047ce50d93e9c7a"; Player=new MediaPlayer (); try {player.setDataSource (this, Uri.parse (uri)); holder=surfaceView.getHolder (); holder.addCallback (new MyCallBack ()); player.prepare (); player.setOnPreparedListener (new MediaPlayer.OnPreparedListener () {@ Override public void onPrepared (MediaPlayer mp) {progressBar.setVisibility (View.INVISIBLE); player.start (); player.setLooping (true);}}); catch (IOException e) {e.printStackTrace () } private class MyCallBack implements SurfaceHolder.Callback {@ Override public void surfaceCreated (SurfaceHolder holder) {player.setDisplay (holder);} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {} @ Override public void surfaceDestroyed (SurfaceHolder holder) {}

Xml file:

The above is all the content of the article "how to play videos with surfaceview+MediaPlayer in android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report