[2.11.0/1.6.0] bus 司机端/乘客屏接口和UI调整
This commit is contained in:
@@ -11,7 +11,9 @@ import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.PagerSnapHelper;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.SnapHelper;
|
||||
|
||||
import com.amap.api.maps.model.LatLng;
|
||||
import com.elegant.utils.UiThreadHandler;
|
||||
@@ -88,10 +90,13 @@ public class BusPassengerRouteFragment extends
|
||||
|
||||
mRouteInfoView = findViewById(R.id.bus_p_line_cl);
|
||||
mStationsListRv = findViewById(R.id.bus_p_line_stations_rl);
|
||||
|
||||
LinearLayoutManager manager = new LinearLayoutManager(getContext());
|
||||
mStationsListRv.setLayoutManager(manager);
|
||||
mAdapter = new BusPassengerLineStationsAdapter(getContext(), mStationsList);
|
||||
mStationsListRv.setAdapter(mAdapter);
|
||||
SnapHelper snapHelper = new PagerSnapHelper();
|
||||
snapHelper.attachToRecyclerView(mStationsListRv);
|
||||
|
||||
mMapArrowIcon = findViewById(R.id.bus_p_arrow_nor);
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:scaleType="fitXY"
|
||||
android:layout_marginLeft="12px"
|
||||
android:src="@drawable/bus_p_line_blue"
|
||||
app:layout_constraintLeft_toLeftOf="@+id/bus_p_circle"
|
||||
app:layout_constraintRight_toRightOf="@+id/bus_p_circle"
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.mogo.och.bus.bean;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
|
||||
/**
|
||||
*
|
||||
* 中止/结束任务请求
|
||||
*/
|
||||
public class BusCloseTaskRequest {
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.mogo.och.bus.bean;
|
||||
|
||||
import com.mogo.eagle.core.data.BaseData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2021/10/19
|
||||
*/
|
||||
public class BusQueryWriteOffPassengersResponse extends BaseData {
|
||||
public List<WriteOffPassenger> data;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mogo.och.bus.bean;
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
|
||||
/**
|
||||
* 查询核销乘客
|
||||
*/
|
||||
public class BusWriteOffPassengersQueryRequest {
|
||||
|
||||
private String sn;
|
||||
private int taskId;
|
||||
private int siteId;
|
||||
private long verificationTime;
|
||||
public BusWriteOffPassengersQueryRequest(int taskId, int siteId,long prePassengerTime) {
|
||||
this.sn = MoGoAiCloudClientConfig.getInstance().getSn();
|
||||
this.taskId = taskId;
|
||||
this.siteId = siteId;
|
||||
this.verificationTime = prePassengerTime;
|
||||
}
|
||||
|
||||
public String getSn() {
|
||||
return sn;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.mogo.och.bus.bean;
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2022/9/23
|
||||
*/
|
||||
public class WriteOffPassenger {
|
||||
public String phone;
|
||||
public String orderNo;
|
||||
public int passengerSize;
|
||||
public long writeOffTime;
|
||||
}
|
||||
@@ -77,7 +77,9 @@ class BusConst {
|
||||
const val ARRIVE_AT_END_STATION_DISTANCE = 10
|
||||
|
||||
// 轮询
|
||||
const val LOOP_PASSENGER_5S = 5 * 1000L
|
||||
const val LOOP_PASSENGER_2S = 2 * 1000L
|
||||
const val LOOP_PASSENGER_1S = 1 * 1000L
|
||||
const val LOOP_DELAY_500 = 500L
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -30,16 +29,16 @@ import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotPlanningListen
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
|
||||
import com.mogo.eagle.core.utilcode.util.NetworkUtils;
|
||||
import com.mogo.eagle.core.utilcode.util.ToastUtils;
|
||||
import com.mogo.eagle.core.utilcode.util.UiThreadHandler;
|
||||
import com.mogo.map.navi.IMogoCarLocationChangedListener2;
|
||||
import com.mogo.module.common.MogoApisHandler;
|
||||
import com.mogo.och.bus.bean.BusOrderBean;
|
||||
import com.mogo.och.bus.bean.BusOrdersResponse;
|
||||
import com.mogo.och.bus.R;
|
||||
import com.mogo.och.bus.bean.BusQueryWriteOffPassengersResponse;
|
||||
import com.mogo.och.bus.bean.BusRoutePlanningUpdateReqBean;
|
||||
import com.mogo.och.bus.bean.BusRoutesResponse;
|
||||
import com.mogo.och.bus.bean.BusRoutesResult;
|
||||
import com.mogo.och.bus.bean.BusStationBean;
|
||||
import com.mogo.och.bus.bean.QueryLeaveAwayPassengersResponse;
|
||||
import com.mogo.och.bus.bean.WriteOffPassenger;
|
||||
import com.mogo.och.bus.callback.IBusControllerStatusCallback;
|
||||
import com.mogo.och.bus.callback.IRefreshBusStationsCallback;
|
||||
import com.mogo.och.bus.callback.ISlidePannelHideCallback;
|
||||
@@ -54,23 +53,18 @@ import com.mogo.och.common.module.biz.provider.LoginService;
|
||||
import com.mogo.och.common.module.utils.CoordinateCalculateRouteUtil;
|
||||
import com.mogo.och.common.module.utils.DateTimeUtil;
|
||||
import com.mogo.och.common.module.utils.PinYinUtil;
|
||||
import com.mogo.och.common.module.utils.SoundPoolHelper;
|
||||
import com.mogo.service.statusmanager.IMogoStatusChangedListener;
|
||||
import com.mogo.service.statusmanager.StatusDescriptor;
|
||||
import com.zhjt.service.chain.ChainLog;
|
||||
import com.zhjt.service.chain.TracingConstants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.exceptions.UndeliverableException;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.plugins.RxJavaPlugins;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import mogo.telematics.pad.MessagePad;
|
||||
|
||||
/**
|
||||
@@ -114,11 +108,13 @@ public class BusOrderModel {
|
||||
|
||||
private LoginService loginService;
|
||||
|
||||
private long prePassengerWriteOffTime = 0; //记录最后查询到乘客核销的时间
|
||||
|
||||
private final Handler handler = new Handler(new Handler.Callback() {
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
if ( msg.what == MSG_QUERY_BUS_STATION ) {
|
||||
BusOrderModel.getInstance().queryBusRoutes();
|
||||
queryBusRoutes();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -509,6 +505,7 @@ public class BusOrderModel {
|
||||
isGoingToNextStation = false;
|
||||
if (o.code == 0){ // 重置成功
|
||||
queryBusRoutes();
|
||||
startOrStopQueryPassengerWriteOff(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -588,12 +585,53 @@ public class BusOrderModel {
|
||||
*/
|
||||
public void queryPassengerWriteOff(){
|
||||
//todo 查询接口拿到核销乘客波播报
|
||||
if ( backgroundCurrentStationIndex > stationList.size() - 1 ) { //到站短时间内调用多次
|
||||
CallerLogger.INSTANCE.e( M_BUS + TAG, "数组越界" );
|
||||
return;
|
||||
}
|
||||
BusServiceManager.getInstance().queryStationWriteOffPassengers(mContext,
|
||||
currentTaskId,
|
||||
stationList.get(backgroundCurrentStationIndex).getSiteId(),
|
||||
prePassengerWriteOffTime,
|
||||
new IBusServiceCallback<BusQueryWriteOffPassengersResponse>(){
|
||||
|
||||
@Override
|
||||
public void onSuccess(BusQueryWriteOffPassengersResponse o) {
|
||||
if (o.code !=0 || o.data == null || o.data.isEmpty()){
|
||||
return;
|
||||
}
|
||||
List<WriteOffPassenger> passengers = o.data;
|
||||
playWriteOffPassengersMsg(passengers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFail(String failMsg) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void playWriteOffPassengersMsg(List<WriteOffPassenger> passengers) {
|
||||
for (int i=0; i< passengers.size();i++){
|
||||
WriteOffPassenger passenger = passengers.get(i);
|
||||
CallerLogger.INSTANCE.e( M_BUS + TAG, "size = " + passenger.passengerSize+
|
||||
" time = "+passenger.writeOffTime);
|
||||
prePassengerWriteOffTime = passenger.writeOffTime;
|
||||
int passengerNum = passenger.passengerSize;
|
||||
if (passengerNum > 1){ //多人播报 "x人"
|
||||
AIAssist.getInstance(mContext).speakTTSVoice(passengerNum + "人");
|
||||
}else { //是 1 播放 "滴"
|
||||
SoundPoolHelper.getSoundPoolHelper().playSoundWithRedId(mContext, R.raw.bus_di);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startOrStopQueryPassengerWriteOff(boolean isStart){
|
||||
if (isStart){
|
||||
BusModelLoopManager.getInstance().startQueryPassengerWriteOffLoop();
|
||||
}else {
|
||||
prePassengerWriteOffTime = 0;
|
||||
SoundPoolHelper.getSoundPoolHelper().releaseSoundPool();
|
||||
BusModelLoopManager.getInstance().stopQueryPassengerWriteOffLoop();
|
||||
}
|
||||
}
|
||||
@@ -601,15 +639,13 @@ public class BusOrderModel {
|
||||
/**
|
||||
* 到站后重置站点状态
|
||||
*/
|
||||
private void arriveSiteStation(boolean isRestart) {
|
||||
private void arriveSiteStation() {
|
||||
if ( backgroundCurrentStationIndex +1 > stationList.size() - 1 ) { //到站短时间内调用多次
|
||||
CallerLogger.INSTANCE.e( M_BUS + TAG, "数组越界" );
|
||||
return;
|
||||
}
|
||||
int arrivedStationIndex = 0;
|
||||
if (!isRestart){
|
||||
arrivedStationIndex = backgroundCurrentStationIndex + 1;
|
||||
}
|
||||
int arrivedStationIndex = backgroundCurrentStationIndex + 1;
|
||||
|
||||
CallerLogger.INSTANCE.d(M_BUS + TAG,"arriveSiteStation-currentStationIndex = "+ arrivedStationIndex);
|
||||
|
||||
BusServiceManager.getInstance().arriveSiteStation(mContext ,
|
||||
@@ -620,11 +656,9 @@ public class BusOrderModel {
|
||||
@Override
|
||||
public void onSuccess(BaseData o) {
|
||||
CallerLogger.INSTANCE.d(M_BUS + TAG,"arriveSiteStation success");
|
||||
if (!isRestart){
|
||||
queryBusRoutes();
|
||||
//todo 开始轮询到站核销的乘客
|
||||
startOrStopQueryPassengerWriteOff(true);
|
||||
}
|
||||
queryBusRoutes();
|
||||
//5s轮询核销乘客
|
||||
startOrStopQueryPassengerWriteOff(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -668,20 +702,6 @@ public class BusOrderModel {
|
||||
});
|
||||
}
|
||||
|
||||
private void arrivedBusStationSuccess() {
|
||||
List<BusStationBean> site = busRoutesResult.getSites();
|
||||
if (site != null && site.size() > 0){
|
||||
backgroundCurrentStationIndex ++;
|
||||
if (refreshBusStationsCallback != null){
|
||||
String lineTime = DateTimeUtil.formatLongToString(
|
||||
busRoutesResult.getTaskTime(),
|
||||
DateTimeUtil.TAXI_HH_mm);
|
||||
refreshBusStationsCallback.updateBusTaskStatus(busRoutesResult.getName(),lineTime,
|
||||
stationList, backgroundCurrentStationIndex ,true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 查询到站下车乘客
|
||||
// */
|
||||
@@ -763,18 +783,16 @@ public class BusOrderModel {
|
||||
}
|
||||
}
|
||||
|
||||
if (backgroundCurrentStationIndex == 0){ //默认是第一站到站查询
|
||||
startOrStopQueryPassengerWriteOff(true);
|
||||
}
|
||||
|
||||
BusStationBean currentStation = stationList.get(backgroundCurrentStationIndex);
|
||||
|
||||
CallerLogger.INSTANCE.d( M_BUS + TAG,
|
||||
"渲染站点信息服务端currentStationIndex="+backgroundCurrentStationIndex
|
||||
+" isLeaving()="+currentStation.isLeaving());
|
||||
|
||||
//当前站点是始发站,告诉服务端到达始发站。 如果没有这个节点, 服务器不知道始发站到达状态
|
||||
// ,订单开始站下在始发站的状态流转有问题 2022.9.23不需要再调用此接口,拿到任务默认就是首站
|
||||
// if (backgroundCurrentStationIndex == 0 && !currentStation.isLeaving()){
|
||||
// arriveSiteStation(true);
|
||||
// }
|
||||
|
||||
// 美化是否开始
|
||||
if (FunctionBuildConfig.isDemoMode && (backgroundCurrentStationIndex >= 0
|
||||
&& backgroundCurrentStationIndex <= stationList.size()-1)
|
||||
@@ -923,18 +941,7 @@ public class BusOrderModel {
|
||||
CallerLogger.INSTANCE.d( M_BUS + TAG, "到站====currentStationIndex=" + backgroundCurrentStationIndex);
|
||||
isGoingToNextStation = false;
|
||||
|
||||
arriveSiteStation(false);
|
||||
|
||||
// queryStationLeaveAwayPassengers();// 2.11.0不再播报乘客下车
|
||||
// UiThreadHandler.postDelayed(new Runnable() {// 先查询下车乘客,再上报到站,便于后台筛查下车订单
|
||||
// @Override
|
||||
// public void run() {
|
||||
// if (!hadQueryLeaveAwayPassager){
|
||||
// arriveSiteStation(false);
|
||||
// }
|
||||
// hadQueryLeaveAwayPassager = false;
|
||||
// }
|
||||
// },1500);
|
||||
arriveSiteStation();
|
||||
}
|
||||
|
||||
public boolean isGoingToNextStation() {
|
||||
|
||||
@@ -13,8 +13,10 @@ import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
|
||||
import com.mogo.och.bus.bean.BusCloseTaskRequest;
|
||||
import com.mogo.och.bus.bean.BusQueryLineTaskResponse;
|
||||
import com.mogo.och.bus.bean.BusQueryLinesResponse;
|
||||
import com.mogo.och.bus.bean.BusQueryWriteOffPassengersResponse;
|
||||
import com.mogo.och.bus.bean.BusRoutePlanningUpdateReqBean;
|
||||
import com.mogo.och.bus.bean.BusRoutesResponse;
|
||||
import com.mogo.och.bus.bean.BusWriteOffPassengersQueryRequest;
|
||||
import com.mogo.och.bus.bean.CarHeartbeatReqBean;
|
||||
import com.mogo.och.bus.bean.BusOrdersResponse;
|
||||
import com.mogo.och.bus.bean.BusQueryLineStationsRequest;
|
||||
@@ -157,6 +159,22 @@ public class BusServiceManager {
|
||||
.subscribe(getSubscribeImpl(context,callback,"queryStationLeaveAwayPassengers"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前到站点核销乘客
|
||||
* @param context
|
||||
* @param siteId
|
||||
* @param callback
|
||||
*/
|
||||
public void queryStationWriteOffPassengers(Context context,int taskId,int siteId,long prePassengerTime
|
||||
, IBusServiceCallback<BusQueryWriteOffPassengersResponse> callback){
|
||||
mService.queryWriteOffPassengers( MoGoAiCloudClientConfig.getInstance().getServiceAppId()
|
||||
,MoGoAiCloudClientConfig.getInstance().getToken()
|
||||
,new BusWriteOffPassengersQueryRequest(taskId,siteId,prePassengerTime))
|
||||
.subscribeOn( Schedulers.io() )
|
||||
.observeOn( AndroidSchedulers.mainThread() )
|
||||
.subscribe(getSubscribeImpl(context,callback,"queryStationWriteOffPassengers"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.mogo.och.bus.net;
|
||||
import com.mogo.eagle.core.data.BaseData;
|
||||
import com.mogo.och.bus.bean.BusCloseTaskRequest;
|
||||
import com.mogo.och.bus.bean.BusOperationStatusRequest;
|
||||
import com.mogo.och.bus.bean.BusOrdersResponse;
|
||||
import com.mogo.och.bus.bean.BusQueryWriteOffPassengersResponse;
|
||||
import com.mogo.och.bus.bean.BusWriteOffPassengersQueryRequest;
|
||||
import com.mogo.och.bus.bean.BusQueryLineTaskResponse;
|
||||
import com.mogo.och.bus.bean.BusQueryLinesResponse;
|
||||
import com.mogo.och.bus.bean.BusRoutePlanningUpdateReqBean;
|
||||
@@ -128,10 +129,6 @@ public interface IBusApiService {
|
||||
@POST( "/autopilot-car-hailing/location/v2/driver/bus/saveLineCoordinate" )
|
||||
Observable<BaseData> updateOrderRoute(@Header ("appId") String appId, @Header("ticket") String ticket, @Body BusRoutePlanningUpdateReqBean data);
|
||||
|
||||
// @Headers( {"Content-type:application/json;charset=UTF-8"} )
|
||||
// @POST("/autopilot-car-hailing/operation/v1/driver/bus/line/resetStart")
|
||||
// Observable<BusRoutesResponse> resetCurrentLineStatus(@Header ("appId") String appId, @Header("ticket") String ticket, @Body BusResetLineStatusRequest data);
|
||||
|
||||
/**
|
||||
* 中止任务
|
||||
* @param appId
|
||||
@@ -152,5 +149,16 @@ public interface IBusApiService {
|
||||
@Headers( {"Content-type:application/json;charset=UTF-8"} )
|
||||
@POST("/autopilot-car-hailing/cab/flow/v1/bus/driver/bus/endTask")
|
||||
Observable<BaseData> endTask(@Header ("appId") String appId, @Header("ticket") String ticket, @Body BusCloseTaskRequest data);
|
||||
|
||||
/**
|
||||
* 任务正常跑完结束
|
||||
* @param appId
|
||||
* @param ticket
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
@Headers( {"Content-type:application/json;charset=UTF-8"} )
|
||||
@POST("/autopilot-car-hailing/operation/v1/driver/order/verificationResult")
|
||||
Observable<BusQueryWriteOffPassengersResponse> queryWriteOffPassengers(@Header ("appId") String appId, @Header("ticket") String ticket, @Body BusWriteOffPassengersQueryRequest data);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.mogo.och.bus.presenter;
|
||||
|
||||
import static com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.M_BUS;
|
||||
import static com.mogo.och.bus.constant.BusConst.LOOP_DELAY;
|
||||
import static com.mogo.och.bus.constant.BusConst.LOOP_PASSENGER_2S;
|
||||
import static com.mogo.och.bus.constant.BusConst.LOOP_DELAY_500;
|
||||
import static com.mogo.och.bus.constant.BusConst.LOOP_PASSENGER_5S;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
|
||||
import com.mogo.och.bus.constant.BusConst;
|
||||
@@ -40,8 +40,8 @@ public class BusModelLoopManager {
|
||||
return;
|
||||
}
|
||||
CallerLogger.INSTANCE.i(M_BUS + TAG, "startQueryPassengerWriteOffLoop()");
|
||||
mQueryPassengerDisposable = Observable.interval(LOOP_DELAY,
|
||||
LOOP_PASSENGER_2S, TimeUnit.MILLISECONDS)
|
||||
mQueryPassengerDisposable = Observable.interval(LOOP_DELAY_500,
|
||||
LOOP_PASSENGER_5S, TimeUnit.MILLISECONDS)
|
||||
.map((aLong -> aLong + 1))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
BIN
OCH/mogo-och-bus/src/main/res/raw/bus_di.mp3
Normal file
BIN
OCH/mogo-och-bus/src/main/res/raw/bus_di.mp3
Normal file
Binary file not shown.
@@ -0,0 +1,118 @@
|
||||
package com.mogo.och.common.module.utils;
|
||||
|
||||
import android.media.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.app.*;
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2022/9/23
|
||||
*
|
||||
*/
|
||||
public class SoundPoolHelper {
|
||||
private SoundPool mSoundPool;
|
||||
private AudioManager mAudioManager;
|
||||
private float volume;
|
||||
// Maximumn sound stream.
|
||||
private static final int MAX_STREAMS = 5;
|
||||
// Stream type.
|
||||
private static final int streamType = AudioManager.STREAM_MUSIC;
|
||||
private int mSoundId;
|
||||
private int resId;
|
||||
private Context mContext;
|
||||
|
||||
private volatile static SoundPoolHelper INSTANCE;
|
||||
|
||||
public static SoundPoolHelper getSoundPoolHelper(){
|
||||
if (INSTANCE == null){
|
||||
synchronized (SoundPoolHelper.class){
|
||||
if (INSTANCE == null){
|
||||
INSTANCE = new SoundPoolHelper();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public SoundPoolHelper(){}
|
||||
|
||||
//播放资源文件
|
||||
public void playSoundWithRedId(Context context,int resId){
|
||||
this.mContext = context;
|
||||
this.resId=resId;
|
||||
init();
|
||||
}
|
||||
|
||||
//init settings
|
||||
private void init(){
|
||||
// AudioManager audio settings for adjusting the volume
|
||||
mAudioManager = (AudioManager)this.mContext. getSystemService(Context.AUDIO_SERVICE);
|
||||
|
||||
// Current volumn Index of particular stream type.
|
||||
float currentVolumeIndex = (float) mAudioManager.getStreamVolume(streamType);
|
||||
|
||||
// Get the maximum volume index for a particular stream type.
|
||||
float maxVolumeIndex = (float) mAudioManager.getStreamMaxVolume(streamType);
|
||||
|
||||
// Volumn (0 --> 1)
|
||||
this.volume = currentVolumeIndex / maxVolumeIndex;
|
||||
|
||||
// Suggests an audio stream whose volume should be changed by
|
||||
// the hardware volume controls.
|
||||
((Activity)this.mContext).setVolumeControlStream(streamType);
|
||||
|
||||
if (mSoundPool == null){
|
||||
// For Android SDK >= 21
|
||||
if (Build.VERSION.SDK_INT >= 21 ) {
|
||||
|
||||
AudioAttributes audioAttrib = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_GAME)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build();
|
||||
|
||||
SoundPool.Builder builder= new SoundPool.Builder();
|
||||
builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);
|
||||
|
||||
this.mSoundPool = builder.build();
|
||||
} else {// for Android SDK < 21
|
||||
// SoundPool(int maxStreams, int streamType, int srcQuality)
|
||||
this.mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// When Sound Pool load complete.
|
||||
this.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
|
||||
@Override
|
||||
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
|
||||
playSound();
|
||||
}
|
||||
});
|
||||
|
||||
//load res
|
||||
this.mSoundId =this.mSoundPool.load(this.mContext,this.resId,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
public void releaseSoundPool() {
|
||||
if (mSoundPool != null) {
|
||||
mSoundPool.autoPause();
|
||||
mSoundPool.unload(mSoundId);
|
||||
mSoundPool.release();
|
||||
mSoundPool = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//play the sound res
|
||||
private void playSound(){
|
||||
float leftVolumn = volume;
|
||||
float rightVolumn = volume;
|
||||
// Play sound of gunfire. Returns the ID of the new stream.
|
||||
int streamId = this.mSoundPool.play(this.mSoundId,leftVolumn, rightVolumn, 1, 0, 1f);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user