1111import com .facebook .react .bridge .ReactContext ;
1212import com .netease .im .ReactCache ;
1313import com .netease .im .uikit .common .util .log .LogUtil ;
14- import com .netease .nimlib .sdk .media .player .AudioPlayer ;
1514import com .netease .nimlib .sdk .media .player .OnPlayListener ;
1615
1716import static android .content .Context .SENSOR_SERVICE ;
2221
2322public 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