addvoice
李富 9 years ago
parent 16e7c4ce27
commit 7869bd8bc9

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -82,7 +82,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
@ -100,8 +99,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-22.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />

@ -107,7 +107,7 @@
<!-- 设置bmob appkey --> <!-- 设置bmob appkey -->
<meta-data <meta-data
android:name="Bmob_APP_KEY" android:name="Bmob_APP_KEY"
android:value="44d76c5b8464f93f380b1083f049bb36" /> android:value="a5f4203810c22eb8bd31e2d17823618b" />
<!-- bmobim end --> <!-- bmobim end -->
<meta-data <meta-data
android:name="com.baidu.lbsapi.API_KEY" android:name="com.baidu.lbsapi.API_KEY"

@ -0,0 +1,180 @@
package cn.bmob.imdemo.adapter;
/**
* Created by lifu on 16/6/13.
*/
import java.io.File;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import cn.bmob.v3.BmobUser;
/**
* --使
* ps:
* @ClassName: BmobVoiceManager
* @Description: TODO
* @author smile
* @date 2014-6-30 11:28:34
*/
public class BmobPlayManager implements OnCompletionListener{
private MediaPlayer mMediaPlayer;
Context context;
// 创建private static类实例
private volatile static BmobPlayManager INSTANCE;
// 同步锁
private static Object INSTANCE_LOCK = new Object();
private boolean isPlaying = false;
/**
* 使--
*/
public static BmobPlayManager getInstance(Context context) {
if (INSTANCE == null)
synchronized (INSTANCE_LOCK) {
if (INSTANCE == null) {
INSTANCE = new BmobPlayManager();
}
INSTANCE.init(context);
}
return INSTANCE;
}
/**
*
* @Title: init
* @Description: TODO
* @param c
* @return
* @throws
*/
public void init(Context c) {
this.context = c;
// 初始化用户管理
}
public void playRecording(String fileName,boolean isUserSpeaker) {
// TODO Auto-generated method stub
File file = new File(fileName);
if(file==null || !file.exists()){
return;
}
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener(new PlayerErrorListener());
} else {
mMediaPlayer.stop();
mMediaPlayer.reset();
}
//开启外放模式
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if (isUserSpeaker){//设置是否使用扬声器,
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
}else{//关闭扬声器--启用耳机听筒模式
audioManager.setSpeakerphoneOn(false);
audioManager.setMode(AudioManager.MODE_IN_CALL);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
}
try {
mMediaPlayer.setDataSource(fileName);
mMediaPlayer.prepare();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
isPlaying = true;
} catch (IOException e) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public void stopPlayback() {
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
onStop();
}
}
public boolean isPlaying() {
// TODO Auto-generated method stub
return isPlaying;
}
public int getPlaybackDuration() {
// TODO Auto-generated method stub
int duration = 0;
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
duration = mMediaPlayer.getDuration();
}
return duration;
}
public MediaPlayer getMediaPlayer() {
// TODO Auto-generated method stub
return mMediaPlayer;
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
onStop();
}
/**
* @Title: onStop
* @Description: TODO
* @param
* @return void
* @throws
*/
private void onStop(){
//用于语音播放完成之后的界面更新
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
isPlaying = false;
}
/**
*
* @ClassName: PlayerErrorListener
* @Description: TODO
* @author smile
* @date 2014-6-30 8:09:56
*/
@SuppressLint("DefaultLocale")
public class PlayerErrorListener implements android.media.MediaPlayer.OnErrorListener {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String whatDescription = "";
switch (what) {
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
whatDescription = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
whatDescription = "MEDIA_ERROR_SERVER_DIED";
break;
default:
whatDescription = Integer.toString(what);
break;
}
return false;
}
}
}

@ -6,6 +6,7 @@ import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.Bundle; import android.os.Bundle;
@ -39,7 +40,10 @@ import com.baidu.mapapi.utils.DistanceUtil;
import com.orhanobut.logger.Logger; import com.orhanobut.logger.Logger;
import java.io.File; import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -48,6 +52,7 @@ import java.util.Map;
import butterknife.Bind; import butterknife.Bind;
import cn.bmob.imdemo.R; import cn.bmob.imdemo.R;
import cn.bmob.imdemo.adapter.BmobPlayManager;
import cn.bmob.imdemo.adapter.ChatAdapter; import cn.bmob.imdemo.adapter.ChatAdapter;
import cn.bmob.imdemo.base.ParentWithNaviActivity; import cn.bmob.imdemo.base.ParentWithNaviActivity;
import cn.bmob.imdemo.util.Util; import cn.bmob.imdemo.util.Util;
@ -66,6 +71,7 @@ import cn.bmob.newim.listener.MessageListHandler;
import cn.bmob.newim.listener.MessageSendListener; import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.newim.listener.ObseverListener; import cn.bmob.newim.listener.ObseverListener;
import cn.bmob.newim.listener.OnRecordChangeListener; import cn.bmob.newim.listener.OnRecordChangeListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.exception.BmobException;
public class LocActivity extends ParentWithNaviActivity implements ObseverListener,MessageListHandler { public class LocActivity extends ParentWithNaviActivity implements ObseverListener,MessageListHandler {
@ -76,6 +82,7 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
BmobIMAudioMessage audio_msg; BmobIMAudioMessage audio_msg;
MediaPlayer mediaPlayer = null; MediaPlayer mediaPlayer = null;
Context mContext; Context mContext;
String currentObjectId = "";
@Bind(R.id.btn_speak) @Bind(R.id.btn_speak)
@ -109,6 +116,12 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
mLocationClient.registerLocationListener(myListener); mLocationClient.registerLocationListener(myListener);
mContext = getApplicationContext(); mContext = getApplicationContext();
mediaPlayer = new MediaPlayer();
try {
currentObjectId = BmobUser.getCurrentUser(mContext).getObjectId();
} catch (Exception e) {
e.printStackTrace();
}
setContentView(R.layout.activity_loc); setContentView(R.layout.activity_loc);
c = BmobIMConversation.obtain(BmobIMClient.getInstance(), (BmobIMConversation) getBundle().getSerializable("c")); c = BmobIMConversation.obtain(BmobIMClient.getInstance(), (BmobIMConversation) getBundle().getSerializable("c"));
@ -532,17 +545,30 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
} }
else if ( msg.getMsgType().equals("sound")){ else if ( msg.getMsgType().equals("sound")){
audio_msg = BmobIMAudioMessage.buildFromDB(true,msg); audio_msg = BmobIMAudioMessage.buildFromDB(true,msg);
//if (audio_msg.getFromId().equals(currentObjectId)) {// 如果是自己发送的语音消息,则播放本地地址
// String localPath = audio_msg.getContent().split("&")[0];
//startPlayRecord(localPath, true);
//} else {// 如果是收到的消息,则需要先下载后播放
String localPath = BmobDownloadManager.getDownLoadFilePath(audio_msg); String localPath = BmobDownloadManager.getDownLoadFilePath(audio_msg);
startPlayRecord(localPath, true); startPlayRecord(localPath, true);
//}
} }
} }
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
public void startPlayRecord(String filePath, boolean isUseSpeaker) { public void startPlayRecord(String filePath, boolean isUseSpeaker) {
if (!(new File(filePath).exists())) { if (!(new File(filePath).exists())) {
return; return;
} }
Logger.i("start play Record "); Logger.i("start play Record " + filePath);
BmobPlayManager playmanager = new BmobPlayManager();
playmanager.init(mContext);
playmanager.playRecording(filePath, isUseSpeaker);
/* AudioManager audioManager = (AudioManager) mContext /* AudioManager audioManager = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE); .getSystemService(Context.AUDIO_SERVICE);
@ -563,15 +589,12 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
// mediaPlayer.setDataSource(filePath); // mediaPlayer.setDataSource(filePath);
// 因此采用此方式会避免这种错误 // 因此采用此方式会避免这种错误
FileInputStream fis = new FileInputStream(new File(filePath)); FileInputStream fis = new FileInputStream(new File(filePath));
//mediaPlayer.setDataSource(fis.getFD()); mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare(); mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override @Override
public void onPrepared(MediaPlayer arg0) { public void onPrepared(MediaPlayer arg0) {
arg0.start(); arg0.start();
} }
}); });
@ -587,6 +610,7 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}*/ }*/
} }
public void stopPlayRecord() { public void stopPlayRecord() {
if (mediaPlayer != null) { if (mediaPlayer != null) {
@ -603,6 +627,7 @@ public class LocActivity extends ParentWithNaviActivity implements ObseverListen
super.onDestroy(); super.onDestroy();
mLocationClient.unRegisterLocationListener( myListener ); mLocationClient.unRegisterLocationListener( myListener );
mLocationClient.stop(); mLocationClient.stop();
// 当不需要定位图层时关闭定位图层 // 当不需要定位图层时关闭定位图层
mBaiduMap.setMyLocationEnabled(false); mBaiduMap.setMyLocationEnabled(false);
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理

Binary file not shown.
Loading…
Cancel
Save