Android 听筒播放模式实现代码
2018-07-20 来源:open-open
package com.example.media; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AAA extends Activity { private Button play;//播放 private Button stop;//停止 private AudioManager am;//提供访问控制音量和钤声模式的操作 private boolean isReleased = false; private boolean isPlaying = false; private MediaPlayer mMediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (Button) findViewById(R.id.main_play); stop = (Button) findViewById(R.id.main_stop); play.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //听筒模式下设置为false am.setSpeakerphoneOn(false); //设置成听筒模式 am.setMode(AudioManager.MODE_IN_CALL); //设置为通话状态 setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); try { if (!isPlaying) { mMediaPlayer = MediaPlayer.create(AAA.this, R.raw.man); isPlaying = true; isReleased = false; mMediaPlayer.setLooping(true); try { mMediaPlayer.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mMediaPlayer.start(); } } catch (IllegalStateException e) { e.printStackTrace(); } //播放完毕监听 mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer arg0) { mMediaPlayer.release(); //设置回正常模式 am.setMode(AudioManager.MODE_NORMAL); } }); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isPlaying && !isReleased) { isReleased = true; mMediaPlayer.stop(); mMediaPlayer.release(); isPlaying = false; //停止播放设置为正常状态 am.setMode(AudioManager.MODE_NORMAL); } } }); } }
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:java获取系统信息
最新资讯
热门推荐