我们知道iPhone录制的音频都是caf格式,Android的Mediaplayer不支持这个格式的播放..那么怎么办? 我们使用AudioTrack来播放!!代码如下:


import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.media.MediaPlayer;import android.media.AudioManager;import android.media.AudioTrack;import android.media.AudioFormat;public class LRChannel extends Activity {    private Button lButton;    private Button rButton;    private TextView myTextView;    private MediaPlayer mMediaPlayer01 = null;    private AudioTrack aAudioTrack01 = null;    private String strFilePath = "/sdcard/test.caf";    private float midVol = 0;       /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                lButton = (Button) findViewById(R.id.ButtonL);        rButton = (Button) findViewById(R.id.ButtonR);        myTextView = (TextView) findViewById(R.id.myTextView);                // left button response        lButton.setOnClickListener(new Button.OnClickListener()        {          @Override          public void onClick(View arg0)          {                playSound(strFilePath, 0);          }        });                // right button response        rButton.setOnClickListener(new Button.OnClickListener()        {          @Override          public void onClick(View arg0)          {              playSound(strFilePath, 1);          }        });    }       // iChannel = 0 means left channel test, iChannel = 1 means right channel test.    private void playSound(String strPath, int iChannel)    {      // If now is playing...      if ( aAudioTrack01 != null )      {          aAudioTrack01.release();          aAudioTrack01 = null;      }      // Get the AudioTrack minimum buffer size      int iMinBufSize = AudioTrack.getMinBufferSize(44100,                          AudioFormat.CHANNEL_CONFIGURATION_STEREO,                          AudioFormat.ENCODING_PCM_16BIT);      if ( iMinBufSize == AudioTrack.ERROR_BAD_VALUE || iMinBufSize == AudioTrack.ERROR )      {          return;      }      // Constructor a AudioTrack object      try      {          aAudioTrack01 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,                    AudioFormat.CHANNEL_CONFIGURATION_STEREO,                    AudioFormat.ENCODING_PCM_16BIT,                    iMinBufSize,                    AudioTrack.MODE_STREAM);      }      catch (IllegalArgumentException iae)      {          myTextView.setText("new AudioTrack Exceeption:" + iae.toString());          iae.printStackTrace();      }      // Write data to buffer      byte data[] = new byte[iMinBufSize];      aAudioTrack01.write(data, 0, data.length);      aAudioTrack01.write(data, 0, data.length);      float lValue = 0;      float rValue = 0;            if ( iChannel == 0 )      {          lValue = 1.0f;          rValue = 0.0f;      }      else if ( iChannel == 1 )      {          lValue = 0.0f;          rValue = 1.0f;      }            aAudioTrack01.play();      if ( aAudioTrack01.setStereoVolume(lValue, rValue) == AudioTrack.SUCCESS )      {          myTextView.setText("setStereoVolume successfully!");      }      aAudioTrack01.stop();      if ( aAudioTrack01.setStereoVolume(midVol, midVol) == AudioTrack.SUCCESS )      {          myTextView.setText("Restore setStereoVolume successfully!");      }      aAudioTrack01.release();      aAudioTrack01 = null;    }       @Override    protected void onDestroy() {        super.onDestroy();        // TODO Auto-generated method stub        if ( aAudioTrack01 != null ) {            aAudioTrack01.setStereoVolume(midVol, midVol);            aAudioTrack01.release();            aAudioTrack01 = null;        }    }   }

        /**         * Play the sound by AudioTrack.         *          * @param soundData         *            The sound which is to be played.         */        private void playSound(byte[] soundData) {            int index = 0;            int offset = 0;            // 8000 byte every second            final int minBufsize = AudioTrack.getMinBufferSize(8000,                    AudioFormat.CHANNEL_OUT_STEREO,// The channel                    AudioFormat.ENCODING_PCM_16BIT);            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,                    8000,                    AudioFormat.CHANNEL_OUT_STEREO,                    AudioFormat.ENCODING_PCM_16BIT,                    minBufsize,                    AudioTrack.MODE_STREAM);            mAudioTrack.play();            while (true) {                try {                    offset = index * minBufsize;                    if (offset >= soundData.length) {                        break;                    }                    publishProgress(offset);                    mAudioTrack.write(soundData, offset, minBufsize);                } catch (Exception e) {                    break;                }                index++;            }            mAudioTrack.release();        }


参考:

http://www.cnblogs.com/innost/archive/2011/01/09/1931457.html

http://crazier9527.iteye.com/blog/467061


更多相关文章

  1. android音频处理
  2. Android音频开发之AudioTrack
  3. Android色彩空间像素格式定义及设定
  4. Android SimapleDateFormat 日期格式的转换方法
  5. 修改android日期格式
  6. Android中URI的格式
  7. android音频基础与SoundPool
  8. Android多媒体应用使用MediaPlayer播放音频

随机推荐

  1. Android判断权限被限制
  2. UIAutomator遇到的坑
  3. 五、Fresco——ScaleType focusCrop属性
  4. android 显示电池电量
  5. 终极报错解决方案:Error:Execution failed
  6. Android获取手机SIM卡运营商信息的方法
  7. Android的showDialog()、onPrepareDialog
  8. Android(安卓)设备侧usb驱动开发
  9. android实现倒计时功能的方法
  10. Android(安卓)Gradle 插件版本说明 以及