android UiAutomator获取视频播放进度的方法

660 阅读3分钟

本人在使用android UiAutomator做测试的时候,有时候需要统计视频播进度,然后去断言上传的进度数据正确与否。具体的思路就是根据进度条的颜色区分,我选的红色,然后去计算各个点的数值,然后计算进度的百分比。

这是app的界面进度条的截图

下面是我两次获取到的数据。

下面是我的代码:

package student;
 
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.android.uiautomator.core.UiObjectNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.RemoteException;
import source.UiAutomatorHelper;
/**
* @author ··-·尘
* @E-mail:Fhaohaizi@163.com
* @version 创建时间:2017年8月18日 上午10:53:24
* @alter 修改时间:2017年9月7日 12:55:36
* 类说明:测试调试用例
*/
@SuppressWarnings("deprecation")
public class Student extends StudentCase{
	public static String jarName,testClass,testName,androidId;
	public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException, IOException, ParseException {
		jarName = "DemoStudent";testClass="student.Student";testName="testTest";
//		PerformanceThread.getInstance().start();//启动线程
		new UiAutomatorHelper(jarName, testClass, testName);//调试用例
//		PerformanceThread.key = false;//结束线程
	}
	public void testTest() throws InterruptedException, IOException, UiObjectNotFoundException, RemoteException {
		clickPiont(500, 500);//点击屏幕,显示播放进度条
		sleep(100);//休眠等待截图
		Bitmap bitmap = getBitmapByResourceId("com.genshuixue.student:id/view_video_coursede_control_seekbar");//获取播放空间的bitmap实例
		double percent = getVideoProgress(bitmap);
	}
}

下面是获取bitmap的方法

	//截取某个控件的图像
	public Bitmap getBitmapByResourceId(String id) throws UiObjectNotFoundException {
		Rect rect = getUiObjectByResourceId(id).getVisibleBounds();//获取控件的rect对象
		String path = screenShot("test");//截图
		Bitmap bitmap = BitmapFactory.decodeFile(path);//创建并实例化bitmap对象
		bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());//截取bitmap实例
		return bitmap;
	}

下面是获取进度的方法:

	//获取视频播放进度条
	public double getVideoProgress(Bitmap bitmap) {
		int height = bitmap.getHeight();
		int width = bitmap.getWidth();
		List<Integer> date = new ArrayList<Integer>();
		for (int i = 0;i < width; i++) {
			int color = bitmap.getPixel(i, height / 2);
			int red = Color.red(color);
//			output(red);
			date.add(red);
			}
		int date1 = 0,date2 = 0,date3 = 0,date4 = 0;
		int status1 = 0,status2 = 0;
		for (int i = 1;i < date.size() - 1;i++) {
			
			if (date.get(i) == 255 && status1 == 0) {
				status1++;
				date1 = i;
			}
			if (date.get(i) == 238 && status2 == 0) {
				status2++;
				date2 = i;
			}
			if (date.get(i + 1) < 238 && date.get(i) == 238) {
				date3 = i;
			}
			if (date.get(i + 1) < 165 && date.get(i) == 165) {
				date4 = i;
			}
		}
//		output(date1, date2, date3, date4);
//		output((date2 + date3 - date1 * 2.00)/(date4 - date1)/2);
		return (date2 + date3 - date1 * 2.00)/(date4 - date1)/2;
	}

技术类文章精选

非技术文章精选

点击查看公众号地图