一、接入前准备
- 下载ShareREC SDK地址 :下载Android_unity3d和iOS_unity3d
- android集成文档 ---- ios集成文档
二、Android端
将下载的ShareRec-xxx-for-Unity.unitypackage导入工程中,将ShareREC组件加入场景中,配置参数如下(Key和Secret填入在mob注册应用的信息):
之后按照文档只需调用相应的开启录制结束录制方法即可以实现录制
// 判断是否支持录制,在启动录制之前判断(在虚拟机上不可用一直false)
ShareREC.IsAvailable();
// 启动录制
ShareREC.StartRecorder();
// 暂停录制
ShareREC.PauseRecorder();
// 恢复录制
ShareREC.ResumeRecorder();
// 停止录制
ShareREC.StopRecorder();
2.1 接入中遇到的问题
- 按照文档的配置完成后,在真机上测试IsAvailable()一直返回false,shareREC的demo工程可以运行,这种情况下,修改打包选项playerSeeting中的设置,下面是配置项:
重要的是矩形框里的设置,Multuthreaded Rendering、 Graphics Jobs、 Protect Graphics Memory设置错误在录制结束时会闪退
在plugins/Android/ShareREC中的AndroidManifest文件中<application>节点下加上秘钥信息:<meta-data android:name="Mob-AppKey" android:value="xxx"/> <meta-data android:name="Mob-AppSecret" android:value="xxx"/>
- 在固定屏幕方向为横屏时,通过ShareREC.ShowVideoCenter()打开视频中心播放视频程序会崩溃,竖屏下不会,没找到shareRec具体原因,只得通过再打开shareREC的页面时候改变屏幕方向,在页面返回的回调
Screen.orientation = ScreenOrientation.Portrait;
三、加入shareSDK分享
下载shareSDK导入,有几点需要注意
-
在导入shareREC的时候的时候Plugins/Android/shareRec下和ShareSDK重复的jar包需要删除,打包过程中AndroidManifest文件中冲突的部分也要删除
shareSdk中的一个jar包需要在android环境下重新生成jar替换,清单文件中包名、key信息也要改,以后会整理一篇详细说明
分享模块配置完成后,在录制完成后调用ShareREC.ShowShare ()如果分享的结果是一张图片,检查清单文件中是否正确加入mob的key信息(2.1),同时在shareRec组件挂载的物体上加上shareSDK组件,组件上的ByPass Approval设置为false,表示不绕过微信的包名签名验证,同时正确填入申请的微信的key信息,测试时把生成的包按照微信开发平台填写的包进行签名,因为分享视频的时候不能绕过验证 附:不同平台分享内容的详细说明
四、接入iOS ShareREC
-
导入在导入ios的shareREC包时千万不要直接覆盖,否则会把android的ShareREC文件覆盖(ios和android的ShareREC脚本不同),如果导入前已经导入过ShareSDK包,要取消勾选MiniJson(shareSDK中有),Editor下的sdk目录不勾选是因为编译成ios工程时在Xcode打开库工程并未加入,还需手动导入,所以在unity中就不加入了;
- 没有导入ios的ShareREC.cs是因为会覆盖,更名后复制内容加入工程,让人想吐槽的是这个文件有两处还是错误的,更正后如下。这个脚本也需要挂载在场景中iOS才能分享
using UnityEngine;
using System.Collections;
using System;
namespace com.mob
{
/// <summary>
/// Finished record event.
/// </summary>
public delegate void FinishedRecordEvent (Exception ex);
/// <summary>
/// Close event.
/// </summary>
public delegate void CloseEvent ();
/// <summary>
/// Edit result event.
/// </summary>
public delegate void EditResultEvent (bool cancelled);
/// <summary>
///Custom platform event
/// </summary>
public delegate void CustomPlatformEvent(string platformName, string title, Hashtable recording);
public delegate void ShareEvent (SocialShareState state);
public delegate void ConfirmEditLastRecordingResultEvent (string mainVideoPath);
public enum SocialPageType
{
Share = 0, // share
ViewCenter = 1, // video center
Profile = 2 // profile
}
public enum SocialShareState
{
ShareStateBegin = 0,
ShareStateSuccess = 1,
ShareStateFail = 2,
ShareStateCancel = 3
}
/// <summary>
/// Share rec.
/// </summary>
public class ShareRecIOs : MonoBehaviour
{
/// <summary>
/// _callback the specified data.
/// </summary>
/// <param name="data">Data.</param>
private void shareRECCallback (string data)
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
#if UNITY_IPHONE
ShareRECIOS.shareRECCallback(data);
#endif
}
else if (Application.platform == RuntimePlatform.Android)
{
#if UNITY_ANDROID
#endif
}
}
/// <summary>
/// Sets the name of the callback object.
/// </summary>
/// <param name="objectName">Object name.</param>
public static void setCallbackObjectName(string objectName)
{
if (objectName == null)
{
objectName = "Main Camera";
}
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
#if UNITY_IPHONE
ShareRECIOS.setCallbackObjectName(objectName);
#endif
}
else if (Application.platform == RuntimePlatform.Android)
{
#if UNITY_ANDROID
#endif
}
}
/// <summary>
/// Pause the recoring.
/// </summary>
public static void pauseRecoring ()
{
#if UNITY_IPHONE
ShareRECIOS.pauseRecoring();
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Resume the recoring.
/// </summary>
public static void resumeRecoring ()
{
#if UNITY_IPHONE
ShareRECIOS.resumeRecoring();
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Starts the recoring.
/// </summary>
public static void startRecoring ()
{
#if UNITY_IPHONE
ShareRECIOS.startRecording();
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Stops the recording.
/// </summary>
public static void stopRecording (FinishedRecordEvent evt)
{
#if UNITY_IPHONE
ShareRECIOS.stopRecording(evt);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Plaies the last recording.
/// </summary>
public static void playLastRecording()
{
#if UNITY_IPHONE
ShareRECIOS.playLastRecording();
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Sets the bit rate.
/// </summary>
/// <param name="bitRate">Bit rate.</param>
public static void setBitRate(int bitRate)
{
#if UNITY_IPHONE
ShareRECIOS.setBitRate(bitRate);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Sets the FPS.
/// </summary>
/// <param name="fps">Fps.</param>
public static void setFPS(int fps)
{
#if UNITY_IPHONE
ShareRECIOS.setFPS(fps);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Sets the minimum recording time.
/// </summary>
/// <param name="time">Time.</param>
public static void setMinimumRecordingTime(float time)
{
#if UNITY_IPHONE
ShareRECIOS.setMinimumRecordingTime(time);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Lasts the recording path.
/// </summary>
/// <returns>The recording path.</returns>
public static string lastRecordingPath()
{
#if UNITY_IPHONE
return ShareRECIOS.lastRecordingPath();
#elif UNITY_ANDROID
#endif
return null;
}
/// <summary>
/// Edits the lasting recording.
/// </summary>
/// <param name="title">Title.</param>
/// <param name="userData">User data.</param>
/// <param name="evt">Evt.</param>
public static void editLastingRecording(string title, Hashtable userData, CloseEvent evt)
{
#if UNITY_IPHONE
ShareRECIOS.editLastRecording(title, userData, evt);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Edits the last recording.
/// </summary>
/// <param name="evt">Evt.</param>
public static void editLastRecording(EditResultEvent evt)
{
#if UNITY_IPHONE
ShareRECIOS.editLastRecording(evt);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Sets the sync audio comment.
/// </summary>
/// <param name="flag">If set to <c>true</c> flag.</param>
public static void setSyncAudioComment(bool flag)
{
#if UNITY_IPHONE
ShareRECIOS.setSyncAudioComment(flag);
#elif UNITY_ANDROID
#endif
}
/// <summary>
/// Opens the social.
/// </summary>
/// <param name="title">Title.</param>
/// <param name="userData">User data.</param>
/// <param name="pageType">Page type.</param>
/// <param name="evt">Evt.</param>
public static void openSocial(string title, Hashtable userData, SocialPageType pageType, CloseEvent evt)
{
#if UNITY_IPHONE
ShareRECIOS.openSocial(title, userData, pageType, evt);
#elif UNITY_ANDROID
#endif
}
public static void addCustomPlatform(string platformName, CustomPlatformEvent evt)
{
#if UNITY_IPHONE
// ShareRECIOS.addCustomPlatform();
#elif UNITY_ANDROID
#endif
}
public static void closeSocial()
{
//#if UNITY_IPHONE
// ShareRECIOS.
//#elif #elif UNITY_ANDROID
// #endif
}
public static void setShareAfterUploadCompleted(bool flag)
{
#if UNITY_IPHONE
ShareRECIOS.setShareAfterUploadCompleted(flag);
#elif UNITY_ANDROID
#endif
}
public static void openSocialWithShareResult (string title, Hashtable userData, SocialPageType pageType, CloseEvent evt, ShareEvent shareEvt)
{
#if UNITY_IPHONE
ShareRECIOS.openSocialWithShareResult(title, userData, pageType, evt, shareEvt);
#elif UNITY_ANDROID
#endif
}
public static void confirmEditLastRecording(ConfirmEditLastRecordingResultEvent evt)
{
#if UNITY_IPHONE
ShareRECIOS.confirmEditLastRecording(evt);
#elif UNITY_ANDROID
#endif
}
}
}
修改了文件名,对应的Plugins/iOS/ShareREC/Editor/ShareRECConfigEditor.cs文件中的对原来文件名的引用处也要改为修改后的[CustomEditor(typeof(ShareRecIOs))]
- 在调用录制方法处通过判断平台进行录制,以下脚本我实现了简单的ios和android的录制,有需要可以参考
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using cn.sharerec;
using System;
using com.mob;
/// <summary>
/// 录制视频和分享的主要脚本.
/// </summary>
public class RecoderController : MonoBehaviour {
private bool isrecing = false;
public GameObject luzhiBtn; // 录制显示的按钮
private Text timeText; // 显示录制时间的文本
float duration = 10f;
// Use this for initialization
void Start () {
timeText = luzhiBtn.transform.Find ("Text").GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
if (isrecing) {
duration -= Time.deltaTime;
timeText.text = (int)duration + "''";
}
}
// 开始录制视频
public void recoderStart() {
// 正在录制中
if (isrecing)
return;
duration = 10;
timeText.text = "10''";
#if UNITY_ANDROID
// 设备不支持录制
if (!ShareREC.IsAvailable())
return;
ShareREC.OnRecorderStartedHandler = onStarted;
ShareREC.StartRecorder ();
#elif UNITY_IPHONE
// ios如果ShareRec脚本不在Main Camera上挂载需要说明
ShareRecIOs.setCallbackObjectName("shareRec");
ShareRecIOs.startRecoring();
onStarted();
#endif
}
// 开始录制的监听
void onStarted() {
// 开始录制,倒计时10S
isrecing = true;
Invoke("stopRecorder",10f);
// 显示录制UI
luzhiBtn.SetActive(true);
}
void stopRecorder() {
#if UNITY_ANDROID
ShareREC.OnRecorderStoppedHandler = onStopped;
ShareREC.StopRecorder ();
#elif UNITY_IPHONE
FinishedRecordEvent fin = new FinishedRecordEvent(recordFinishedHandler);
ShareRecIOs.stopRecording(fin);
#endif
luzhiBtn.SetActive(false);
}
// ios停止录制
void recordFinishedHandler(Exception ex)
{
isrecing = false;
if (ex == null)
{
//调到分享界面
//Hashtable userData = new Hashtable();
//userData["DayOfWeek"] = System.DateTime.Today.DayOfYear;
Screen.orientation = ScreenOrientation.Portrait;
ShareRecIOs.openSocial ("分享一个视频", null, SocialPageType.Share, new CloseEvent(()=>{
onShareReturn();
}));
}
}
// 停止录制的监听
void onStopped() {
isrecing = false;
#if UNITY_ANDROID
// 视频上传完毕才能分享,不然上传失败也没办法观看
// 修改屏幕方向,不然mob在观看视频会崩溃
Screen.orientation = ScreenOrientation.Portrait;
ShareREC.OnReturnFromShareHandler = onShareReturn;
ShareREC.setShareAfterUpload ();
ShareREC.ShowShare ();
#elif UNITY_IPHONE
#endif
}
// android 从分享界面返回
void onShareReturn() {
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
}
- 在Xcode下需要加入SDK包到工程根目录,在加入libz.td libicu.td libstdc.td javascript libsqlite 等设置不再详细说明
五、将shareRec录制的视频显示在相册中
录制结束后android端会保存一个视频文件在设备上(位置可在shareREC上指定),在相册中显示把这个路径通知到相册就可以了
ios 录制的视频位置不能指定,录制的视频存放在沙盒里的,同样的通过sdk提供的方法得到路径,在根据路径保存视频,相册显示
(1)、android
获取刚录制视频的路径
long[] aaa = ShareREC.ListLocalVideos();
//得到最后一个视频的视频id
long b = aaa[aaa.Length - 1];
//通过id得到视频的存储路径
string videoPathAndroid = ShareREC.GetLocalVideoPath(b);
StartCoroutine (ScreenshotManager.SaveExisting (videoPathAndroid, false));
(2)、iOS
string videoPathIos = ShareRecIOs.lastRecordingPath ();
saveVideoAtPathToPhotoalbum (videoPathIos);
(2.1) saveVideoAtPathToPhotoalbum 说明
此方法是一个插件方法,在Plugins/iOS下加入两个文件
1.PhotoAlbumUtil.h
#import <Foundation/Foundation.h>
#import <Security/Security.h>
@interface PhotoAlbumUtil : NSObject
+ (bool)saveVideoAtPathToPhotoalbum:(NSString *)path;
@end
2.PhotoAlbumUtil.m
#import "PhotoAlbumUtil.h"
#import <AssetsLibrary/AssetsLibrary.h>
@implementation PhotoAlbumUtil
+ (bool)saveVideoAtPathToPhotoalbum:(NSString *)path {
NSLog(@"存入视频地址%s-------------",path);
if (path == NULL) {
return false;
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path]
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Save video fail:%@",error);
} else {
NSLog(@"Save video succeed.");
}
}];
return true;
}
@end
最后,在调用方法的脚本中加入
[DllImport("__Internal")]
private static extern void saveVideoAtPathToPhotoalbum( string path );
说明:本文接入的shareREC版本 android:ShareREC for Unity Android 2.0.11.unitypackage iOS:v2.0.3