Android Media Player, the broadcast side is so simple.

Source: Internet
Author: User
Tags file url

There's nothing else. Hope to engage in streaming media development cases. Also promised to provide friends blog. Last week. Make the last chance.

Here, share with you.

The first thing to understand is the concept: what is streaming media? Reprint please specify the source http://blog.csdn.net/gaoshouxiaodi/article/details/41259351

Streaming media: The so-called streaming media refers to the media format that is played on the internet in a streaming way.

Streaming media, also known as streaming media, refers to a business using a video transmission server to send programs as packets sent to the network. After the user extracts the data from the decompression device, the program will show up as before it is sent.

Common formats for streaming media: Mov. asf. 3gp. Viv. swf. Rt. Rp. Rm. Each format has its own pros and cons. We can decide according to the actual situation.

Generally speaking, there are roughly three ways to implement streaming media.

One. Audio and video files are split into several small files. While it is playing. Download them individually. and then play. Also called pseudo-streaming media.

Second, the video through the conversion software, on the server side to convert to streaming media format files. Then the client plays.

Three. In the process of recording the video locally.

Direct codec to a streaming media format file. (Maybe I'll talk about this alone in my blog)

In fact, the principle of the implementation of streaming media is similar to the 1th, just can be understood as, split the package a lot of other, smaller. And is still a total. Its network transmission, the same is a number of parcel channel or multi-channel issued.

Our well-known Android phones support video formats that are not many, 3GP. mp4 and so on. Can be targeted to the general demand, this support is really little poor. The fake idea of playing other formats on your Android phone. This involves the codec of the video.

Video decoding, divided into soft decoding and hard decoding. Unfortunately, neither of me.

I don't want to be here temporarily.

OK. There are a lot of people, they selfless dedication, in their dictionary, their time is our time. Their money is our money, their daughter-in-law ... Hum..

They are called open source.

I found a very good open source project on the Internet, Vitamio. Such a good thing that I did not know before. So I started to download their SDK. Demo, development documentation and other practical stuff.

Unfortunately, I didn't read it. Give me the feeling is that they provide the things disorganized, but it is really good things, how to do, resistant to temper, engage!

https://www.vitamio.org/docs/Tutorial/2014/0210/29.html glued to their introductory link.

Interested friends can take a look.

It is not entirely counted. The Vitamio framework supports no more than hundreds of video formats.

The following is an introduction to the simple playback implementation:

The first is the interface:

<p></p><pre name= "code" class= "HTML" ><?

XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation=" Vertical "> <io.vov.vitamio.widget.centerlayout android:layout_width=" Match_parent " android: layout_height= "Match_parent" android:orientation= "vertical" > <io.vov.vitamio.widget.videoview android:id= "@+id/surface_view" android:layout_width= "match_parent" android:layout_height= "Match_ Parent " android:layout_centerhorizontal=" true " android:layout_centervertical=" true "/> </ Io.vov.vitamio.widget.centerlayout></linearlayout>

A very easy to display layout interface,
Io.vov.vitamio.widget.VideoView
is its own definition of the video display control.

Then there is the implementation of the activity:

public class Videoviewdemo extends Activity {/** * todo:set the PATH variable to a streaming video URL or a local media f Ile * path. *///Video Path Private String path = "";p rivate videoview mvideoview; @Overridepublic void OnCreate (Bundle icicle) { Super.oncreate (icicle);//initializes the Vitamio framework.

The return value is used to do the related operation. There are usually no things to do. Initialization must be in front of the display interface. if (! Libschecker.checkvitamiolibs (this)) return;//Display Interface Setcontentview (r.layout.videoview);//Initialize Component Mvideoview = ( Videoview) Findviewbyid (R.id.surface_view);//set two network stream media address, we can directly test. Path = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";p ath = "http://www.modrails.com/videos/ Passenger_nginx.mov "; if (Path = =" ") {//Tell ' the user to provide a media file Url/path. Toast.maketext (Videoviewdemo.this, "Please edit the Videoviewdemo Activity, and set path" + "variable to your media file url/ Path ", Toast.length_long). Show (); return;} else {/* * can also use this method to play streaming media * Mvideoview.setvideouri (Uri.parse (urlstring)); */// Set the path of the Videoview playback Mvideoview.setvideopath (path),//Create a video playback controller, the controller can be defined by itself. Here is the default implementation Mvideoview.setmediacontroller (new Mediacontroller (this));//Request Focus mvideoview.requestfocus ();// Set Playback listener Mvideoview.setonpreparedlistener (new Mediaplayer.onpreparedlistener () {@Overridepublic void onprepared ( MediaPlayer MediaPlayer) {//optional need Vitamio 4.0//set playback speed mEdiaplayer.setplaybackspeed (1.0f);}}); /start playback after loading, this line of code can control the video playback. Mvideoview.start ();}}}

Prior to use. Need to look at the methods in the Videoview class:

class overview


public class Videoview extends Surfaceview implements Mediacontroller.mediaplayercontrol
Used to play a video file. Videoview can load images from various sources (such as resources or contentprovider) and pay attention to calculating video size. For use in a casual layout manager, it also provides display options such as zooming.
Videoview also provides methods that encapsulate a lot of mediaplayer, such as Getvideowidth, Setsubshown.


Constant


public static final int Video_layout_origin
The number of scaling parameters. The original screen size.
Constant Value: 0


public static final int Video_layout_scale
The number of scaling parameters. Screen full screen.
Constant Value: 1


public static final int Video_layout_stretch
The number of scaling parameters. Picture stretching.


Constant Value: 2


public static final int video_layout_zoom
To zoom in, the screen is cropped.
Constant Value: 3


Public methods


public void setvideolayout (int layout, float aspectratio)
Gets the URI of the scanned video.
Number of references
Layout Scaling Parameters
The aspectration aspect ratio, for 0 will self-detect.


public boolean isValid ()
is surface valid. See the IsValid method for your surface.




public void Setvideopath (String path)
Sets the video path.




public void Setvideouri (URI uri)
Sets the video URI. (Can be a network video address)


public void Stopplayback ()
Stops video playback. and release resources.


public void Setmediacontroller (Mediacontroller Controller)
Set up the media controller.
Number of references
Controller media controllers. Attention is Io.vov.vitamio.widget.MediaController.


public void Setonpreparedlistener (Onpreparedlistener l)
Register a callback function that is called after the video preprocessing is complete.

Called after the video preprocessing is complete. The width, height, and aspect ratio information for the video is now available, and you can call Seekto to have the video start playing from the specified location.


public void Setoncompletionlistener (Oncompletionlistener l)
Register a callback function that is called after the video has finished playing.




public void Setonerrorlistener (Onerrorlistener l)
Register a callback function that is called when an error occurs during an asynchronous operation call.

For example, video open failed.




public void Setonbufferingupdatelistener (Onbufferingupdatelistener l)
Note a callback function. Called when the network video stream buffers changes.




public void Setonseekcompletelistener (Onseekcompletelistener l)
Note a callback function. Called after the seek operation is complete.


public void Setonsubtitleupdatelistener (Onsubtitleupdatelistener l)
Register a callback function that is called when the caption needs to be displayed.


public void Setoninfolistener (Oninfolistener l)
Register a callback function that is called when there is a warning or error message. For example: Start buffering, buffering end, download speed changes.




public boolean ontouchevent (motionevent ev)
Handles show/Hide Mediacontroller.




public void Start ()
Start playback.


public void Pause ()
Pauses playback.


public void Suspend ()
Pending (temporary not implemented)


public void Resume ()
Resumes playback.




Public long getduration ()
Gets the length of time the video is playing.


Public long GetCurrentPosition ()
Gets the current playback position.




public void Seekto (long msec)
Sets the playback position.
Number of references
msec Location


public boolean isplaying ()
Whether it is playing.




public int Getbufferpercentage ()
Gets the buffer percentage.


public void SetVolume (float leftvolume, float rightvolume)
Set the volume.
Number of references
Leftvolume left Channel
Rightvolume Right Channel


public int getvideowidth ()
Gets the video width.


public int getvideoheight ()
Gets the video height.


public float Getvideoaspectratio ()
Sets the video width-to-height ratio.

No video or wide-height returns 0.


public void setvideoquality (int quality)
Set the video quality.
Number of references
Quality See MediaPlayer constants: Videoquality_low (Fluent), Videoquality_medium (normal), Videoquality_high (high quality).




public void setbuffersize (int bufSize)
Sets the video buffer size (default 1024KB) unit byte.




public boolean isbuffering ()
Check whether the buffer is complete.




public void setmetaencoding (String encoding)
Sets the metadata encoding.

For example: UTF-8


Public String getmetaencoding ()
Gets the metadata encoding.


Public HashMap Getaudiotrackmap (String encoding)
Gets the audio tracks embedded in the video. For example: 中文版


public int Getaudiotrack ()
Sets the playback track number.


public void Setaudiotrack (int audioindex)
To set the track number, you must use the return value of Getaudiotrackmap.




public void Setsubshown (Boolean shown)
Sets whether captions are displayed.


Number of references
Shown true indicates that subtitles are displayed


public void setsubencoding (String encoding)
Sets the caption encoding.


Number of references
Encoding subtitle encoding. Assume that null will self-detect itself.




public int getsublocation ()
Gets the caption position type. 0 for inline subtitles, 1 for external subtitles.




public void Setsubpath (String subpath)
Sets the external caption path. Must be a local file path.


Public String Getsubpath ()
Gets the external caption path.


public void Setsubtrack (int trackid)
Sets the caption number. Must be a return value of Getsubtrackmap.


public int Getsubtrack ()
Gets the caption number.


Public HashMap Getsubtrackmap (String encoding)
Gets the video inline caption collection.
Number of references
encoding format string encoding. Assume that null will self-detect itself.
return value
Returns a map that consists of a caption name and a caption number.


public boolean canpause ()
Whether it can be paused. (temporarily no function implemented)


public boolean Canseekbackward ()
(temporarily no function implemented)


public boolean Canseekforward ()
(temporarily no function implemented)


Protected method


Protected Boolean isinplaybackstate ()
Whether it is in the playing state.


Real-time proof. Open source is the ladder of human social progress. Reprint please indicate the source.


Vitamio Library


For streaming media codecs. FFmpeg wait, next time talk about.





Android Media Player, the broadcast side is so simple.

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.