这次我们需要接入视频播放器的jar包
//视频播放
api('com.shuyu:GSYVideoPlayer:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
api('com.shuyu:gsyVideoPlayer-java:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
api('com.shuyu:GSYVideoPlayer-exo2:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
在activity_send_message.xml添加发送视频的按钮 ,以及显示视频
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/deepskyblue"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_return"
android:layout_width="44sp"
android:layout_height="44sp"
android:gravity="center_vertical">
<ImageView
android:layout_width="24sp"
android:layout_height="24sp"
android:layout_marginStart="20dp"
android:contentDescription="@string/tv_icon_des"
android:src="@drawable/return1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="@string/btn_send_message"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/ed_send_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_send_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_send_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消息接收显示" />
<TextView
android:id="@+id/tv_receive_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_album" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片接收显示" />
<ImageView
android:id="@+id/iv_receive_message"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_video" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="视频接收显示" />
<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
在SendMessageActivity添加按钮的注册以及播放视频事件
package heath.com.chat.message;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import java.io.File;
import heath.com.chat.R;
import heath.com.chat.service.IMService;
import heath.com.chat.utils.RealPathFromUriUtils;
import heath.com.chat.utils.ToastUtil;
public class SendMessageActivity extends AppCompatActivity implements View.OnClickListener {
private static IMService mImService;
private LinearLayout mLlReturn;
private EditText mEdSendText;
//调用系统相册-选择图片
private static final int IMAGE = 1;
/**
* 发消息
*/
private Button mBtnSendText;
private static TextView mTvReceiveMessage;
private Button mBtnAlbum;
private Button mBtnVideo;
private static ImageView mIvReceiveMessage;
private boolean video = false;//false为图片,true为视频
private static StandardGSYVideoPlayer videoPlayer;
private static OrientationUtils orientationUtils;
private static Context context;
private static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_message);
initView();
init();
}
private void init() {
// 绑定服务
Intent service = new Intent(SendMessageActivity.this, IMService.class);
bindService(service, mMyServiceConnection, BIND_AUTO_CREATE);
context = this;
activity = this;
}
@Override
public void onDestroy() {
super.onDestroy();
// 解绑服务
if (mMyServiceConnection != null) {
unbindService(mMyServiceConnection);
}
GSYVideoManager.releaseAllVideos();
if (orientationUtils != null)
orientationUtils.releaseListener();
}
MyServiceConnection mMyServiceConnection = new MyServiceConnection();
private void initView() {
mLlReturn = (LinearLayout) findViewById(R.id.ll_return);
mEdSendText = (EditText) findViewById(R.id.ed_send_text);
mBtnSendText = (Button) findViewById(R.id.btn_send_text);
mBtnSendText.setOnClickListener(this);
mTvReceiveMessage = (TextView) findViewById(R.id.tv_receive_message);
mBtnAlbum = (Button) findViewById(R.id.btn_album);
mBtnVideo = (Button) findViewById(R.id.btn_video);
mIvReceiveMessage = (ImageView) findViewById(R.id.iv_receive_message);
videoPlayer = this.findViewById(R.id.video_player);
mBtnAlbum.setOnClickListener(this);
mBtnVideo.setOnClickListener(this);
mIvReceiveMessage.setVisibility(View.GONE);
videoPlayer.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
default:
break;
case R.id.ll_return:
finish();
break;
case R.id.btn_album:
intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
video = false;
startActivityForResult(intent, IMAGE);
break;
case R.id.btn_video:
intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("video/*");
video = true;
startActivityForResult(intent, IMAGE);
break;
case R.id.btn_send_text:
final String content = mEdSendText.getText().toString();//消息文本
String account = "1";//目前这里是写死的账号
SessionTypeEnum type = SessionTypeEnum.P2P;//会话类型
final IMMessage textMessage = MessageBuilder.createTextMessage(account, type, content);
NIMClient.getService(MsgService.class).sendMessage(textMessage, false).setCallback(new RequestCallback<Void>() {
@Override
public void onSuccess(Void param) {
ToastUtil.toastOnUiThread(SendMessageActivity.this, "发送成功");
}
@Override
public void onFailed(int code) {
Log.e("文本发送失败", "onEvent: " + code);
}
@Override
public void onException(Throwable exception) {
Log.e("文本发送异常", "onEvent: " + exception);
}
});
mEdSendText.setText("");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片路径
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
//将uri转换为路径
String path = RealPathFromUriUtils.getRealPathFromUri(this, selectedImage);
File file = new File(path);
String account = "1";//目前这里是写死的账号
IMMessage message;
if (video) {
MediaPlayer mediaPlayer = null;
try {
mediaPlayer = MediaPlayer.create(this, Uri.fromFile(file));
} catch (Exception e) {
e.printStackTrace();
}
// 视频文件持续时间
final long duration = mediaPlayer == null ? 0 : mediaPlayer.getDuration();
// 视频高度
final int height = mediaPlayer == null ? 0 : mediaPlayer.getVideoHeight();
// 视频宽度
final int width = mediaPlayer == null ? 0 : mediaPlayer.getVideoWidth();
message = MessageBuilder.createVideoMessage(account, SessionTypeEnum.P2P, file, duration, width, height, null);
} else {
message = MessageBuilder.createImageMessage(account, SessionTypeEnum.P2P, file, file.getName());
}
NIMClient.getService(MsgService.class).sendMessage(message, false).setCallback(new RequestCallback<Void>() {
@Override
public void onSuccess(Void param) {
try {
ToastUtil.toastOnUiThread(SendMessageActivity.this, "发送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailed(int code) {
Log.e("图片发送失败", "onEvent: " + code);
}
@Override
public void onException(Throwable exception) {
exception.printStackTrace();
Log.e("图片发送异常", "onEvent: " + exception);
}
});
}
}
//收到文本消息更新界面
public static void updateData(String message) {
mTvReceiveMessage.setText(message);
}
//收到图片消息更新界面
public static void updateData1(String message) {
mIvReceiveMessage.setImageURI(Uri.parse(message));
mIvReceiveMessage.setVisibility(View.VISIBLE);
}
//收到视频消息更新界面
public static void updateData2(String message) {
videoPlayer.setVisibility(View.VISIBLE);
videoPlayer.setUp(message, true, "测试视频");
//增加封面
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
videoPlayer.setThumbImageView(imageView);
//增加title
videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
//设置返回键
videoPlayer.getBackButton().setVisibility(View.VISIBLE);
//设置旋转
orientationUtils = new OrientationUtils(activity, videoPlayer);
//设置全屏按键功能,这是使用的是选择屏幕,而不是全屏
videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orientationUtils.resolveByClick();
}
});
//是否可以滑动调整
videoPlayer.setIsTouchWiget(true);
//设置返回按键功能
videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
videoPlayer.getFullscreenButton().performClick();
return;
}
//释放所有
videoPlayer.setVideoAllCallBack(null);
}
});
videoPlayer.startPlayLogic();
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out
.println("--------------onServiceConnected--------------");
IMService.MyBinder binder = (IMService.MyBinder) service;
mImService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out
.println("--------------onServiceDisconnected--------------");
}
}
@Override
protected void onPause() {
super.onPause();
videoPlayer.onVideoPause();
}
@Override
protected void onResume() {
super.onResume();
videoPlayer.onVideoResume();
}
@Override
public void onBackPressed() {
//先返回正常状态
if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
videoPlayer.getFullscreenButton().performClick();
return;
}
//释放所有
videoPlayer.setVideoAllCallBack(null);
super.onBackPressed();
}
}
在IMService添加接收视频的消息处理
else if (message.getMsgType() == MsgTypeEnum.video) {
String path = ((VideoAttachment) message.getAttachment()).getUrl();
SendMessageActivity.updateData2(path);
}
项目下载地址:Android聊天软件开发(基于网易云IM即时通讯)——第三部分_安卓开发聊天-Android代码类资源-CSDN下载
接单
小编承接外包,有意者可加
QQ:1944300940
微信号:wxid_g8o2y9ninzpp12