zoukankan      html  css  js  c++  java
  • 【Android 多媒体应用】使用MediaRecoder录制,MediaPlayer播放音频数据

    1.MainActivity.java

    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.IOException;
    
    public class MainActivity extends Activity implements View.OnClickListener {
        private final static String TAG = "debug--";
        private Button btnRecord,btnStop,btnPlay;
        private File soundFile;
        private MediaRecorder mRecoder;
        private MediaPlayer mPlayer;
        private TextView mTxt ;
        private Handler handler = new Handler(){
            public void handleMessage(Message msg) {
                Log.d(TAG, Integer.toString(msg.what));
                mTxt.setText(msg.what/60+":"+msg.what%60);
                super.handleMessage(msg);
            }
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnRecord = (Button)findViewById(R.id.start);
            btnStop = (Button)findViewById(R.id.stop);
            btnPlay = (Button)findViewById(R.id.play);
            mTxt = (TextView)findViewById(R.id.txtTime);
            btnRecord.setOnClickListener(this);
            btnStop.setOnClickListener(this);
            btnPlay.setOnClickListener(this);
        }
    
        public void onDestroy() {
    
            super.onDestroy();
            if((mRecoder != null) && (soundFile !=null) && (soundFile.exists())) {
                mRecoder.stop();
                mRecoder.release();
                mRecoder = null;
            }
    
        }
    
        @Override
        public void onClick(View v)
        {
            switch(v.getId())
            {
                case R.id.start:
                    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                    {
                        Toast.makeText(MainActivity.this,"check SD Card Faild!",Toast.LENGTH_SHORT).show();
                        return;
                    }
    
                    try {
                        soundFile = new File("sdcard/sound.amr");
                        mRecoder = new MediaRecorder();
                        mRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mRecoder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                        mRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecoder.setOutputFile(soundFile.getAbsolutePath());
                        mRecoder.prepare();
                        mRecoder.start();
                        showRecordTime();
                        Log.d(TAG, "start");
                        Toast.makeText(MainActivity.this,"start Recording!",Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.stop:
                    if(soundFile != null && soundFile.exists())
                    {
                        mRecoder.stop();
                        mRecoder.release();
                        mRecoder = null;
                        Log.d(TAG, "stop");
                        Toast.makeText(MainActivity.this,"Stop Recording!",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.play:
                {
                    Log.d(TAG,btnPlay.getText().toString());
                    if (btnPlay.getText().toString().equalsIgnoreCase("Play") ) {
                        Log.d(TAG,"play@@@@@@@");
                        mPlayer = new MediaPlayer();
                        try {
                            mPlayer.setDataSource("sdcard/sound.amr");
                            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                            mPlayer.prepare();
                            mPlayer.start();
                            btnPlay.setText("Stop");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else if (btnPlay.getText().toString().equalsIgnoreCase("Stop") )
                    {
                        if(mPlayer != null)
                        {
                            mPlayer.stop();
                            mPlayer.release();
                            btnPlay.setText("Play");
                        }
                    }
                }
                break;
            }
    
        }
    
        private void showRecordTime() {
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int time_sec = 0;
                    while(mRecoder != null)
                    {
                        try {
                            Thread.sleep(1000);
                            Message message = new Message();
                            time_sec +=1;
                            message.what = time_sec ;
                            handler.sendMessage(message);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
    
                }
            }).start();
    
        }
    }

    2.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="center"
            android:textSize="40dip"
            android:textColor="#000000"
            android:id="@+id/txtTime"/>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="1dp">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/start"
                android:text="start"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/stop"
                android:text="stop"/>
        </LinearLayout>
    
        <Button
            android:text="play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/play" />
    
    
    </LinearLayout>

    3.AndroidMainfest.xml添加权限

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 相关阅读:
    再学 GDI+[91]: TGPImage(11) 转灰度图像
    再学 GDI+[90]: TGPImage(10) 获取图像的调色板信息
    给 Memo 排序的函数
    再学 GDI+[97]: TGPImage(17) 获取 GDI+ 所支持的可编码、可解码的图像格式
    再学 GDI+[94]: TGPImage(14) 增减图像的红、绿、蓝三色的成分
    上周热点回顾(10.2611.1)
    博客园上海俱乐部Windows 7社区发布活动的奖品
    顶吧!顶出今日头条
    博客园电子期刊2009年10月刊发布啦
    对于近期社区问题的一点想法
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6198570.html
Copyright © 2011-2022 走看看