Skip to content

Commit e970309

Browse files
committed
fix:播放本地音乐,最近会话显示
fix:播放本地音乐,最近会话显示
1 parent 41ac8ae commit e970309

File tree

6 files changed

+50
-16
lines changed

6 files changed

+50
-16
lines changed

android/src/main/java/com/netease/im/RNNeteaseImModule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ public void startSession(String sessionId, String type, final Promise promise) {
14531453

14541454
return;
14551455
}
1456-
sessionService.startSession(sessionId, type);
1456+
sessionService.startSession(handler, sessionId, type);
14571457
}
14581458

14591459
/**
@@ -1653,7 +1653,13 @@ public void play(String audioFile, Promise promise) {
16531653
public void playLocal(String resourceFile, String type, Promise promise) {
16541654

16551655
Uri uri = Uri.parse(resourceFile);
1656-
audioPlayService.playAudio(handler, reactContext, AudioManager.STREAM_MUSIC, uri.getScheme(), uri.getPath());
1656+
LogUtil.i(TAG, "scheme:" + uri.getScheme());
1657+
String filePath = uri.getPath();
1658+
if(filePath.startsWith("/")){
1659+
filePath = filePath.substring(1);
1660+
}
1661+
LogUtil.i(TAG, "path:" + filePath);
1662+
audioPlayService.playAudio(handler, reactContext, AudioManager.STREAM_RING, uri.getScheme(), filePath);
16571663
}
16581664

16591665
/**

android/src/main/java/com/netease/im/ReactCache.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,9 @@ public static Object createRecentList(List<RecentContact> recents, int unreadNum
190190
map.putString("nick", fromNick);
191191
String teamNick = "";
192192
if (contact.getSessionType() == SessionTypeEnum.Team && !TextUtils.equals(LoginService.getInstance().getAccount(), fromAccount)) {
193-
193+
String tid = contact.getContactId();
194+
teamNick = getTeamUserDisplayName(tid, fromAccount)+": ";
194195
if ((contact.getAttachment() instanceof NotificationAttachment)) {
195-
String tid = contact.getContactId();
196-
teamNick = getTeamUserDisplayName(tid, fromAccount);
197-
teamNick += ": ";
198196
if (AitHelper.hasAitExtention(contact)) {
199197
if (contact.getUnreadCount() == 0) {
200198
AitHelper.clearRecentContactAited(contact);

android/src/main/java/com/netease/im/session/AudioPlayService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public void playAudio(Handler handler, ReactContext context, int audioStreamType
5252
if (TextUtils.isEmpty(filePath)) {
5353
return;
5454
}
55-
55+
LogUtil.i(type, "path:" + filePath);
5656
if (audioStreamType == -1) {
5757
register(context, true);
58+
}else {
59+
currentAudioStreamType = audioStreamType;
5860
}
5961
if (isPlayingAudio()) {
6062
stopAudio(handler);
@@ -70,7 +72,9 @@ public void playAudio(Handler handler, ReactContext context, int audioStreamType
7072
}
7173
// ReactCache.emit(ReactCache.observeAudioRecord, ReactCache.createAudioPlay("Volume", context.getCurrentActivity().getVolumeControlStream()));
7274

73-
context.getCurrentActivity().setVolumeControlStream(currentAudioStreamType); // 默认使用听筒播放
75+
if (context.getCurrentActivity() != null) {
76+
context.getCurrentActivity().setVolumeControlStream(currentAudioStreamType); // 默认使用听筒播放
77+
}
7478
if (TextUtils.equals(type, SCHEME_FILE)) {
7579
currentAudioPlayer.setDataSource(filePath);
7680
} else if (TextUtils.equals(type, SCHEME_RAW)) {

android/src/main/java/com/netease/im/session/AudioPlayerM.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.res.AssetFileDescriptor;
5+
import android.content.res.Resources;
56
import android.media.AudioManager;
67
import android.media.AudioManager.OnAudioFocusChangeListener;
78
import android.media.MediaPlayer;
@@ -110,15 +111,18 @@ public final void setDataSource(String var1) {
110111
}
111112

112113
public final void setDataSource(Type defType, String name) {
114+
type = defType;
113115
if (defType == Type.file) {
114116
setDataSource(name);
115117
} else if (defType == Type.raw) {
116-
type = Type.raw;
117118
int resid = mContext.getResources().getIdentifier(name, "raw", mContext.getPackageName());
118-
assetFileDescriptor = mContext.getResources().openRawResourceFd(resid);
119+
try {
120+
assetFileDescriptor = mContext.getResources().openRawResourceFd(resid);
121+
} catch (Resources.NotFoundException e) {
122+
e.printStackTrace();
123+
}
119124
} else if (defType == Type.assets) {
120125
try {
121-
type = Type.assets;
122126
assetFileDescriptor = mContext.getAssets().openFd(name);
123127
} catch (IOException e) {
124128
e.printStackTrace();
@@ -228,12 +232,16 @@ public boolean onError(MediaPlayer var1, int var2, int var3) {
228232
});
229233

230234
try {
231-
if (this.mAudioFile != null) {
235+
if ((type == Type.file && this.mAudioFile != null) || assetFileDescriptor != null) {
232236
if (type == Type.file) {
233237
this.mPlayer.setDataSource(this.mAudioFile);
234238
} else {
235-
AssetFileDescriptor afd = assetFileDescriptor;
236-
this.mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
239+
AssetFileDescriptor fd = assetFileDescriptor;
240+
if (fd.getDeclaredLength() < 0) {
241+
this.mPlayer.setDataSource(fd.getFileDescriptor());
242+
} else {
243+
this.mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
244+
}
237245
}
238246
this.mPlayer.prepare();
239247
this.mPlayer.start();

android/src/main/java/com/netease/im/session/SessionService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.netease.im.session;
22

3+
import android.media.AudioManager;
34
import android.net.Uri;
5+
import android.os.Handler;
46
import android.support.annotation.NonNull;
57
import android.text.TextUtils;
68

@@ -75,6 +77,8 @@ public class SessionService {
7577
private Set<String> timedItems = new HashSet<>(); // 需要显示消息时间的消息ID
7678
private IMMessage lastShowTimeItem; // 用于消息时间显示,判断和上条消息间的时间间隔
7779

80+
private Handler handler;
81+
7882
private SessionService() {
7983
}
8084

@@ -127,6 +131,9 @@ public void onIncomingMessage(@NonNull List<IMMessage> messages) {
127131
updateShowTimeItem(addedListItems, false);
128132
}
129133
List<IMMessage> r = onQuery(addedListItems);
134+
if (r.size() > 0) {
135+
AudioPlayService.getInstance().playAudio(handler, ReactCache.getReactContext(), AudioManager.STREAM_RING, "raw", "msg");
136+
}
130137
refreshMessageList(r);
131138

132139
}
@@ -518,8 +525,9 @@ public int compare(IMMessage o1, IMMessage o2) {
518525

519526
/****************************** 消息处理 ***********************************/
520527

521-
public void startSession(String sessionId, String type) {
528+
public void startSession(Handler handler, String sessionId, String type) {
522529
clear();
530+
this.handler = handler;
523531
this.sessionId = sessionId;
524532
sessionTypeEnum = SessionUtil.getSessionType(type);
525533
registerObservers(true);

android/src/main/java/com/netease/im/session/extension/RedPacketOpenAttachement.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
/**
1212
* Created by dowin on 2017/6/12.
13+
* <p>
14+
* {
15+
* "msgtype": "redpacketOpen",
16+
* "data": {
17+
* "sendId": "",发红包的ID
18+
* "openId": "",拆红包的ID
19+
* "hasRedPacket": "",是否还有红包 '0' '1'是已经拆完
20+
* "serialNo": ""红包ID
21+
* }
22+
* }
1323
*/
1424

1525
public class RedPacketOpenAttachement extends CustomAttachment {
@@ -67,7 +77,7 @@ public String getTipMsg(boolean show) {
6777
sendName = NimUserInfoCache.getInstance().getUserDisplayNameYou(sendId);
6878
}
6979
String end = "";
70-
if (show&&"1".equals(hasRedPacket) && TextUtils.equals(LoginService.getInstance().getAccount(), sendId)) {
80+
if (show && "1".equals(hasRedPacket) && TextUtils.equals(LoginService.getInstance().getAccount(), sendId)) {
7181
end = ",你的红包已被领完";
7282
}
7383
return openName + "领取了" + sendName + "的红包" + end;

0 commit comments

Comments
 (0)