Android -- AudioPlayer

简介:

AudioPlayer

主要是实现边录边播(AudioRecord+AudioTrack)以及对音频的实时处理(如会说话的汤姆猫、语音)

优点:语音的实时处理,可以用代码实现各种音频的封装

缺点:输出是PCM语音数据,如果保存成音频文件,是不能够被播放器播放的,所以必须先写代码实现数据编码以及压缩

示例:

使用AudioRecord类录音,并实现WAV格式封装。录音20s,输出的音频文件大概为3.5M左右(已写测试代码)

Demo

public class SoundActivity extends Activity implements  AdapterView.OnItemClickListener
        , MediaPlayer.OnCompletionListener {
  public static final String DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "1122" + File.separator;

  private ListView mListView;
  private SoundAdapter mAdapter;
  private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);
        mListView = (ListView) findViewById(R.id.lv_sound);
        mListView.setOnItemClickListener(this);
        initListView();
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnCompletionListener(this);
    }

    private void initListView() {
        mAdapter = new SoundAdapter(SoundActivity.this, DIR);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String fileName = (String) mAdapter.getItem(position);
        String file = DIR + fileName;

        try {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(file);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mMediaPlayer.reset();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaPlayer.release();
    }
public class SoundAdapter extends BaseAdapter {
    private Context mContext;

    private File mDir;
    private int mTotalNumber = 0;
    private List<String> mFileNameList;

    public SoundAdapter(Context context, String dirPath) {
        mContext = context;
        mDir = new File(dirPath);
        mTotalNumber = mDir.list().length;
        mFileNameList = Arrays.asList(mDir.list());
    }

    @Override
    public int getCount() {
        return mTotalNumber;
    }

    @Override
    public Object getItem(int position) {
        return mFileNameList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            vh.textView = (TextView) convertView.findViewById(R.id.text_view);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.textView.setText(mFileNameList.get(position));
        return convertView;
    }

    class ViewHolder {
        TextView textView;
    }

    @Override
    public void notifyDataSetChanged() {
        mTotalNumber = mDir.list().length;
        mFileNameList = Arrays.asList(mDir.list());
        super.notifyDataSetChanged();
    }
}

截图




本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/4725728.html,如需转载请自行联系原作者
相关文章
|
数据库 Android开发 数据库管理
|
物联网 Android开发
|
传感器 Java Android开发
|
Android开发 数据可视化
|
物联网 Android开发