您好,登錄后才能下訂單哦!
這篇文章主要介紹“SDK For Android快速接入的方法是什么”,在日常操作中,相信很多人在SDK For Android快速接入的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SDK For Android快速接入的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
由于三個版本接入方式大同小異,本文將著重介紹基礎(chǔ)版接入過程,標準版和專業(yè)版可以基于基礎(chǔ)版方式接入,后續(xù)僅說明接入差異的地方。
目前 aar 平臺版本最低要求 >= 4.3,先從SDK 下載頁面下載相應(yīng)版本的 SDK,解壓之后,將 libs
文件夾下的 QuSdk-RC.aar
拷到 Android 工程模塊中的 libs
文件夾下,將 jniLibs
文件夾下的 armeabi-v7a
文件夾也整體拷貝到 libs
文件夾下。
拷貝完成之后目錄的文件如下:
cdn.com/a3c99cdf2c604e073dabbb2aa90a1f05294c1fed.png">
之后按照如下方式修改 Android 項目工程主模塊下的 build.gradle
文件:
Step1. 修改 jniLibs
的源文件夾;
android { sourceSets.main { jniLibs.srcDir "libs" } }
Step2. 將 libs
文件夾加入倉庫中;
repositories { flatDir { dirs 'libs' } }
Step3. 增加 aar 所需依賴。
dependencies { implementation(name: 'QuSdk-RC', ext: 'aar') implementation 'com.android.support:appcompat-v7:24.2.1' implementation 'com.android.support:design:24.2.1' implementation 'com.google.code.findbugs:jsr305:3.0.0' implementation 'com.github.bumptech.glide:glide:3.7.0' implementation 'pub.devrel:easypermissions:0.2.1' implementation 'com.squareup.okhttp3:okhttp:3.2.0' implementation 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar' implementation 'com.squareup.okio:okio:1.12.0' implementation 'com.google.code.gson:gson:2.8.0' }
如果此處遭遇
java.lang.NoSuchFieldError
錯誤,可以參考短視頻安卓常見問題解決。
請根據(jù)具體的項目情況選擇合適的 SDK 初始化時機,Demo 工程在 Applicatioin 的 onCreate()
方法中初始化。
package me.bogerchan.alishortvideodemo import android.app.Application import com.aliyun.common.httpfinal.QupaiHttpFinal /** * Created by hb.chen on 2018/1/6. */ class MyApplication : Application() { override fun onCreate() { super.onCreate() System.loadLibrary("QuCore-ThirdParty") System.loadLibrary("QuCore") QupaiHttpFinal.getInstance().initOkHttpFinal() } }
經(jīng)過上述過程,實際上已經(jīng)接入完成,這時候你可以參考文檔直接開始使用各種 API 了,附下示例代碼。
package me.bogerchan.alishortvideodemo import android.Manifest import android.app.Activity import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v7.app.AppCompatActivity import android.widget.Toast import com.aliyun.demo.recorder.AliyunVideoRecorder import com.aliyun.struct.common.VideoQuality import com.aliyun.struct.snap.AliyunSnapVideoParam import me.bogerchan.alishortvideodemo.basic.R class MainActivity : AppCompatActivity() { companion object { val REQUEST_CODE_RECORD_VIDEO = 1 val REQUEST_CODE_FOR_PERMISSION = 2 } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findViewById(R.id.btn_start_record).setOnClickListener { startRecordActivity() } ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO), REQUEST_CODE_FOR_PERMISSION) } private fun startRecordActivity() { val recordParam = AliyunSnapVideoParam.Builder() .setResolutionMode(AliyunSnapVideoParam.RESOLUTION_720P) .setRatioMode(AliyunSnapVideoParam.RATIO_MODE_9_16) .setRecordMode(AliyunSnapVideoParam.RECORD_MODE_AUTO) .setNeedClip(true) .setMaxDuration(10000) .setMinDuration(2000) .setVideQuality(VideoQuality.HD) .setSortMode(AliyunSnapVideoParam.SORT_MODE_MERGE) .build() AliyunVideoRecorder.startRecordForResult(this, REQUEST_CODE_RECORD_VIDEO, recordParam) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { REQUEST_CODE_RECORD_VIDEO -> { if (resultCode == Activity.RESULT_OK && data != null) { val type = data.getIntExtra(AliyunVideoRecorder.RESULT_TYPE, 0) if (type == AliyunVideoRecorder.RESULT_TYPE_CROP) { Toast.makeText(this, "類型為裁剪", Toast.LENGTH_SHORT).show() } else if (type == AliyunVideoRecorder.RESULT_TYPE_RECORD) { Toast.makeText(this, "文件路徑為 ">
標準版相較于基礎(chǔ)版,在引入 so 文件時候多了幾個文件,同時 aar
文件名有所變動。最終拷貝結(jié)果如下:
build.gradle
文件修改與基礎(chǔ)版接入一樣,只是需要將接入 aar
文件名替換成標準版對應(yīng)的名字。
相較于基礎(chǔ)版,需要加載的 so 增多了幾個,其中部分 so 文件作為可選功能根據(jù)實際情況決定是否加載,具體可以參閱阿里云短視頻 SDK 文檔。接入后的 Application 文件參考:
package me.bogerchan.alishortvideodemo import android.app.Application import com.aliyun.common.httpfinal.QupaiHttpFinal /** * Created by hb.chen on 2018/1/6. */ class MyApplication : Application() { override fun onCreate() { super.onCreate() System.loadLibrary("aliresample") System.loadLibrary("live-openh364") System.loadLibrary("QuCore-ThirdParty") System.loadLibrary("QuCore") QupaiHttpFinal.getInstance().initOkHttpFinal() } }
經(jīng)過上述過程,實際上已經(jīng)接入完成,這時候你可以參考文檔直接開始使用各種 API 了,附下示例代碼。
package me.bogerchan.alishortvideodemo import android.Manifest import android.content.pm.PackageManager import android.opengl.GLSurfaceView import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v7.app.AppCompatActivity import android.widget.Toast import com.aliyun.recorder.AliyunRecorderCreator import com.aliyun.struct.recorder.CameraType import com.aliyun.struct.recorder.MediaInfo import me.bogerchan.alishortvideodemo.std.R class MainActivity : AppCompatActivity() { companion object { val REQUEST_CODE_FOR_PERMISSION = 1 } private val mRecorder by lazy { AliyunRecorderCreator.getRecorderInstance(this) } private var mCameraType = CameraType.FRONT override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO), REQUEST_CODE_FOR_PERMISSION) initAliyunRecorder() findViewById(R.id.btn_start_record).setOnClickListener { Toast.makeText(this, "開始錄制片段", Toast.LENGTH_SHORT).show() mRecorder.startRecording() } findViewById(R.id.btn_stop_record).setOnClickListener { Toast.makeText(this, "停止錄制片段", Toast.LENGTH_SHORT).show() mRecorder.stopRecording() } findViewById(R.id.btn_finish_record).setOnClickListener { Toast.makeText(this, "結(jié)束錄制", Toast.LENGTH_SHORT).show() mRecorder.finishRecording() } findViewById(R.id.btn_change_camera_type).setOnClickListener { Toast.makeText(this, "切換前后置", Toast.LENGTH_SHORT).show() mRecorder.switchCamera() } } override fun onStart() { super.onStart() mRecorder.startPreview() } override fun onPause() { super.onPause() mRecorder.stopPreview() } override fun onDestroy() { super.onDestroy() AliyunRecorderCreator.destroyRecorderInstance() } private fun initAliyunRecorder() { mRecorder.setDisplayView(findViewById(R.id.glsv_content) as GLSurfaceView) val mediaInfo = MediaInfo() mediaInfo.videoWidth = 800 mediaInfo.videoHeight = 1200 mediaInfo.isHWAutoSize = true mRecorder.setMediaInfo(mediaInfo) mRecorder.setCamera(mCameraType) mRecorder.setOutputPath(externalCacheDir.absolutePath + "/capture.mp4") } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { REQUEST_CODE_FOR_PERMISSION -> { grantResults.forEach { if (it == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, "沒有權(quán)限,不玩了", Toast.LENGTH_SHORT).show() finish() return@forEach } } } } } }
專業(yè)版相較于基礎(chǔ)版,在引入 so 文件時候多了幾個文件,同時 aar
文件名有所變動。
build.gradle
文件修改與基礎(chǔ)版接入一樣,只是需要將接入 aar
文件名替換成專業(yè)版對應(yīng)的名字。
相較于基礎(chǔ)版,需要加載的 so 增多了幾個,其中部分 so 文件作為可選功能根據(jù)實際情況決定是否加載,具體可以參閱阿里云短視頻 SDK 文檔。接入后的 Application 文件參考:
package me.bogerchan.alishortvideodemo import android.app.Application import com.aliyun.common.httpfinal.QupaiHttpFinal /** * Created by hb.chen on 2018/1/6. */ class MyApplication : Application() { override fun onCreate() { super.onCreate() System.loadLibrary("live-openh364") System.loadLibrary("QuCore-ThirdParty") System.loadLibrary("QuCore") System.loadLibrary("FaceAREngine") System.loadLibrary("AliFaceAREngine") QupaiHttpFinal.getInstance().initOkHttpFinal() } }
經(jīng)過上述過程,實際上已經(jīng)接入完成,這時候你可以參考文檔直接開始使用各種 API 了,附下示例代碼。
package me.bogerchan.alishortvideodemo import android.Manifest import android.content.pm.PackageManager import android.opengl.GLSurfaceView import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v7.app.AppCompatActivity import android.widget.Toast import com.aliyun.recorder.AliyunRecorderCreator import com.aliyun.struct.recorder.CameraType import com.aliyun.struct.recorder.MediaInfo import me.bogerchan.alishortvideodemo.pro.R class MainActivity : AppCompatActivity() { companion object { val REQUEST_CODE_FOR_PERMISSION = 1 } private val mRecorder by lazy { AliyunRecorderCreator.getRecorderInstance(this) } private var mCameraType = CameraType.FRONT override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO), REQUEST_CODE_FOR_PERMISSION) initAliyunRecorder() findViewById(R.id.btn_start_record).setOnClickListener { Toast.makeText(this, "開始錄制片段", Toast.LENGTH_SHORT).show() mRecorder.startRecording() } findViewById(R.id.btn_stop_record).setOnClickListener { Toast.makeText(this, "停止錄制片段", Toast.LENGTH_SHORT).show() mRecorder.stopRecording() } findViewById(R.id.btn_finish_record).setOnClickListener { Toast.makeText(this, "結(jié)束錄制", Toast.LENGTH_SHORT).show() mRecorder.finishRecording() } findViewById(R.id.btn_change_camera_type).setOnClickListener { Toast.makeText(this, "切換前后置", Toast.LENGTH_SHORT).show() mRecorder.switchCamera() } } override fun onStart() { super.onStart() mRecorder.startPreview() } override fun onPause() { super.onPause() mRecorder.stopPreview() } override fun onDestroy() { super.onDestroy() AliyunRecorderCreator.destroyRecorderInstance() } private fun initAliyunRecorder() { mRecorder.setDisplayView(findViewById(R.id.glsv_content) as GLSurfaceView) val mediaInfo = MediaInfo() mediaInfo.videoWidth = 800 mediaInfo.videoHeight = 1200 mediaInfo.isHWAutoSize = true mRecorder.setMediaInfo(mediaInfo) mRecorder.setCamera(mCameraType) mRecorder.needFaceTrackInternal(true) mRecorder.setOutputPath(externalCacheDir.absolutePath + "/capture.mp4") } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { REQUEST_CODE_FOR_PERMISSION -> { grantResults.forEach { if (it == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, "沒有權(quán)限,不玩了", Toast.LENGTH_SHORT).show() finish() return@forEach } } } } } }
到此,關(guān)于“SDK For Android快速接入的方法是什么”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。