Android MediaPlayer Architecture — creation process of mediaplayer Android MediaPlayer Architecture — Introduction (1)

This article is for the author's own study. The content of the article only comes from the author's poor thinking. Please don't hesitate to give advice on the problems.

Mediaplayer can be used to control the playback of audio / video files or streaming media. In Android, mediaplayer class is used as the basic class of audio and video playback, and a series of processing has been carried out around it. The simplest step in learning a new module is to find a typical application and analyze the data flow and control flow of the whole module through its implementation. Typical mediaplayer interfaces in Java include video playback class videoview and audio dedicated mediaplayer class.

Invalid Sign

In Android, the videoview component is provided to play video files. To play video with VideoView components, you first need to create the component in the layout file, then get the component in Activity, and apply the setVideoPath () method or setVideoURI () method to load the video to play, and finally call the start () method to play the video. In addition, the videoview component also provides stop () and pause () methods to stop or pause the playback of video.

In app, the typical simple use of videoview is as follows:

Let's see the effect first. Just a few lines of code, a player is ready. You can also pause, fast forward, fast backward and progress bar control.

PS: videoview also provides many other playback control APIs, which will not be introduced here. The above code is only a personal demo, which is inevitably wrong. Please refer to it carefully.

Any gorgeous language is not as simple and direct as source code

After the calling process of setvideopath (string path) - > setvideouri (URI) - > setvideouri (URI, 1) "> headers), the program finally comes to the openvideo() function:

It can be clearly seen from the above code that we first create a mediaplayer class object, and then go to setdatasource. Here, the processing of videoview:: openvideo gives the final processing power to mediaplayer. Next, we enter the world of mediaplayer

First, focus on the creation process of mediaplayer object, which is also a basic requirement for analyzing Android source code. Go through the processing flow of Java -- > JNI (libmedia_jni. So) - > frameworks (libmedia. So).

Mediaplayer.java constructor. This part is also analyzed in the introduction of Android mediaplayer architecture (I)

You can see that when using videoview to create mediaplayer, you will go through the following steps: new videoview - > New mediaplayer - > native_ Setup is a typical object creation process and passed to JNI.

Invalid Sign

Invalid Sign

3.2 setdatasource process

Mediaplayer Java class provides a variety of setdatasource methods to set different URI playback streams. Here, we take playing local files as an example to introduce the processing flow:

  VideoView::setVideoURI() --> MediaPlayer::setDataSource(mContext,mHeaders); --> MediaPlayer::setDataSource(uri.toString()) --> MediaPlayer::setDataSource(path,null,null) --> MediaPlayer::setDataSource(fd) --> setDataSource(fd,0,0x7ffffffffffffffL) --> _ setDataSource(fd,offset,length)

Finally, it will be transferred to_ Setdatasource (FD, length). See that this method is declared as a native method

At the JNI layer, we find the JNI method implementation corresponding to this method:

  android_ media_ MediaPlayer_ The setdatasourcefd() method is defined as follows:

As you can see from the above code, status is finally called_ t MediaPlayer::setDataSource(int fd,int64_t offset,int64_t length)

//*********************************************************************************************************************************************************************

Invalid Sign

Invalid Sign

In the typical binder C / S architecture, obtain the proxy of mediaplayerservice, create a player through mediaplayerservice, and then call setdatasource for this player.

Startup and acquisition

Mediaplayerservice, like other binder services, provides external services as a server. It is started in MediaServer:

  /frameworks/av/media/mediaserver/main_ mediaserver.cpp

Definition of the instance() method in / frameworks / AV / media / libmedialayerservice / mediaplayerservice.cpp:

In the above code, we registered a binder service named "media. Player", that is, mediaplayerservice, and then we can request this service through binder = SM - > getservice (string16 ("media. Player")

Creation of player

After obtaining the mediaplayer service, go to create player: SP < imediaplayer > player (Service - > create (thisdosessionid));

Create request processing:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>