Android Open Source - opentok-android-sdk-samples Audio Level View






From Project

Back to project page opentok-android-sdk-samples.

License

The source code is released under:

Apache License

If you think the Android project opentok-android-sdk-samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.opentok.android.demo.ui;
/*from  w w w. j av  a2s  .co  m*/
import com.opentok.android.demo.opentoksamples.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;

public class AudioLevelView extends View {

  Context mContext;
  float mValue = 0;
  Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  Paint mPaintGradient = new Paint(Paint.ANTI_ALIAS_FLAG);
  Rect mBounds = new Rect();
  Bitmap mHeadset;
  boolean mMute = false;

  public AudioLevelView(Context context) {
    super(context);
    mContext = context;
    init();
  }
  
  public AudioLevelView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    init();
  }

  private void init() {
    mPaint.setStyle(Style.FILL);
    mPaint.setColor(getResources().getColor(R.color.black_grey));
    mPaintGradient.setStyle(Style.FILL);
    mPaintGradient.setColor(getResources().getColor(R.color.init_gradient));
  }

  @Override
  protected void onDraw(Canvas canvas) {
    float radius = 500 * 0.5f;
    canvas.drawCircle(getWidth(), 0,
        radius, mPaint);
    
    if (!mMute) {
      if (mHeadset != null) {
        canvas.drawBitmap(mHeadset,
            (getWidth() - mHeadset.getWidth() - 30),
            mHeadset.getHeight() * 0.3f, mPaint);
      }
      canvas.drawCircle(getWidth(), 0,
          radius*mValue, mPaintGradient);
    } 
  }
  
  public void setIcons(Bitmap headset) {
    mHeadset = headset;
  }

  public void setMeterValue(float value) {
    // Convert linear value to logarithmic
    double db = 20 * Math.log10(value);
    float floor = -40;
    float level = 0;
    if (db > floor) {
      level = (float) db - floor;
      level /= -floor;
    }
    mValue = level;

    // force redraw
    invalidate();
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBounds.left = (int) (0 + w * 0.10);
    mBounds.top = (int) (0 + h * 0.10);
    mBounds.right = (int) (w * 0.90);
    mBounds.bottom = (int) (h * 0.90);
    // Update gradient
    mPaintGradient.setShader(new RadialGradient(w / 2, h / 2, h / 2,
        getResources().getColor(R.color.color0_gradient), getResources().getColor(R.color.color1_gradient), TileMode.CLAMP));

  }

  
}




Java Source Code List

com.opentok.android.demo.audio.CustomAudioDevice.java
com.opentok.android.demo.config.OpenTokConfig.java
com.opentok.android.demo.multiparty.MySession.java
com.opentok.android.demo.multiparty.MySubscriber.java
com.opentok.android.demo.opentoksamples.AudioDeviceActivity.java
com.opentok.android.demo.opentoksamples.HelloWorldActivity.java
com.opentok.android.demo.opentoksamples.MultipartyActivity.java
com.opentok.android.demo.opentoksamples.OpenTokSamples.java
com.opentok.android.demo.opentoksamples.UIActivity.java
com.opentok.android.demo.opentoksamples.VideoCapturerActivity.java
com.opentok.android.demo.opentoksamples.VideoRendererActivity.java
com.opentok.android.demo.opentoksamples.VoiceOnlyActivity.java
com.opentok.android.demo.services.ClearNotificationService.java
com.opentok.android.demo.ui.AudioLevelView.java
com.opentok.android.demo.ui.MeterView.java
com.opentok.android.demo.ui.fragments.PublisherControlFragment.java
com.opentok.android.demo.ui.fragments.PublisherStatusFragment.java
com.opentok.android.demo.ui.fragments.SubscriberControlFragment.java
com.opentok.android.demo.ui.fragments.SubscriberQualityFragment.java
com.opentok.android.demo.video.CustomVideoCapturer.java
com.opentok.android.demo.video.CustomVideoRenderer.java