Basic use of android-MediaPlayer-playing audio and video

Source: Internet
Author: User

Preparations:

1. SurfaceView

Provides a dedicated drawing surface embedded inside of a view hierarchy. you can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen

2. SurfaceHolder

Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available throughSurfaceView
Class.

3. MediaPlayer.

4. Playback file data source

Else ---------------------------------------------------------------------------------------------------------------------------------

<SurfaceView
Android: id = "@ + id/surfaceview"
Android: layout_width = "320px"
Android: layout_height = "240px"

/>

1. Obtain surfaceview -------- surfaceview = (surfaceview) findviewbyid (R. Id. surfaceview );

2. Obtain surfaceholder ------- surfaceholder = surfaceview. getholder ();

Surfaceholder. settype (surfaceholder. surface_type_push_buffers); // set the display content to be pushed from the outside.

Surfaceholder. addcallback (this); // a class instance is required to implement the surfaceholder. Callback interface.

3. instantiate a player mediaplayer = new mediaplayer ();

Mediaplayer. setdatasource ("http://www.bogotobogo.com/Video/sample.3gp"); // sets the data source

Mediaplayer. setdisplay (surfaceholder); // The settings are displayed in the surfaceview

Mediaplayer. setAudioStreamType (AudioManager. STREAM_MUSIC); // you can specify the stream format.

Mediaplayer. prepare ();

Mediaplayer. start ();

Related Article

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.