Android Studio 2 - 16、17 第三方腾讯云直播与屏幕适配加语言国际化

Android Studio 2 - 16、17 第三方腾讯云直播与屏幕适配加语言国际化

  • 屏幕适配
    • 在Model中的 bulid.gradle导入依赖
    • 继承应用类
    • 清单文件中加入适配数据
    • XML布局
  • 第三方腾讯云直播
    • 在Model中的 bulid.gradle导入依赖
    • 继承应用类
    • 清单文件中加入适配数据
    • XML布局
    • java代码
  • 国际化
    • 清单文件中加入适配数据
    • 写入成套的引用语音XML
    • 例如
    • XML布局

屏幕适配

在Model中的 bulid.gradle导入依赖

implementation 'com.zhy:autolayout:1.4.5'//屏幕适配

继承应用类

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        AutoLayoutConifg.getInstance().useDeviceSize();
    }
}

清单文件中加入适配数据

 <meta-data android:name="design_width" android:value="当前模板宽度"></meta-data>
 <meta-data android:name="design_height" android:value="当前模板高度"></meta-data>

android:usesCleartextTraffic="true"
        android:name=".App"

XML布局

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="540px"
        android:layout_height="860px" />

</com.zhy.autolayout.AutoLinearLayout>

第三方腾讯云直播

在Model中的 bulid.gradle导入依赖

implementation 'com.tencent.liteavsdk:LiteAVSDK_Smart:latest.release@aar'

继承应用类


public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // 获取到的 licence url
        String licenceURL = "http://license.vod2.myqcloud.com/license/v1/a1fa568129129bf875992ccdce52deab/TXLiveSDK.licence";
        // 获取到的 licence key
        String licenceKey = "f03fadbb706713da050d3d67f91cdc99";
        TXLiveBase.getInstance().setLicence(this, licenceURL, licenceKey);
    }
}

清单文件中加入适配数据

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-feature android:name="android.hardware.Camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_push"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="推流"
        android:textSize="30sp" />

    <Button
        android:id="@+id/btn_over"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="结束"
        android:textSize="30sp" />

    <com.tencent.rtmp.ui.TXCloudVideoView
        android:id="@+id/pusher_tx_cloud_view"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <Button
        android:onClick="onClick"
        android:id="@+id/btn_la"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拉流"
        android:textSize="30sp" />

    <com.tencent.rtmp.ui.TXCloudVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="200dp" />


</LinearLayout>

java代码

package com.example.day16_tencent;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.widget.Toolbar;

import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "jsy";

    private TXLivePusher mLivePusher;

    private TXLivePlayer mLivePlayer;
    private TXCloudVideoView mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //动态权限
        String[] permission = {
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.CAMERA,
                Manifest.permission.READ_PHONE_STATE,

        };
        requestPermissions(permission, 200);

        TXLivePushConfig mLivePushConfig = new TXLivePushConfig();
        mLivePusher = new TXLivePusher(this);

        // 一般情况下不需要修改 config 的默认配置
        mLivePusher.setConfig(mLivePushConfig);

        //启动本地摄像头预览
        TXCloudVideoView mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.startCameraPreview(mPusherView);


        //mPlayerView 即 step1 中添加的界面 view
        mView = (TXCloudVideoView) findViewById(R.id.video_view);
        //创建 player 对象
        mLivePlayer = new TXLivePlayer(this);
        //关键 player 对象与界面 view
        mLivePlayer.setPlayerView(mView);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        }
    }

    //TODO 推流
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_push:
                //此处填写您的 rtmp 推流地址
                String rtmpURL = "rtmp://61832.livepush.myqcloud.com/live/ccc?txSecret=d91d9d91ee3bde632599329c1e3dd0bd&txTime=5D82547F";
                int ret = mLivePusher.startPusher(rtmpURL.trim());
                if (ret == -5) {
                    Log.i(TAG, "startRTMPPush: license 校验失败");
                }
                break;
            case R.id.btn_over:
                break;
            case R.id.btn_la:
                Toast.makeText(this, "1111", Toast.LENGTH_SHORT).show();
                String flvUrl = "https://61832.liveplay.myqcloud.com/live/ccc.flv";
                //推荐 FLV
                mLivePlayer.startPlay(flvUrl, TXLivePlayer.PLAY_TYPE_LIVE_FLV);
                break;
            default:
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLivePlayer.stopPlay(true); // true 代表清除最后一帧画面
        mView.onDestroy();
    }
}

国际化

清单文件中加入适配数据

android:configChanges="locale|layoutDirection|keyboard"
        android:usesCleartextTraffic="true"

写入成套的引用语音XML

Android Studio 2 - 16、17 第三方腾讯云直播与屏幕适配加语言国际化_第1张图片

例如

//写入后 更换手机语言设置才能看到效果
<resources>
    <string name="app_name">国际化</string>
    <string name="talk_text">你来这干嘛?!</string>
    <string name="btn_text">暗夜精灵</string>

</resources>
//或
<resources>
    <string name="app_name">ここぉ</string>
    <string name="talk_text">お早う</string>
    <string name="btn_text">神様</string>

</resources>

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tale_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="@string/talk_text"
        android:textSize="30sp" />

    <Button
        android:id="@+id/btn_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="@string/btn_text"
        android:textSize="30sp" />

</LinearLayout>

你可能感兴趣的:(学习)