getMaxAmplitude返回0 - Android

时间:2012-02-17 17:22:25

标签: android media-player return zero

我试图获得录制的声音的maxAmplitude。 我做了一个工作得很好的录音机,但是当我使用getMaxAmplitude来获得振幅时,它总是返回0。 我看了整个互联网,但无法找到一个有效的解决方案,所以我希望有人可以帮助我,因为我卡住了。 请记住我是初学者;)

这是我的代码:

import java.io.IOException;    

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class vumeter extends Activity{
    private static final String LOG_TAG = "AudioRecordTest";
    protected static final String TAG = null;
    private static String mFileName = null;
    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;
    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;
    public Handler mHandler;
    public int currentAmplitude;
    public boolean activeThread;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
            }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        if (mRecorder == null) {

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        }

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();

    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        activeThread = true;
        mRecorder = null;
    }


    public void run() { 
        int i = 0; 
        while(i == 0) { 
            Message msg = mHandler.obtainMessage(); 
            Bundle b = new Bundle(); 
            try { 
                Thread.sleep(250); 
            } catch (InterruptedException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            if (mRecorder != null) { 
                currentAmplitude = mRecorder.getMaxAmplitude(); 
                b.putLong("currentTime", currentAmplitude); 
                Log.i("AMPLITUDE", new Integer(currentAmplitude).toString()); 
            } else { 
                b.putLong("currentTime", 0); 
            } 
            msg.setData(b); 
            mHandler.sendMessage(msg); 
        } 
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }

    public vumeter() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        TextView tv = new TextView(this);
        ll.addView(tv,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
        tv.setText(Integer.toString(currentAmplitude));
        setContentView(ll);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

1 个答案:

答案 0 :(得分:0)

你不能在任何地方调用你的run()方法。你宁愿用这种方法创建一个线程:

Private Sub cmdEmail1_Click()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strBody As String
Dim strPDF As String
Dim strFolder As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)

strBody = Me.txtSubject
strPDF = Me.txtFile
On Error Resume Next
With OutMail
    .To = Me.txtemail
    .CC = ""
    .BCC = Me.txtBBCemail
    .Subject = Me.txtSubject
    .Body = Me.txtMessage
    .Recipients.ResolveAll
'   .SendUsingAccount =   OutApp.Session.Accounts.Item(2)                         '2nd email
    .SentOnBehalfOfName = Me.txtFromEmail
    .Attachments.Add        strPDF                                                    'attachments
    .Send

End With
Me.txtSent = "email was sent to Outlook "

On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

然后在您选择的方法中启动此线程,例如startRecording()。不要忘记中断()它。而且,你最好在循环中添加一个检查标志。