Android Audio Development (7): Using the OpenSL ES API (bottom)

Source: Internet
Author: User

This article is the seventh article in my "Android Audio development" series, the previous article generally describes the basic overview of the Android OpenSL ES API, which tells you what the framework has, what you can do, and what you can't do. This article focuses on some of the key design and concepts of the OPENSL ES framework and its API interfaces, so that you can better read the relevant code for OPENSL ES only if you understand them. The sample code is placed at the end of the article, and I believe you understand the basic concepts and you can easily read the details of the code.


1. Object-oriented C-language interface


OpenSL es is written in C, but its interface is an object-oriented approach, not a series of function interfaces, but a Interface way to provide the API, which is a more important point to understand the OpenSL ES API.


This may be said to be more abstract, for example, the General C language Library, such as: The math library, the interface provided may be this:


Double cosh (double);d ouble sinh (double);d ouble tanh (double);


We call this function directly in the code, but OpenSL ES does not provide the API, and its majority API requires such access:


The following code is the "initialize" SLENGINEITF engineobject for the Audio Engine object; Slresult result = (*engineobject)->realize (Engineobject, Sl_boolean_false);


Thus, OpenSL ES API Most of the number is called through the "object", if developed under the Android NDK C code, it should not be too unfamiliar, because we call "jni* env" function is also called the same way.


2. Objects and Interfaces


OpenSL ES has two concepts that must be understood, that is, object and interface,object can be imagined as Java's object class, Interface can be imagined as Java Interface, but they are not exactly the same, the following further explained Their relationship:


(1) Each object may have one or more Interface, and the official defines a series of Interface for each object.

(2) Each Object object provides some of the most basic operations, such as: Realize,resume,getstate,destroy, and so on, if you want to use the function supported by the object, you must pass its getinterface function to get Interface interface, and then access the function function via Interface

(3) Not every system has implemented all the Interface OPENSL ES is defined for Object, so you need to make some choices and judgments when acquiring Interface.


By looking at the "OpenSLES.h" file, we can see the IDs of all the object objects defined by the OpenSL ES, and we can create the corresponding object instances by using the object ID.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/D6/wKiom1cvHVvg-z39AAIbcyKy2jA188.png "title=" 1.png " alt= "Wkiom1cvhvvg-z39aaibcyky2ja188.png"/>


Among them, our more commonly used should be: ENGINE, Audioplayer and Audiorecorder object.


Again, all the Interface IDs are defined in the "OpenSLES.h" file, and we can get the corresponding function interface from the object through the Interface ID.


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/D3/wKioL1cvHlKT-AA-AAcnHCYfm7s495.png "title=" 2.png " alt= "Wkiol1cvhlkt-aa-aacnhcyfm7s495.png"/>


3. State mechanism of OPENSL ES


OpenSL ES also has a more important concept, which is its state mechanism:


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/D3/wKioL1cvHoOwUlRqAAGULV5vk6Y741.png "title=" 3.png " alt= "Wkiol1cvhoowulrqaagulv5vk6y741.png"/>

Any object of OpenSL ES, once created, enters the sl_object_state_unrealized state, in which the system does not allocate any resources until the realize function is called.


After realize the object, it goes into the sl_object_state_realized state, which is an "available" state in which the individual functions and resources of the object can be accessed normally.


When some system events occur, such as an error or an Audio device being preempted by another application, the OpenSL ES object enters the sl_object_state_suspended state and calls the resume function if it wants to return to normal use.


When the object's Destroy function is called, the resource is freed and returned to the sl_object_state_unrealized state.


In short, the life cycle of a OpenSL ES object is the process from create to destroy, and the life cycle control is done through the developer display call.


4. Commonly used objects and structures


Mind maintains a concept, that is, in OpenSL es, all API access and control is done through the Interface, even OpenSL ES inside the Object is also through the SLOBJECTITF Interface to access and use.


4.1 Engine Object and SLENGINEITF Interface


OpenSL ES inside the core object is: Engine object, audio engine objects, it mainly provides the following two functions:


(1) Managing the life cycle of Audio Engine

(2) Provide management interface: SLENGINEITF, this interface can be used to create all other object objects

(3) Provide device property query interface: SLENGINECAPABILITIESITF and SLAUDIOIODEVICECAPABILITIESITF, these interfaces can query the device for some property information


The Engine object is created in the following way:


SLOBJECTITF engineobject;slcreateengine (&engineobject, 0, nullptr, 0, nullptr, nullptr);


Initialize/Destroy:


(*engineobject)->realize (Engineobject, Sl_boolean_false);(*engineobject)->destroy (engineObject);


Get the Management interface:


SLENGINEITF engineengine; (*engineobject)->getinterface (Engineobject, Sl_iid_engine, & (EngineEngine));


Below we can happily use Engineengine to create all the other objects of OpenSL ES.


4.2 Media Object


Another important object in OpenSL ES is media object, which represents the abstraction of multimedia functions, such as: player, recorder, and so on.


We can create an instance of the player object with the Createaudioplayer method provided by SLENGINEITF, and you can create a recorder instance by SLENGINEITF the Createaudiorecorder method provided.


4.3 Data Source and data Sink


OpenSL ES, both structures exist as arguments for creating Media object objects, and data source represents the input source information, i.e. where the data comes from, what data parameters are entered, and data sink represents the output information. That is, the data output to where, with what parameters to output.


4.3.1 Basic definition


The definition of Data Source is as follows:


typedef struct SLDATASOURCE_ {void *plocator; void *pformat;} Sldatasource;


The Data Sink is defined as follows:


typedef struct SLDATASINK_ {void *plocator; void *pformat;} Sldatasink;


Among them, Plocator mainly has the following several kinds:


Sldatalocator_addresssldatalocator_bufferqueuesldatalocator_iodevicesldatalocator_midibufferqueuesldatalocator _uri


That is, the input source/output source of the Media Object object, which can be either a URL or Device, or from a buffer queue, is completely configured by the specific type and scenario of a media object object.


4.3.2 Sample Description


Different Media object instances, the specifics of data source and data sink are not the same.


For example, for the player:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/D6/wKiom1cvHuezbdo3AAIuZfv-pHc958.png "title=" 4.png " alt= "Wkiom1cvhuezbdo3aaiuzfv-phc958.png"/>


And for Recorder:


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/D3/wKioL1cvH9DhoyauAAHH2i9iQPU292.png "title=" 5.png " alt= "Wkiol1cvh9dhoyauaahh2i9iqpu292.png"/>

5. Sample programs and references


Confined to space, about the OpenSL ES of the Player and Recorder related API detailed usage, not in this article, in fact, understand the above introduction of these concepts, combined with the official "Opensl_es_specification_1.0.1.pdf", As well as the sample code I gave, it's easy to read and understand the usage of these APIs, as shown in the example code:


Https://github.com/Jhuster/AudioDemo


6. Summary


about how Android OpenSL ES API is introduced here, the article is not clear of the place to welcome messages or letters [email protected] exchange, or follow my Sina Weibo @ Lu _ June or the public number @Jhuster to obtain the latest articles and information.


650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/7F/D6/wKiom1cvHxqDp9WlAACb8XAe6Uo227.jpg "title=" Weixin _jhuster.jpg "alt=" Wkiom1cvhxqdp9wlaacb8xae6uo227.jpg "/>


This article is from the "jhuster column" blog, be sure to keep this source http://ticktick.blog.51cto.com/823160/1771239

Android Audio Development (7): Using the OpenSL ES API (bottom)

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.