博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android -- AudioPlayer
阅读量:5886 次
发布时间:2019-06-19

本文共 3499 字,大约阅读时间需要 11 分钟。

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
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(); }}

截图

122209154896420.jpg

我是天王盖地虎的分割线

转载地址:http://cmrix.baihongyu.com/

你可能感兴趣的文章
SQLServer 批量备份与还原
查看>>
51Nod 1010 只包含因子2 3 5的数 Label:None
查看>>
Java中String和byte[]间的转换浅析
查看>>
什么是异步
查看>>
WordPress 主题切换
查看>>
cookie和session
查看>>
【java】path和classpath
查看>>
UVa 10057 - A mid-summer night's dream
查看>>
解决3 字节的 UTF-8 序列的字节 3 无效
查看>>
浅谈浏览器兼容性问题-(1)产生、看待与思
查看>>
iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)...
查看>>
BeanUtils\DBUtils
查看>>
VC 创建托盘,托盘tooltip。右键托盘菜单,点击别的地方会隐藏掉的问题。
查看>>
第一天,新的定义
查看>>
WPF EventSetter Handler Command
查看>>
polya定理,环形涂色
查看>>
day4-装饰器前奏
查看>>
【Jest】笔记三:全局变量
查看>>
forward和redirect的区别
查看>>
使用JavaMail完成邮件的编写
查看>>