分享音频点击跳转页面显示黑屏播放器(页面什么都没有全黑)
后来在分享音频的时候,需要添加分享音乐的URL地址,也就是WXMusicObject.musicUrl属性,但是又一个肯人的地方来了。当我设置了一个分享的音频地址的时候OK可以分享也可以播放,但是当我点击分享内容的时候跳转到了一个不知道是什么鬼的页面,除了播放、暂停按钮什么都没有全黑页面。郁闷无比呀!后来通过多方查找返现,WX并没有提供分享页面的属性在WXMusicObject类中。于是只能通过musicUrl属性自己拼接:
分享地址 + "#wechat_music_url=" + 音频地址
微信客户端首次登陆分享失败问题
本以为可以安安稳稳的分享数据了,好景不长测试给提了BUG,微信客户端首次登陆(清除WX数据)通过项目首次分享不成功。但是郁闷是第二次又成功了。也不知道微信在搞什么东东。找了好多文档都没有这样的事情发生,也测试IOS的分享也是没有这个问题,一度认为可能是WX-SDK问题。后来通过微信Demo发现每个Activity中都调用了IWXAPI.register()注册方法。于是将该方法写在了Application中,满心认为应该没有问题了,但是通过测试返现没有任何作用,于是各种测试发现也在Application中的时候当开启应用程序之后在登陆WX客户端分享就失败了。但是当登陆后在开启应用程序分享就没有问题了。所以调整了一下注册的位置,当我每次进行分享的时候调用register()方法。分享完毕的时候调用unregister()方法取消注册。这个问题就完美解决了。
下面以分享音频为例,简单封装了一个工具类和大家分享
public class WXShareUtils {
private final String TAG = TTLog.makeLogTag(WXShareUtils.class);
private int THUMB_SIZE = 150;
private static volatile WXShareUtils manager;
//构造器对象
private Builder mBuilder;
private IWXAPI api;
private WXShareUtils() {
}
public static WXShareUtils getInstance() {
WXShareUtils cache = manager;
if (cache == null) {
synchronized (WXShareUtils.class) {
cache = manager;
if (cache == null) {
cache = new WXShareUtils();
manager = cache;
}
}
}
return cache;
}
public void shareMusic() throws IllegalStateException {
if (mBuilder == null) {
TTLog.d(TAG, "The utility object is null. --->Builder is not null.<---");
throw new IllegalStateException("The utility object is null. --->Builder is not null.<---");
} else if (TextUtils.isEmpty(mBuilder.getMusicUrl())) {
TTLog.d(TAG, "--->MusicUrl is not null.<---");
throw new IllegalStateException("--->MusicUrl is not null.<---");
} else if (mBuilder.getScene() != 0 && mBuilder.getScene() != 1) {
TTLog.d(TAG, "--->scene is SendMessageToWX.Req.WXSceneTimeline(0) or SendMessageToWX.Req.WXSceneSession(1).<---");
throw new IllegalStateException("--->scene is SendMessageToWX.Req.WXSceneTimeline(0) or SendMessageToWX.Req.WXSceneSession(1).<---");
} else if (mBuilder.getActivity() == null) {
TTLog.d(TAG, "--->Activity is not null.<---");
throw new IllegalStateException("--->Activity is not null.<---");
} else if (TextUtils.isEmpty(mBuilder.getTitle())) {
TTLog.d(TAG, "--->Title is not null.<---");
throw new IllegalStateException("--->Title is not null.<---");
} else if (TextUtils.isEmpty(mBuilder.getDescription())) {
TTLog.d(TAG, "--->Description is not null.<---");
throw new IllegalStateException("--->Description is not null.<---");
} else if (TextUtils.isEmpty(mBuilder.getShareUrl())) {
TTLog.d(TAG, "--->Share Url is not null.<---");
throw new IllegalStateException("--->Share Url is not null.<---");
}
RequestAsyncTask task = new RequestAsyncTask() {
@Override
public void callBack(Bitmap bitmap) {
shareWXMusic(bitmap);
releaseBuilder();
}
};
task.execute(mBuilder.getImageUrl());
}
/**
* 分享音频
*
* @param bmp
*/
private void shareWXMusic(Bitmap bmp) {
api = getApi();
api.registerApp(Constants.APP_ID);
if (api.isWXAppInstalled()) {
WXMusicObject music = new WXMusicObject();
music.musicUrl = mBuilder.getShareUrl() + "#wechat_music_url=" + mBuilder.getMusicUrl();
// music.musicUrl = mBuilder.getMusicUrl();
// music.musicLowBandUrl = mBuilder.getMusicUrl();
// music.musicDataUrl = mBuilder.getShareUrl();
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = music;
msg.title = mBuilder.getTitle();
msg.description = mBuilder.getDescription();
Bitmap thumbBmp = null;
if (bmp != null) {
thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
msg.thumbData = bmpToByteArray(thumbBmp, true);
bmp.recycle();
} else {
Bitmap resBitmap = BitmapFactory.decodeResource(TTApplication.getAppResources(), R.drawable.share_default_icon);
thumbBmp = Bitmap.createScaledBitmap(resBitmap, THUMB_SIZE, THUMB_SIZE, true);
msg.thumbData = bmpToByteArray(thumbBmp, true);
resBitmap.recycle();
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("music");
req.message = msg;
req.scene = mBuilder.getScene();
api.sendReq(req);
} else {
ToastUtils.showToast(mBuilder.getActivity(), R.string.share_error_content);
}
api.unregisterApp();
}
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
private byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
if (needRecycle) {
bmp.recycle();
}
return result;
}
private Bitmap getImageFromNet(String url) {
HttpURLConnection conn = null;
try {
URL mURL = new URL(url);
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("GET"); //设置请求方法
conn.setConnectTimeout(10000); //设置连接服务器超时时间
conn.setReadTimeout(5000); //设置读取数据超时时间
conn.setDoInput(true);
conn.connect(); //开始连接
int responseCode = conn.getResponseCode(); //得到服务器的响应码
if (responseCode == 200) {
//访问成功
InputStream is = conn.getInputStream(); //获得服务器返回的流数据
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
Bitmap bmpImage = BitmapFactory.decodeStream(bufferedInputStream);//根据流数据 创建一个bitmap对象
return bmpImage;
} else {
//访问失败
TTLog.d(TAG, "访问失败===responseCode:" + responseCode);
}
} catch (Exception e) {
TTLog.e(TAG, e, "load image url fail");
return null;
} finally {
if (conn != null) {
conn.disconnect(); //断开连接
}
}
return null;
}
/**
* 异步加载图片
*/
private abstract class RequestAsyncTask extends AsyncTask<String, Void, Bitmap> {
public abstract void callBack(Bitmap bitmap);
@Override
protected Bitmap doInBackground(String... params) {
if (TextUtils.isEmpty(params[0])) {
return null;
}
return getImageFromNet(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
callBack(bitmap);
}
}
/*
* 内部Builder构建器
* */
public static class Builder {
private String musicUrl = null;
private String title = null;
private String description = null;
private String imageUrl = null;
private int scene = -1;//0 好友 1朋友圈 2 收藏
private Activity activity = null;
private String shareUrl = null;
public String getMusicUrl() {
return musicUrl;
}
public Builder setMusicUrl(String musicUrl) {
this.musicUrl = musicUrl;
return this;
}
public String getTitle() {
return title;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public String getDescription() {
return description;
}
public Builder setDescription(String description) {
this.description = description;
return this;
}
public String getImageUrl() {
return imageUrl;
}
public Builder setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
return this;
}
public int getScene() {
return scene;
}
/**
* 0 好友 1朋友圈
*
* @param scene
* @return
*/
public Builder setScene(int scene) {
this.scene = scene;
return this;
}
public Activity getActivity() {
return activity;
}
public Builder setActivity(Activity activity) {
this.activity = activity;
return this;
}
public String getShareUrl() {
return shareUrl;
}
public Builder setShareUrl(String shareUrl) {
this.shareUrl = shareUrl;
return this;
}
}
private IWXAPI getIWXAPI() {
return WXAPIFactory.createWXAPI(mBuilder.getActivity(), Constants.APP_ID, false);
}
private IWXAPI getIWXAPI(Context context){
return WXAPIFactory.createWXAPI(context, Constants.APP_ID, false);
}
public IWXAPI getApi() {
if (api != null)
return api;
else
return getIWXAPI();
}
public IWXAPI getApi(Context context) {
if (api != null)
return api;
else
return getIWXAPI(context);
}
private void releaseBuilder() {
if (mBuilder != null) {
mBuilder = null;
TTLog.d(TAG, "release WXShareUtils Builder.");
}
}
/**
* 配置方法
*
* @param builder
* @return
*/
public WXShareUtils option(Builder builder) {
this.mBuilder = builder;
return manager;
}
}