Click here to Skip to main content
11,702,130 members (66,338 online)
Click here to Skip to main content

Tagged as

How to access network speed when playing video in Android Platform

, 25 Dec 2011 CPOL 18.1K 7
Rate this:
Please Sign up or sign in to vote.
As MediaPlayer of Android has no API to know the buffering time, I just only access the speed of network via reading the linux proc file(/proc/net/dev) to enable the UI displaying the download speed
  1. The function of reading how many bytes have downloaded via wifi port:

    private long parserNumber(String line) throws Exception {
    		long ret=0;
    		String[] delim = line.split(" ");
    		if(delim.length >= 1){
    			ret = Long.parseLong(delim[0]);
    		return ret;
    	public long syncFetchReceivedBytes() {
    		// TODO Auto-generated method stub
    		ProcessBuilder cmd;
    		long readBytes=0;
    		BufferedReader rd = null;
    		try {
    			String[] args = { "/system/bin/cat", "/proc/net/dev" };
    			cmd = new ProcessBuilder(args);
    			Process process = cmd.start();
    			rd = new BufferedReader(new InputStreamReader(
    			String line;
    			int linecount = 0;
    			while ((line = rd.readLine()) != null) {
    					String[] delim = line.split(":");
    		} catch (Exception ex) {
    		} finally {
    			if (rd != null) {
    				try {
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    		return readBytes;
  2. Invoke the above function periodically (such as 1 second) to access network speed, for example:
    long curReadBytes=syncFetchReceivedBytes();
    String strSpeed=(curReadBytes-lastReadBytes)/1024 kbps
    lastReadBytes = curReadBytes;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China
I am a software engineer with programming experience C/C++, MFC/Win32,Assembler(C51/ARM),Java and work in Science Park of ShenZhen City,the global manufacturer center,in China.
I like pingpong,basketball,bowling and table tennis sport.

You may also be interested in...

Comments and Discussions

GeneralIt useful one,thanks. Let me know, the function of reading h... Pin
Member 851903626-Dec-11 18:34
memberMember 851903626-Dec-11 18:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by tojine
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid