Skip to content

Commit ae77aac

Browse files
authored
Merge pull request #135 from skill20/master
新增发送视频接口 sendVoiceMessage
2 parents e9a2cdb + 4f71996 commit ae77aac

File tree

8 files changed

+213
-13
lines changed

8 files changed

+213
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 2.1.4
2+
+新增:新增发送视频接口 sendVoiceMessage
13
## 2.1.2
24
+升级:升级 android jcore 2.8.2
35
## 2.1.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependencies:
1717
1818
//pub.dev 集成
1919
dependencies:
20-
jmessage_flutter: 2.1.2
20+
jmessage_flutter: 2.1.4
2121
```
2222

2323

android/src/main/java/com/jiguang/jmessageflutter/JmessageFlutterPlugin.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
67
import android.media.MediaPlayer;
78
import android.net.Uri;
89
import android.text.TextUtils;
@@ -42,6 +43,7 @@
4243
import cn.jpush.im.android.api.content.LocationContent;
4344
import cn.jpush.im.android.api.content.MessageContent;
4445
import cn.jpush.im.android.api.content.TextContent;
46+
import cn.jpush.im.android.api.content.VideoContent;
4547
import cn.jpush.im.android.api.content.VoiceContent;
4648
import cn.jpush.im.android.api.enums.ContentType;
4749
import cn.jpush.im.android.api.enums.PlatformType;
@@ -308,6 +310,8 @@ public void onMethodCall(MethodCall call, Result result) {
308310
getMessageReceiptDetails(call, result);
309311
} else if (call.method.equals("setMessageHaveRead")) {
310312
setMessageHaveRead(call, result);
313+
} else if (call.method.equals("sendVideoMessage")) {
314+
sendVideoMessage(call, result);
311315
} else {
312316
result.notImplemented();
313317
}
@@ -1045,6 +1049,79 @@ private void sendFileMessage(MethodCall call, Result result) {
10451049
}
10461050
}
10471051

1052+
private void sendVideoMessage(MethodCall call, Result result) {
1053+
HashMap<String, Object> map = call.arguments();
1054+
1055+
int duration;
1056+
String thumbImagePath = "", thumbFormat = "", videoPath, videoFileName = "";
1057+
Map<String, String> extras = null;
1058+
MessageSendingOptions options = null;
1059+
Conversation conversation;
1060+
1061+
try {
1062+
JSONObject params = new JSONObject(map);
1063+
1064+
conversation = JMessageUtils.createConversation(params);
1065+
if (conversation == null) {
1066+
handleResult(ERR_CODE_CONVERSATION, ERR_MSG_CONVERSATION, result);
1067+
return;
1068+
}
1069+
1070+
videoPath = params.getString("videoPath");
1071+
1072+
if (params.has("thumbFormat")) {
1073+
thumbFormat = params.getString("thumbFormat");
1074+
}
1075+
1076+
if (params.has("thumbImagePath")) {
1077+
thumbImagePath = params.getString("thumbImagePath");
1078+
}
1079+
1080+
if (params.has("duration")) {
1081+
duration = params.getInt("duration");
1082+
} else {
1083+
MediaPlayer mediaPlayer = MediaPlayer.create(mContext, Uri.parse(videoPath));
1084+
duration = mediaPlayer.getDuration() / 1000;
1085+
mediaPlayer.release();
1086+
}
1087+
1088+
if (params.has("videoFileName")) {
1089+
videoFileName = params.getString("videoFileName");
1090+
}
1091+
1092+
if (params.has("extras")) {
1093+
extras = fromJson(params.getJSONObject("extras"));
1094+
}
1095+
1096+
if (params.has("messageSendingOptions")) {
1097+
options = toMessageSendingOptions(params.getJSONObject("messageSendingOptions"));
1098+
}
1099+
} catch (JSONException e) {
1100+
e.printStackTrace();
1101+
handleResult(ERR_CODE_PARAMETER, ERR_MSG_PARAMETER, result);
1102+
return;
1103+
}
1104+
try {
1105+
File videoFile = getFile(videoPath);
1106+
1107+
Bitmap bitmap = null;
1108+
if (!TextUtils.isEmpty(thumbImagePath)) {
1109+
bitmap = BitmapFactory.decodeFile(thumbImagePath);
1110+
}
1111+
1112+
VideoContent content = new VideoContent(bitmap, thumbFormat, videoFile, videoFileName, duration);
1113+
if (extras != null) {
1114+
content.setExtras(extras);
1115+
}
1116+
1117+
sendMessage(conversation, content, options, result);
1118+
} catch (Exception e) {
1119+
e.printStackTrace();
1120+
handleResult(ERR_CODE_FILE, ERR_MSG_FILE, result);
1121+
}
1122+
1123+
}
1124+
10481125
private void sendLocationMessage(MethodCall call, Result result) {
10491126
HashMap<String, Object> map = call.arguments();
10501127

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"image_picker","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker-0.7.5+2\\\\","dependencies":[]},{"name":"jmessage_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jmessage-flutter-plugin\\\\","dependencies":[]},{"name":"jpush_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jpush-flutter-plugin\\\\","dependencies":[]},{"name":"modal_progress_hud_nsn","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\modal_progress_hud_nsn-0.1.0-nullsafety-1\\\\","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\flutter_plugin_android_lifecycle-2.0.1\\\\","dependencies":[]},{"name":"image_picker","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker-0.7.5+2\\\\","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"jmessage_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jmessage-flutter-plugin\\\\","dependencies":[]},{"name":"jpush_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jpush-flutter-plugin\\\\","dependencies":[]},{"name":"modal_progress_hud_nsn","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\modal_progress_hud_nsn-0.1.0-nullsafety-1\\\\","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"image_picker_for_web","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker_for_web-2.0.0\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle","image_picker_for_web"]},{"name":"image_picker_for_web","dependencies":[]},{"name":"jmessage_flutter","dependencies":[]},{"name":"jpush_flutter","dependencies":[]},{"name":"modal_progress_hud_nsn","dependencies":[]}],"date_created":"2021-05-24 10:32:10.964211","version":"2.2.0"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"image_picker","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker-0.7.5+2\\\\","dependencies":[]},{"name":"jmessage_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jmessage-flutter-plugin\\\\","dependencies":[]},{"name":"modal_progress_hud_nsn","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\modal_progress_hud_nsn-0.1.0-nullsafety-1\\\\","dependencies":[]},{"name":"video_thumbnail","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\video_thumbnail-0.3.3\\\\","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\flutter_plugin_android_lifecycle-2.0.1\\\\","dependencies":[]},{"name":"image_picker","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker-0.7.5+2\\\\","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"jmessage_flutter","path":"D:\\\\Workspace\\\\flutter\\\\jmessage-flutter-plugin\\\\","dependencies":[]},{"name":"modal_progress_hud_nsn","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\modal_progress_hud_nsn-0.1.0-nullsafety-1\\\\","dependencies":[]},{"name":"video_thumbnail","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\video_thumbnail-0.3.3\\\\","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"image_picker_for_web","path":"D:\\\\Workspace\\\\android\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.flutter-io.cn\\\\image_picker_for_web-2.0.0\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle","image_picker_for_web"]},{"name":"image_picker_for_web","dependencies":[]},{"name":"jmessage_flutter","dependencies":[]},{"name":"modal_progress_hud_nsn","dependencies":[]},{"name":"video_thumbnail","dependencies":[]}],"date_created":"2021-06-01 11:58:00.043626","version":"2.2.0"}

example/lib/main.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter/services.dart';
35
import 'package:flutter_test/flutter_test.dart';
@@ -7,6 +9,7 @@ import 'package:jmessage_flutter_example/conversation_manage_view.dart';
79
import 'package:jmessage_flutter_example/group_manage_view.dart';
810
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
911
import 'package:platform/platform.dart';
12+
import 'package:video_thumbnail/video_thumbnail.dart';
1013

1114
const String kMockAppkey = "e58a32cb3e4469ebf31867e5"; //'你自己应用的 AppKey';
1215
const String kMockUserName = '0001';
@@ -261,6 +264,48 @@ class _MyHomePageState extends State<MyHomePage> {
261264
});
262265
}
263266

267+
void demoSendVideoMessage() async {
268+
print(flutter_log + "demoSendVideoMessage " + usernameTextEC2.text);
269+
270+
setState(() {
271+
_loading = true;
272+
});
273+
274+
if (usernameTextEC2.text == "") {
275+
setState(() {
276+
_loading = false;
277+
_result = "【发消息】对方 username 不能为空";
278+
});
279+
return;
280+
}
281+
String username = usernameTextEC2.text;
282+
283+
PickedFile? selectVideoPath = await ImagePicker().getVideo(
284+
source: ImageSource.gallery, maxDuration: const Duration(seconds: 10));
285+
286+
String? thumbnailPath = await VideoThumbnail.thumbnailFile(
287+
video: selectVideoPath!.path,
288+
imageFormat: ImageFormat.PNG,
289+
maxWidth: 128,
290+
// specify the width of the thumbnail, let the height auto-scaled to keep the source aspect ratio
291+
quality: 25,
292+
);
293+
294+
// print('selectVideoPath ======${selectVideoPath.path},thumbnailPath = $thumbnailPath');
295+
JMSingle type = JMSingle.fromJson({"username": username});
296+
JMVideoMessage msg = await jmessage.sendVideoMessage(
297+
type: type,
298+
duration: null,
299+
thumbFormat: "",
300+
videoFileName: "",
301+
thumbImagePath: thumbnailPath,
302+
videoPath: selectVideoPath.path);
303+
setState(() {
304+
_loading = false;
305+
_result = "【视频消息】${msg.toJson()}";
306+
});
307+
}
308+
264309
static final String chatRoomMsgListenerID =
265310
"chatRoomMsgListenerID"; //监听聊天室消息的监听id
266311
static final String receiveMsgListenerID = "receiveMsgListenerID"; //监听消息的监听id
@@ -1125,6 +1170,9 @@ class _MyHomePageState extends State<MyHomePage> {
11251170
mainAxisAlignment: MainAxisAlignment.center,
11261171
children: <Widget>[
11271172
new Text(" "),
1173+
new CustomButton(
1174+
title: "发送视频消息", onPressed: demoSendVideoMessage),
1175+
new Text(" "),
11281176
new CustomButton(
11291177
title: "会话管理界面",
11301178
onPressed: () {

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies:
2222
# Use with the CupertinoIcons class for iOS style icons.
2323
cupertino_icons: ^1.0.3
2424
image_picker: ^0.7.5+2
25+
video_thumbnail: ^0.3.3
2526

2627
dev_dependencies:
2728
flutter_test:

lib/jmessage_flutter.dart

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class JmessageFlutter {
457457
}
458458

459459
Future<JMUserInfo?> getMyInfo() async {
460-
Map userJson = await _channel.invokeMethod('getMyInfo');
460+
Map? userJson = await _channel.invokeMethod('getMyInfo');
461461
if (userJson == null) {
462462
return null;
463463
} else {
@@ -805,6 +805,47 @@ class JmessageFlutter {
805805
return res;
806806
}
807807

808+
Future<JMVideoMessage> sendVideoMessage({
809+
@required dynamic type,
810+
811+
/// (JMSingle | JMGroup | JMChatRoom)
812+
String? thumbImagePath,
813+
String? thumbFormat,
814+
@required String? videoPath,
815+
String? videoFileName,
816+
int? duration,
817+
JMMessageSendOptions? sendOption,
818+
Map<dynamic, dynamic>? extras,
819+
}) async {
820+
Map param = type.toJson();
821+
Map optionMap = {};
822+
if (sendOption != null) {
823+
optionMap = {
824+
'messageSendingOptions': sendOption.toJson()
825+
..removeWhere((key, value) => value == null)
826+
};
827+
}
828+
829+
if (extras != null) {
830+
param..addAll({'extras': extras});
831+
}
832+
833+
param
834+
..addAll(optionMap)
835+
..addAll({
836+
'thumbImagePath': thumbImagePath,
837+
'thumbFormat': thumbFormat,
838+
'videoPath': videoPath,
839+
'videoFileName': videoFileName,
840+
'duration': duration
841+
});
842+
843+
Map resMap = await _channel.invokeMethod(
844+
'sendVideoMessage', param..removeWhere((key, value) => value == null));
845+
var res = JMNormalMessage.generateMessageFromJson(resMap);
846+
return res;
847+
}
848+
808849
/**
809850
* 消息撤回
810851
*
@@ -2033,33 +2074,37 @@ class JMNormalMessage {
20332074
switch (type) {
20342075
case JMMessageType.text:
20352076
return JMTextMessage.fromJson(json);
2036-
break;
20372077
case JMMessageType.image:
20382078
return JMImageMessage.fromJson(json);
2039-
break;
20402079
case JMMessageType.voice:
20412080
return JMVoiceMessage.fromJson(json);
2042-
break;
20432081
case JMMessageType.location:
20442082
return JMLocationMessage.fromJson(json);
2045-
break;
20462083
case JMMessageType.file:
20472084
return JMFileMessage.fromJson(json);
2048-
break;
20492085
case JMMessageType.custom:
20502086
return JMCustomMessage.fromJson(json);
2051-
break;
20522087
case JMMessageType.event:
20532088
return JMEventMessage.fromJson(json);
2054-
break;
20552089
case JMMessageType.prompt:
20562090
return JMPromptMessage.fromJson(json);
2057-
break;
2091+
case JMMessageType.video:
2092+
return JMPromptMessage.fromJson(json);
20582093
}
20592094
}
20602095
}
20612096

2062-
enum JMMessageType { text, image, voice, file, custom, location, event, prompt }
2097+
enum JMMessageType {
2098+
text,
2099+
image,
2100+
voice,
2101+
file,
2102+
custom,
2103+
location,
2104+
event,
2105+
prompt,
2106+
video
2107+
}
20632108

20642109
class JMTextMessage extends JMNormalMessage {
20652110
final JMMessageType type = JMMessageType.text;
@@ -2146,6 +2191,33 @@ class JMLocationMessage extends JMNormalMessage {
21462191
super.fromJson(json);
21472192
}
21482193

2194+
class JMVideoMessage extends JMNormalMessage {
2195+
String videoPath; // 视频地址
2196+
String thumbFormat; //视频缩略图格式名
2197+
int duration; // 视频时长
2198+
String thumbImagePath; // 视频缩略图
2199+
String videoFileName; // 视频名称
2200+
2201+
Map toJson() {
2202+
var json = super.toJson();
2203+
json['thumbImagePath'] = thumbImagePath;
2204+
json['videoPath'] = videoPath;
2205+
json['duration'] = duration;
2206+
json['thumbImagePath'] = thumbImagePath;
2207+
json['videoFileName'] = videoFileName;
2208+
2209+
return json;
2210+
}
2211+
2212+
JMVideoMessage.fromJson(Map<dynamic, dynamic> json)
2213+
: videoPath = json['videoPath'],
2214+
thumbFormat = json['thumbFormat'],
2215+
duration = json['duration'],
2216+
thumbImagePath = json['thumbImagePath'],
2217+
videoFileName = json['videoFileName'],
2218+
super.fromJson(json);
2219+
}
2220+
21492221
class JMCustomMessage extends JMNormalMessage {
21502222
Map<dynamic, dynamic> customObject; // 自定义键值对
21512223

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: jmessage_flutter
22
description: JIGUANG officially supported JMessage Flutter plugin (Android & iOS). 极光推送官方支持的 Flutter 插件(Android & iOS)(https://www.jiguang.cn).
3-
version: 2.1.2
3+
version: 2.1.4
44
homepage: https://www.jiguang.cn
55

66
environment:

0 commit comments

Comments
 (0)