linkv_rtc_plugin 0.0.1 linkv_rtc_plugin: ^0.0.1 copied to clipboard
A new flutter plugin project.
example/lib/main.dart
import 'dart:async';
import 'dart:math';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:linkv_rtc_plugin/linkv_api_defines.dart';
import 'package:linkv_rtc_plugin/linkv_rtc_plugin.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'linkv_ui.dart';
import 'live_page.dart';
void main() => runApp(MyApp());
const String APP_ID_LIVE = "XYWAhtXcWoApAUCXeTZjbcSvdrrunAhj";
const String APP_SECRET_LIVE =
"09FAF2A1AC61DA50E6C8FBED654186DCAFF206FCFE4A932093728E209A4476CB5D2E725CE7261DE19DC9F28902F83725E84938C8CCC35670C647E6A2E8EE998DB1DA091197137A9473B096826062673BCB21829872BFE32FF12F20AE9AD58897570508D8A9508CBD7BF3AA5C0E46B1DE26E297BE4FDEF8D16100ADA107F5D7C3CD176308F29DC4332EC725F438B80FC24A2DBC0D484F93DDE894F52FD1F309FEA63E54FF04EA8BA22D04A26637E99FAAB1B81805B812C542B16EF498E076DB85";
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
print("wing myBackgroundMessageHandler:");
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
print("wing data: $data");
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
print("wing notification: $notification");
}
// Or do other work.
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
debugShowCheckedModeBanner: false,
home: MainView(),
);
}
}
class MainView extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MainView> {
final _roomIdFieldController = TextEditingController();
bool _isSDKInit = false;
String _version = "";
String userId = "";
bool _cameraAuthed = false;
var TAG = "main [Wing] ";
bool isTokenRequesting = false;
bool isIMAuthed = true;
@override
void initState() {
super.initState();
initSDK();
LinkvRtcPlugin.buildVersion().then((value) => {
this.setState(() {
_version = value;
})
});
_roomIdFieldController.text = "1352";
getUid().then((value) {
userId = value;
print("D/Wing own userId = " + userId);
});
}
@override
void dispose() {
super.dispose();
}
Future<String> getUid() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String uid = prefs.getString("uid");
if (uid == null || uid.length == 0) {
uid = Random().nextInt(10000).toString();
prefs.setString("uid", uid);
}
return uid;
}
void initSDK() {
LinkvRtcPlugin.initSDK(APP_ID_LIVE, APP_SECRET_LIVE, '', false)
.then((result) {
print('initSDK result:$result');
if (result == LVErrorCode.SUCCESS) {
_isSDKInit = true;
}
});
LinkvRtcPlugin.setLogLevel(1);
}
void onClickWatchLive() async {
print("onClickWatchLive");
bool isCan = await canLive();
if (!isCan) return;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LivePage(false, _roomIdFieldController.text, userId)));
}
void onClickStartLive() async {
print("onClickStartLive");
bool isCan = await canLive();
if (!isCan) return;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LivePage(true, _roomIdFieldController.text, userId)));
}
Future<bool> canLive() async {
// 1. 相机权限
bool canLive = true;
if (!_cameraAuthed) canLive = await checkPermission();
if (!canLive) return canLive;
// 2. 是否输入了房间id
if (_roomIdFieldController.text.length == 0) {
LinkvUi.showAlert(context, "Please enter your room number!");
return false;
}
// 3. SDK是否已经初始化成功了
if (!_isSDKInit) {
initSDK();
return false;
}
return true;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text("uid :$userId"),
),
body: SafeArea(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => FocusScope.of(context).requestFocus(new FocusNode()),
child: Container(
padding: const EdgeInsets.only(left: 20, top: 20, right: 20),
child: ListView(
children: <Widget>[
Row(
children: <Widget>[
Text("Version: "),
Expanded(
child: Text("$_version"),
)
],
),
Padding(
padding: const EdgeInsets.only(top: 50),
),
TextField(
controller: _roomIdFieldController,
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(
left: 10, top: 12, bottom: 12),
hintText: "Please enter room_id",
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey)),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.cyan))),
),
Padding(
padding: const EdgeInsets.only(top: 20),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width - 20,
color: Colors.cyan,
child: CupertinoButton(
child: Text(
"Start Live",
style: TextStyle(color: Colors.white),
),
onPressed: onClickStartLive,
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width - 20,
color: Colors.cyan,
child: CupertinoButton(
child: Text(
"Watch Live",
style: TextStyle(color: Colors.white),
),
onPressed: onClickWatchLive,
),
),
],
),
),
),
),
),
);
}
// MARK: - Permission
Future<bool> checkPermission() async {
_cameraAuthed = true;
Permission _camera = Permission.camera;
Permission _microphone = Permission.microphone;
PermissionStatus status;
status = await _camera.request();
if (status != PermissionStatus.granted) return false;
status = await _microphone.request();
if (status != PermissionStatus.granted) return false;
return true;
}
}