当前位置:编程学习 > wap >>

.MediaRecorder.getMaxAmplitude() 得到的值一直是0

代码在其它手机上用用这个方法都能拿到音量,在电信的本维5100s上就一直是0,但是微信可以拿到,QQ也可以,像遇见就不行,请问这个问题应该怎么解决

private Context context;
boolean success = false;
private static boolean showCancel;// 显示取消

public XWVoiceRecorder(Context context) {
this.context = context;
init();
}

public void setSavePath(String path) {

MyLog.log("MyVR setSavePath p" + mFileName);
mFileName = path;
}

/**
 * 返回当前录音文件保存路径
 * 
 * @return
 */
public String getSavePath() {
MyLog.log("MyVR getSavePath p" + mFileName);
if (mFileName == null)
return null;
// return mFileName;
File file = new File(mFileName);
if (file != null && file.canRead()) {
String fileName1 = mFileName;
if (mFileName.contains("/") && mFileName.lastIndexOf("/") + 1 < mFileName.length()) {
fileName1 = mFileName.substring(mFileName.lastIndexOf("/") + 1, mFileName.length());
} else if (mFileName.lastIndexOf("/") + 1 == mFileName.length()) {
fileName1 = mFileName;
}

MyLog.log("recorde  fileName1 =" + fileName1);
String sendNameString = fileName1;

MyLog.log("recorde  sendNameString =" + fileName1);
return sendNameString;
} else {
return null;
}
}

/**
 * 返回文件路径
 * 
 * @return 路径
 */
public String getRecordFilePath() {
return mFileName;
}

public void setOnFinishedRecordListener(OnFinishedRecordListener listener) {
finishedListener = listener;
}

private String mFileName = null;

private OnFinishedRecordListener finishedListener;

private long startTime;

private Dialog recordIndicator;

private static int[] res = { R.drawable.mic_0, R.drawable.mic_1, R.drawable.mic_2, R.drawable.mic_3, R.drawable.mic_4,
R.drawable.mic_5, R.drawable.mic_6, R.drawable.mic_7, R.drawable.mic_8, R.drawable.mic_too_short };

private static ImageView view;

private MediaRecorder recorder;

private ObtainDecibelThread thread;

private Handler volumeHandler;

private void init() {
volumeHandler = new ShowVolumeHandler();
}

/**
 * 显示对话框,并开始录音
 */
public void startRecord(String path) {
if (!hasSDCard()) {
if (context != null) {
Toast.makeText(context, "没有SD卡", 1).show();
}
return;
}
this.mFileName = path;
if (recordIndicator == null) {
recordIndicator = new Dialog(getContext(), R.style.like_toast_dialog_style);
View view2 = LayoutInflater.from(context).inflate(R.layout.radio_mic, null);

recordIndicator.setContentView(view2);
view = (ImageView) view2.findViewById(R.id.radio_image);
view.setImageResource(R.drawable.mic_2);
recordIndicator.setOnDismissListener(onDismiss);
LayoutParams lp = recordIndicator.getWindow().getAttributes();
lp.gravity = Gravity.CENTER;
recordIndicator.setCanceledOnTouchOutside(true);
}
startTime = System.currentTimeMillis();
volumeHandler.removeCallbacks(cancelRecording);
// recordIndicator.dismiss();
startRecording();
}

private Context getContext() {
return context;
}

public void finishRecord() {
if (!hasSDCard()) {
return;
}
stopRecording();

long intervalTime = System.currentTimeMillis() - startTime;
MyLog.log(" time=" + intervalTime / 1000);
if (XWVoicePlayer.getPlayLength(getRecordFilePath()) < 1) {// 太短,则录音取消
success = false;
volumeHandler.sendEmptyMessage(9);
volumeHandler.postDelayed(cancelRecording, 500);

File file = new File(mFileName);
file.delete();
return;
} else {
success = true;
recordIndicator.dismiss();
}

if (finishedListener != null)
finishedListener.onFinishedRecord(mFileName);
if (recordIndicator != null) {
recordIndicator.dismiss();
recordIndicator = null;
}
}

public boolean isSuccess() {
return success;
}

public void cancelRecord() {
try {
if (!hasSDCard()) {
return;
}
stopRecording();
// recordIndicator.dismiss();
// Toast.makeText(getContext(), "取消录音!", Toast.LENGTH_SHORT).show();
if (recordIndicator != null) {
recordIndicator.dismiss();
}
File file = new File(mFileName);
if (file != null && file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
 * 取消录音,隐藏对话框
 */
private Runnable cancelRecording = new Runnable() {

@Override
public void run() {
if (recordIndicator != null) {
recordIndicator.dismiss();
}
}
};

private void startRecording() {
try {
if (recorder == null) {
recorder = new MediaRecorder();
} else {
recorder.reset();
}
recorder.setMaxDuration(70000);
recorder.setOnErrorListener(new OnErrorListener() {

@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.i("xpf", " error="+what+" ex="+extra);
}
});
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Log.i("xpf", "set oudinsorce");
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
MyLog.log("MyVR p" + mFileName);
recorder.setOutputFile(mFileName);
if (hasSDCard()) {
recorder.prepare();
}
recorder.start();
thread = new ObtainDecibelThread();
thread.start();
DisShowCancel();
recordIndicator.show();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void stopRecording() {
if (thread != null) {
thread.exit();
thread = null;
}
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
}

private class ObtainDecibelThread extends Thread {

private volatile boolean running = true;

public void exit() {
running = false;
}

@Override
public void run() {
while (running) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (recorder == null || !running) {
break;
}
if (!hasSDCard()) {
return;
}
int x = recorder.getMaxAmplitude();
Log.i("xpf", "set getMaxAmplitude");
MyLog.log("ffx=" + x);
if (x != 0) {
int f = (int) (10 * Math.log(x) / Math.log(10)) - 32;
MyLog.log("ff=" + f);
if (f < 0) {
f = -f;
}
if (f == 0) {
volumeHandler.sendEmptyMessage(0);// 安静状态
} else if (f >= 8) {
volumeHandler.sendEmptyMessage(8);// 最大
} else {
volumeHandler.sendEmptyMessage(f);// 按音量
}
}

}
}

}

private OnDismissListener onDismiss = new OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
stopRecording();
}
};

public static boolean hasSDCard() {
String status = Environment.getExternalStorageState();
if (!status.equals(Environment.MEDIA_MOUNTED)) {
return false;
}
return true;
}

static class ShowVolumeHandler extends Handler {

@Override
public void handleMessage(Message msg) {
MyLog.log("ffs=" + msg.what + " L=" + res.length);
if (msg.what < res.length && !showCancel) {
view.setImageResource(res[msg.what]);
}
}
}

public interface OnFinishedRecordListener {
public void onFinishedRecord(String audioPath);
}

public void showCancel() {
showCancel = true;
view.setImageResource(R.drawable.radio_up_cancel);
}

public void DisShowCancel() {
showCancel = false;
view.setImageResource(R.drawable.mic_0);
}

--------------------编程问答-------------------- 然后看了android的源码,得到音量这个方法用的是本地的native 方法。但是我又不会C++,也没有源码不能分析,然后反编了微信的apk,看到他们的录音好你用的是自己写的native方法。如下:
package com.tencent.mm.compatible.audio;

import android.media.MediaRecorder;
import com.tencent.mm.compatible.f.k;
import com.tencent.mm.pointers.PByteArray;
import com.tencent.mm.sdk.platformtools.by;
import com.tencent.mm.sdk.platformtools.y;

public class SimpleMediaRecorder
{
  public static final int[] bnp = { 13, 14, 16, 18, 20, 21, 27, 32 };
  private static long bnw = 0L;
  private static Object bnx = new Object();
  private n bnA = null;
  private b bnB;
  private aa bnC;
  private int bnD = 8000;
  private int bnE = 0;
  private k bnF = new k();
  private p bnG = new u(???);
  private z bnq;
  private int bnr = 0;
  private String bns = null;
  private v bnt = null;
  private long bnu = 0L;
  private long bnv = 0L;
  private MediaRecorder bny;
  private int bnz;

  public SimpleMediaRecorder(b paramb)
  {
    this.bnB = paramb;
    if (paramb == b.bmv)
    {
      this.bnz = 7;
      this.bny = new MediaRecorder();
      return;
    }
    this.bnD = 8000;
    int i = by.a((Integer)com.tencent.mm.compatible.c.n.gJ().get(102401), 0);
    y.aC("MicroMsg.SimpleMediaRecorder", "dk16k sr:" + this.bnD + " notsu:" + i);
    if (i == 1)
      this.bnD = 8000;
    this.bnr = 0;
    this.bns = null;
    this.bnt = null;
    try
    {
      y.aC("MicroMsg.SimpleMediaRecorder", "!!out mutex :" + bnx.hashCode());
      synchronized (bnx)
      {
        this.bnA = new n(this.bnD, 120, true, 0);
        this.bnA.a(this.bnG);
        this.bnC = aa.bnV;
        this.bnz = 1;
        return;
      }
    }
    catch (Exception localException)
    {
      while (localException.getMessage() != null)
      {
        y.az("MicroMsg.SimpleMediaRecorder", localException.getMessage());
        this.bnC = aa.bnY;
      }
      y.az("MicroMsg.SimpleMediaRecorder", "Unknown error occured while initializing recording");
    }
  }

  private static native boolean native_init();

  private static native boolean native_pcm2amr(int paramInt1, byte[] paramArrayOfByte, int paramInt2, PByteArray paramPByteArray, int paramInt3);

  private static native boolean native_pcmresamp(byte[] paramArrayOfByte, int paramInt, PByteArray paramPByteArray);

  private static native boolean native_release();

  public final void a(z paramz)
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bnq = paramz;
      this.bny.setOnErrorListener(new t(this));
      this.bnC = aa.bnY;
      return;
    }
    try
    {
      if (this.bnC != aa.bnV);
      this.bnq = paramz;
      return;
    }
    catch (Exception localException)
    {
      if (localException.getMessage() != null)
      {
        y.az("MicroMsg.SimpleMediaRecorder", localException.getMessage());
        this.bnC = aa.bnY;
        return;
      }
      y.az("MicroMsg.SimpleMediaRecorder", "Unknown error occured while setting output path");
    }
  }

  public final int getMaxAmplitude()
  {
    if (this.bnB == b.bmv)
      if (this.bny == null);
    do
    {
      return 0;
      return this.bny.getMaxAmplitude();
    }
    while (this.bnC != aa.bnX);
    int i = this.bnr;
    this.bnr = 0;
    return i;
  }

  public final void gq()
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bny.setMaxDuration(3600010);
      return;
    }
    this.bnu = 3600010L;
  }

  public final void gr()
  {
    if ((this.bnB != b.bmv) || (this.bny == null))
      return;
    this.bny.setAudioEncoder(1);
  }

  public final void gs()
  {
    if ((this.bnB != b.bmv) || (this.bny == null))
      return;
    this.bny.setAudioSource(1);
  }

  public final void gt()
  {
    if ((this.bnB != b.bmv) || (this.bny == null))
      return;
    this.bny.setOutputFormat(3);
  }

  public final boolean gu()
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny != null)
      {
        this.bny.stop();
        this.bny.release();
        this.bny = null;
      }
      return true;
    }
    k localk = new k();
    y.aC("MicroMsg.SimpleMediaRecorder", "Stop now  state:" + this.bnC);
    if (this.bnC != aa.bnX)
    {
      y.az("MicroMsg.SimpleMediaRecorder", "stop() called on illegal state");
      this.bnC = aa.bnY;
      return true;
    }
    synchronized (bnx)
    {
      if (this.bnA != null)
      {
        this.bnA.gk();
        this.bnA.a(null);
      }
      long l1 = localk.hk();
      this.bnC = aa.bnZ;
      long l2 = localk.hk();
      this.bnt.gy();
      long l3 = by.L(this.bnv);
      y.az("MicroMsg.SimpleMediaRecorder", "toNow " + l3 + " sStartTS " + this.bnv + " bufferLen " + bnw);
      if ((l3 <= 2000L) || (bnw != 0L))
        break label250;
      com.tencent.mm.compatible.c.n.gJ().set(102401, Integer.valueOf(1));
      y.az("MicroMsg.SimpleMediaRecorder", "16k not suppourt");
      label250: y.aC("MicroMsg.SimpleMediaRecorder", "Wait Stop Time Media:" + l1 + " Read:" + l2 + " Thr:" + localk.hk());
      return false;
    }
  }

  public final void prepare()
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bny.prepare();
      return;
    }
    if ((this.bnC != aa.bnV) || (this.bns == null))
    {
      this.bnC = aa.bnY;
      release();
      return;
    }
    this.bnC = aa.bnW;
  }

  public final void release()
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bny.release();
      return;
    }
    if (this.bnC == aa.bnX)
      gu();
    while (true)
      synchronized (bnx)
      {
        if (this.bnA != null)
        {
          this.bnA.gk();
          this.bnA = null;
        }
        return;
      }
  }

  public final void setOutputFile(String paramString)
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bny.setOutputFile(paramString);
      this.bns = paramString;
      return;
    }
    if (this.bnC == aa.bnV)
    {
      this.bns = paramString;
      return;
    }
    this.bnC = aa.bnY;
  }

  public final void start()
  {
    if (this.bnB == b.bmv)
    {
      if (this.bny == null)
        return;
      this.bny.start();
      return;
    }
    y.aC("MicroMsg.SimpleMediaRecorder", "Start now  state:" + this.bnC);
    if (this.bnC == aa.bnW)
    {
      this.bnv = System.currentTimeMillis();
      bnw = 0L;
      this.bnC = aa.bnX;
      synchronized (bnx)
      {
        if (this.bnt == null)
        {
          this.bnt = new v();
          v.a(this.bnt, this.bnz, this.bns);
        }
        this.bnA.gj();
        return;
      }
    }
    com.tencent.mm.compatible.c.n.gJ().set(102401, Integer.valueOf(1));
    y.az("MicroMsg.SimpleMediaRecorder", "start() called on illegal state");
    this.bnC = aa.bnY;
  }
}
--------------------编程问答--------------------
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,