1. 程式人生 > >wex5 cordova直播推流與播放器外掛開發

wex5 cordova直播推流與播放器外掛開發

1.android開發可實現功能的原生程式碼(能執行不會程式崩潰,不然後期在wex5中的h5頁面中js呼叫原生程式碼的時候會一直在jni_onload程式無反應)

2.配置外掛檔案建立如下圖的目錄


libs檔案中放外掛需要的依賴包等,res裡面存放的是android的原生程式碼中需要的資原始檔,www檔案下的js檔案是cordova外掛中需要通過js呼叫的CordovaPlugin(具體開發步驟可參考wex5的官方文件cordova android外掛開發),plugin-ex.xml和plugin.js.xdoc是wex5中cordova外掛的說明文件(所有xml檔案必須以<?xml version="1.0" encoding="UTF-8"?>開通並且不能在前面加入空格或者換行),plugin.xml是外掛中最為重要的配置文件具體程式碼如下

<?xml version="1.0" encoding="UTF-8"?>   
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        id="com.justep.cordova.plugin.pusher"    
        version="5.3.6">   
//id為js中呼叫的id名字    version是xml的編譯版本
<engines> <engine name="cordova" version=">=3.3.0" /> </engines> <name>pusher</name> <description>pusher_plugin</description> <js-module src="www/pusher.js" name="pusher"><!--pusher.js的地址路徑--> <clobbers target="navigator.pusher" /> </js-module> <platform name="android"><!--android配置--> <config-file target="res/xml/config.xml" parent="/*"><!--config.xml中新增外掛名字及包路徑對應到具體的cordovaplugin類--> <feature name="pusher"> <param name="android-package" value="com.justep.cordova.plugin.pusher.pusher" /> </feature> </config-file> <config-file target="AndroidManifest.xml" parent="/manifest"> <uses-permission android:name="android.permission.CAMERA"> </uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO"> </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> </config-file> <!--在wex5的專案AndroidManifest.xml新增配置請求-->
<config-file target="AndroidManifest.xml" parent="/manifest/application"> <activity android:name="com.justep.cordova.plugin.pusher.DemoActivity" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize" android:allowBackup="true" android:supportsRtl="true" android:windowSoftInputMode="stateHidden|adjustPan" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> </activity> <activity android:name="com.justep.cordova.plugin.pusher.LiveCameraActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:launchMode="singleTask" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/> </config-file>
<!--在wex5的專案AndroidManifest.xml註冊外掛需要的activity-->
<!--string配置--> <config-file target="res/values/strings.xml" parent="/resources"><string name="max_bitrate">Max Bitrate</string><string name="best_bitrate">Best Bitrate</string><string name="quit_live">Long time reconnect failed, please exit</string><string name="network_message">The current network status is very poor, continue the live broadcast?</string><string name="camera_success">Camera open successfully</string><string name="camera_fail">Camera open failed</string><string name="error_message">-22 error</string><string name="mute_off">Mute close</string><string name="mute_on">Mute on</string><string name="mirror_off">Mirror off</string><string name="mirror_on">Mirror on</string><string name="advance_setting">美顏等級</string><string name="watermark_position">position (topright 1, topleft 2, bottomright 3, bottomleft 4):</string><string name="fps_hint">The fps must be between 0~30</string><string name="watermard_url">水印路徑:</string><string name="fps">Fps</string><string name="front_camera_mirror">前置攝像</string><string name="landscape">水平</string><string name="portrait_normal">垂直</string><string name="ready_push">開始直撿</string><string name="rtmp_url">請輸兿url</string><string name="beauty_level">Beauty Level</string><string name="toggle_switch_flash_on">Flash on</string><string name="toggle_switch_flash_off">Flash off</string><string name="app_camera">Camera switch</string><string name="live_push">Start pushing</string><string name="live_stop">Stop pushing</string><string name="toggle_switch_beauty_on">Beauty on</string><string name="toggle_switch_beauty_off">Beauty off</string><string name="min_bitrate">Min bitrate</string><string name="min_rate_value">300</string><string name="initial_bitrate">Initial bitrate</string><string name="VIDEO_CAPTURE_FPS">Video capture fps:</string><string name="AUDIO_ENCODER_FPS">Audio encoder fps:</string><string name="VIDEO_ENCODER_FPS">Video encoder fps:</string><string name="OUTPUT_BITRATE">Output bitrate:</string><string name="AV_OUTPUT_DIFF">The difference between audio pts and video pts:</string><string name="AUDIO_OUTPUT_FPS">Output fps of audio:</string><string name="VIDEO_OUTPUT_FPS">Output fps of video:</string><string name="STREAM_PUBLISH_TIME">Time costs of stream published:</string><string name="STREAM_SERVER_IP">Stream server IP:</string><string name="VIDEO_DELAY_DURATION">Video delay duration:</string><string name="AUDIO_DELAY_DURATION">Audio delay duration:</string><string name="VIDEO_CACHE_FRAME_CNT">Number of video cache frame:</string><string name="AUDIO_CACHE_FRAME_CNT">Number of audio cache frame:</string><string name="VIDEO_CACHE_BYTE_SIZE">Size of video cache:</string><string name="AUDIO_CACHE_BYTE_SIZE">Size of audio cache:</string><string name="VIDEO_FRAME_DISCARD_CNT">Number of video frame dropped:</string><string name="AUDIO_FRAME_DISCARD_CNT">Number of audio frame dropped:</string><string name="CUR_VIDEO_BUEATY_DURATION">Time costs of beauty:</string><string name="CUR_VIDEO_ENCODER_DURATION">Time costs of video encoding:</string><string name="cur_video_encode_bitrate">Current video encoding bitrate:</string><string name="illegal_output_resolution">The output resolution is illegal</string><string name="VIDEO_OUTPUT_FRAME_COUNT">Number of video output frame:</string><string name="VIDEO_DATA">Size of video output frame:</string><string name="VIDEO_BUFFER_COUNT">Number of video buffer frame:</string><string name="VIDEO_DROPPED_FRAMECOUNT">Number of video dropped frame:</string><string name="AUDIO_DATA">Size of audio output:</string><string name="reconnect_fail">Reconnection failed</string><string name="dialog_title">Error tips</string><string name="ok">OK</string><string name="no_camera_permission">Camera usage is not permitted, please open first</string><string name="no_record_audio_permission">Audio is not permitted to record, please open first</string></config-file>
<!--因為wex5生成的專案已經包含string.xml檔案,直接複製會導致不能成功生成apk,只能將字串全面寫入專案中存在的strings檔案中。注:
如果是寫入的時候目標檔案必須存在,如果是複製檔案的話不能在目標資料夾中存在同名檔案-->
<framework src="src/android/build.gradle" custom="true" type="gradleReference"/><source-file src="src/android/libs/AlivcPlayer-3.2.0.aar" target-dir="libs/"/>     <source-file src="src/android/libs/live-pusher-3.2.0.aar" target-dir="libs/"/>        <!--新增專案依賴 同時還需要配置一個gradle檔案 gradle檔案原始碼後面貼出--> <source-file src="src/android/pusher.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/AdvancedSettingDialog.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/DataStatistics.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/DemoActivity.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/LiveCameraActivity.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/ToastUtils.java" target-dir="src/com/justep/cordova/plugin/pusher" /><source-file src="src/android/res/drawable/ic_launcher_background.xml" target-dir="res/drawable"/><source-file src="src/android/res/drawable/ic_launcher_foreground.xml" target-dir="res/drawable"/><source-file src="src/android/res/mipmap/ic_launcher.xml" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/ic_launcher_round.xml" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/beauty.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/beauty1.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/bubble.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/fash_open.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/finish.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/flash_off.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/microphone_open.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/microphone_off.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/off.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/pause.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/satrt.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/set_up.png" target-dir="res/mipmap"/><source-file src="src/android/res/mipmap/xuanzhuan.png" target-dir="res/mipmap"/><source-file src="src/android/res/layout/activity_live_camera.xml" target-dir="res/layout"/><source-file src="src/android/res/layout/demo_activity.xml" target-dir="res/layout"/><source-file src="src/android/res/layout/layout_advance_setting.xml" target-dir="res/layout"/><source-file src="src/android/res/layout/performance_log.xml" target-dir="res/layout"/><source-file src="src/android/res/values/arrays.xml" target-dir="res/values"/><source-file src="src/android/res/values/colors.xml" target-dir="res/values"/><source-file src="src/android/res/values/styles.xml" target-dir="res/values"/> <!--將需要的資原始檔複製到相應的路徑資料夾下--> </platform> </plugin>

gradle檔案原始碼:

/*
 * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
 *
 * @[email protected]
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apache License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/Apache-2.0/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @[email protected]
 */
//兩種檔案依賴庫方式都加上
repositories { jcenter() flatDir { dirs 'libs' }}repositories { mavenCentral() flatDir { dirs 'libs' }}dependencies { compile(name: 'AlivcPlayer-3.2.0', ext: 'aar') compile(name: 'live-pusher-3.2.0', ext: 'aar')}