Android 听筒播放模式实现代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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