35

I'm developing an app which needs to get music file by streaming for playing live.

In the request song api I can specify the bandwith (eg: 50kbps, 100kbps, 300, 600 or 1 Mbps).

The more the bandwith is big, the more the file will get time to be fetched. As I don't want the users to be restricted about that I have multiple choices to deal with it:

  • Detect wether the phone is using 3g, wifi or Edge and specify an bandwith for each connection speed.

  • Let the user decide the quality of the song he will get: like youtube (but the users won't be people that know much about computing: the more easy is the software, the more it will fit)

  • Having a way to evaluate properly the connection speed: Like fetching a file, measure the time that it took and set the bandwith.

I know that connection speed could vary a lot if user loose the wifi, or is using 3g moving in the street. And the thing is that I can't change the bandwidth when the song will be playing.

Maybe you have experience about that you would like to share?

Thank you!

Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
GrandMarquis
  • 1,913
  • 1
  • 18
  • 30

4 Answers4

43

Facebook released a library for this:

https://github.com/facebook/network-connection-class

this wasn't existing in 2011..

GrandMarquis
  • 1,913
  • 1
  • 18
  • 30
5

why not try to change your view of things. Try to flow with your users. say your user wishes to download 128 kbit quality song. you start the download , WHILE downloading you make an average download time, take a few seconds for this average to stabilize, and if it's below certain value make a pop up to tell the user that his connection is too slow for the current bandwidth and ask him if to lessen the quality or to keep downloading slowly.

This will:

  1. let the users the option to always assume they can get the best quality media.
  2. let u do your check in runtime and change the quality accordingly while downloading without the need to pre check.
  3. keeps your app simple to users.

I know i'm not answering your specific requirement, i'm just offering a different view.

codeScriber
  • 4,582
  • 7
  • 38
  • 62
  • Nice point of view, that's exactly the type of answers I was waiting. One thing that is not good for me is that I will have yo restart the download from scratch. I'll have a talk with my team mates and we'll see – GrandMarquis Sep 27 '11 at 07:18
4
protected String doInBackground(String... urls) {
    String response = "";

    startTime = System.currentTimeMillis();
    for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {

            HttpResponse execute = client.execute(httpGet);
            InputStream content = execute.getEntity().getContent();


            BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                response += s;
            }
            endTime = System.currentTimeMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return response;
}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub


    long dataSize = result.length() / 1024;
    takenTime = endTime - startTime;
    long s = takenTime / 1000;
    double speed = dataSize / s;

    Toast.makeText(context, "" + s + "kbps", Toast.LENGTH_SHORT).show();
}
  • Can you add the initializers for the startTime and endTime variables? We're not sure if it's a long or what. – Pier Betos Jan 07 '16 at 08:40
2

Detect network connection type on Android

You can check all available options here: http://developer.android.com/reference/android/telephony/TelephonyManager.html

This can fix the mobile network type but can't help you with the Wifi speed, you should code it by downloading something from a server you know and calculate the time.

I hope it helps.

Community
  • 1
  • 1
atzu
  • 424
  • 3
  • 14
  • 3
    Thank you, I know search engines and it's a real question not something I'm too lazy to search on google. Next time: give source http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android – GrandMarquis Sep 26 '11 at 13:21
  • You're welcome just tried to help, anyway you have all the options in TelephonyManager to fix the connection type issue. For the other, I think that you should test your bandwidth on site. You're right about the source, edited and I apologize about that. – atzu Sep 26 '11 at 13:26
  • Ok, then i'll try the third option: fetching a file and with time that took: calculate the bandwith ;) – GrandMarquis Sep 26 '11 at 13:28
  • Well, you can know the bandwith for the mobile networks in a quite accurate way, as long as you don't switch from one to another as you said. – atzu Sep 26 '11 at 13:30
  • Changed it, because as you said, just c&p-ed and I think it's better not to duplicate contents. – atzu Sep 26 '11 at 13:36