opened
shiv-u
username_0
Before filing a question:
-----------------------
- This issue tracker is intended ExoPlayer specific questions. If you're asking
  a general Android development question, please do so on Stack Overflow.
- Search existing issues, including issues that are closed. It’s often the
  quickest way to get an answer!
  https://github.com/google/ExoPlayer/issues?q=is%3Aissue
- Consult our developer website, which can be found at https://exoplayer.dev/.
  It provides detailed information about supported formats, devices as well as
  information about how to use the ExoPlayer library.
- The ExoPlayer library Javadoc can be found at
  https://exoplayer.dev/doc/reference/

When filing a question:
-----------------------
Fill out the sections below, leaving the headers but replacing the content. If
you're unable to provide certain information, please explain why in the relevant
section. We may close issues if they do not include sufficient information.

### [REQUIRED] Searched documentation and issues
I have looked into https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/upstream/BandwidthMeter.html and https://medium.com/google-exoplayer/https-medium-com-google-exoplayer-simplified-bandwidth-meter-usage-17d8189f978b but didn't find anything related to data consumption.
### [REQUIRED] Question
I want to calculate the amount of data consumed while streaming a video. I know that we can get that data by using https://developer.android.com/reference/android/net/TrafficStats#getUidRxBytes(int) but this needs user permission. So is there any way through which I can get the data consumed ?
Can we get the data consumed by my app using bandwidthmeter  ?
issue
created
tonihei
username_1
The easiest way to sum up all loaded bytes is to listen to `AnalyticsListener.onLoadCompleted/onLoadCanceled/onLoadError` and add up the bytes reported in `loadEventInfo.bytesLoaded`.

You could theoretically also add a new `TransferListener` to all your `DataSources` to get the same data as the `DefaultBandwidthMeter`. However, there are some problems with doing that and I would advise against that:
 1. The `TransferListener` callbacks are called on background threads and require you to add additional thread safety to make it work for your case.
 2. The callbacks are not called for all loads because some of them are intentionally left out for bandwidth metering (e.g. manifest loads).
comment
created
shiv-u
username_0
I got it working as you suggested but the number of bytes which I get from onLoadCompleted is slightly more than the original video size, is there any explanation for that ?
comment
created
tonihei
username_1
That's hard to say without example media and concrete reproduction steps. Could possibly be related to seeks in the media (e.g. to read metadata at the end of the file), or user-initated seeks that resulted in the same data read twice, or network errors that resulted in a retry of the same data.
comment
created
shiv-u
username_0
@username_1 consider this issue, whenever there is buffering onLoadError is called and I keep track of bytesloaded as below 

     public void onLoadError(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData, IOException io,boolean wasCancelled){
        this.totalBytesLoaded+=loadEventInfo.bytesLoaded;

       System.out.println("in OnLoadError total"+totalBytesLoaded+","+loadEventInfo.bytesLoaded+","+eventTime.currentPlaybackPositionMs);
        //  System.out.println("in OnLoadError"+loadEventInfo.bytesLoaded);
    } 
and when the player is seeked till the end of the media it will call onLoadCompleted

         public void onLoadError(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData, IOException io,boolean wasCancelled){
        this.totalBytesLoaded+=loadEventInfo.bytesLoaded;

       System.out.println("in OnLoadError total"+totalBytesLoaded+","+loadEventInfo.bytesLoaded+","+eventTime.currentPlaybackPositionMs);
        //  System.out.println("in OnLoadError"+loadEventInfo.bytesLoaded);
    }
there also I am adding the number of bytes to the total number of bytes loaded till then which was due to onLoadError. Then the final number of bytes loaded will be more than the file size. I am not using cache mechanism, so am I doing it right ?
comment
closed
tonihei
username_1
issue
created
tonihei
username_1
Closing, because the question was answered.
comment