Skip to content

Commit 6fccb9f

Browse files
committed
添加资源文件语音播放(raw,assets)
添加资源文件语音播放(raw,assets)
1 parent a03777a commit 6fccb9f

File tree

3 files changed

+306
-10
lines changed

3 files changed

+306
-10
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.app.Activity;
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
8+
import android.media.AudioManager;
9+
import android.net.Uri;
810
import android.os.Build;
911
import android.os.Handler;
1012
import android.os.Looper;
@@ -50,6 +52,7 @@
5052
import com.netease.nimlib.sdk.RequestCallback;
5153
import com.netease.nimlib.sdk.RequestCallbackWrapper;
5254
import com.netease.nimlib.sdk.ResponseCode;
55+
import com.netease.nimlib.sdk.auth.AuthService;
5356
import com.netease.nimlib.sdk.auth.LoginInfo;
5457
import com.netease.nimlib.sdk.friend.FriendService;
5558
import com.netease.nimlib.sdk.friend.constant.FriendFieldEnum;
@@ -144,7 +147,7 @@ public void login(String contactId, String token, final Promise promise) {
144147
// LogUtil.i(TAG, "contactId:" + contactId);
145148
// LogUtil.i(TAG, "token:" + token);
146149
// LogUtil.i(TAG, "md5:" + MD5.getStringMD5(token));
147-
// NIMClient.getService(AuthService.class).openLocalCache(contactId);
150+
NIMClient.getService(AuthService.class).openLocalCache(contactId);
148151
LoginService.getInstance().login(new LoginInfo(contactId, token), new RequestCallback<LoginInfo>() {
149152
@Override
150153
public void onSuccess(LoginInfo loginInfo) {
@@ -1270,7 +1273,7 @@ public int onResult(int code, IMMessage message) {
12701273
});
12711274
if (result == 0) {
12721275
showTip("请选择消息");
1273-
}else if (result == 1) {
1276+
} else if (result == 1) {
12741277
showTip("该类型消息不支持撤销");
12751278
}
12761279
return 0;
@@ -1646,6 +1649,13 @@ public void play(String audioFile, Promise promise) {
16461649
audioPlayService.play(handler, reactContext, audioFile);
16471650
}
16481651

1652+
@ReactMethod
1653+
public void playLocal(String resourceFile,String type, Promise promise) {
1654+
1655+
Uri uri = Uri.parse(resourceFile);
1656+
audioPlayService.playAudio(handler, reactContext, AudioManager.STREAM_MUSIC, uri.getScheme(), uri.getPath());
1657+
}
1658+
16491659
/**
16501660
* 停止播放录音
16511661
*

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.facebook.react.bridge.ReactContext;
1212
import com.netease.im.ReactCache;
1313
import com.netease.im.uikit.common.util.log.LogUtil;
14-
import com.netease.nimlib.sdk.media.player.AudioPlayer;
1514
import com.netease.nimlib.sdk.media.player.OnPlayListener;
1615

1716
import static android.content.Context.SENSOR_SERVICE;
@@ -22,7 +21,10 @@
2221

2322
public class AudioPlayService implements SensorEventListener {
2423

25-
AudioPlayer currentAudioPlayer;
24+
final static String SCHEME_FILE = "file";
25+
final static String SCHEME_RAW = "raw";
26+
final static String SCHEME_ASSETS = "assets";
27+
AudioPlayerM currentAudioPlayer;
2628
private String currentFile;
2729
private int state;
2830
private int currentAudioStreamType = AudioManager.STREAM_VOICE_CALL;
@@ -42,12 +44,18 @@ public static AudioPlayService getInstance() {
4244
}
4345

4446
public void play(Handler handler, ReactContext context, String filePath) {
47+
playAudio(handler, context, -1, SCHEME_FILE, filePath);
48+
}
49+
50+
public void playAudio(Handler handler, ReactContext context, int audioStreamType, String type, String filePath) {
4551

4652
if (TextUtils.isEmpty(filePath)) {
4753
return;
4854
}
4955

50-
register(context, true);
56+
if (audioStreamType == -1) {
57+
register(context, true);
58+
}
5159
if (isPlayingAudio()) {
5260
stopAudio(handler);
5361
if (currentFile != null && currentFile.equals(filePath)) {
@@ -58,12 +66,18 @@ public void play(Handler handler, ReactContext context, String filePath) {
5866
currentFile = filePath;
5967

6068
if (currentAudioPlayer == null) {
61-
currentAudioPlayer = new AudioPlayer(context);
69+
currentAudioPlayer = new AudioPlayerM(context);
6270
}
6371
// ReactCache.emit(ReactCache.observeAudioRecord, ReactCache.createAudioPlay("Volume", context.getCurrentActivity().getVolumeControlStream()));
6472

6573
context.getCurrentActivity().setVolumeControlStream(currentAudioStreamType); // 默认使用听筒播放
66-
currentAudioPlayer.setDataSource(filePath);
74+
if (TextUtils.equals(type, SCHEME_FILE)) {
75+
currentAudioPlayer.setDataSource(filePath);
76+
} else if (TextUtils.equals(type, SCHEME_RAW)) {
77+
currentAudioPlayer.setDataSource(AudioPlayerM.Type.raw, filePath);
78+
} else if (TextUtils.equals(type, SCHEME_ASSETS)) {
79+
currentAudioPlayer.setDataSource(AudioPlayerM.Type.assets, filePath);
80+
}
6781

6882
currentAudioPlayer.setOnPlayListener(new BasePlayerListener(currentAudioPlayer));
6983

@@ -78,17 +92,24 @@ public void stopPlay(Handler handler, ReactContext context) {
7892

7993
private SensorManager sensorManager;
8094
private Sensor sensor;
95+
private boolean hasRegister;
8196

8297
void register(ReactContext context, boolean register) {
98+
99+
if (hasRegister && register) {
100+
return;
101+
}
102+
83103
if (sensorManager == null) {
84104
sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
85105
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
86106
}
87107
if (register) {
88108
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
89-
} else {
109+
} else if (hasRegister && !register) {
90110
sensorManager.unregisterListener(this, sensor);
91111
}
112+
hasRegister = register;
92113
}
93114

94115
@Override
@@ -189,10 +210,10 @@ interface AudioControllerState {
189210

190211
public class BasePlayerListener implements OnPlayListener {
191212

192-
protected AudioPlayer listenerPlayingAudioPlayer;
213+
protected AudioPlayerM listenerPlayingAudioPlayer;
193214
private long position = -1;
194215

195-
public BasePlayerListener(AudioPlayer playingAudioPlayer) {
216+
public BasePlayerListener(AudioPlayerM playingAudioPlayer) {
196217
listenerPlayingAudioPlayer = playingAudioPlayer;
197218
}
198219

0 commit comments

Comments
 (0)