2011.08.12(3)-android AudioTrack 오디 오 재생

4174 단어 android
2011.08.12(3)-android AudioTrack 오디 오 재생
참고:
http://blog.sina.com.cn/s/blog_74b752870100qxrv.html
http://crazier9527.iteye.com/blog/467061
http://www.cnblogs.com/mcodec/articles/1933741.html
mp3 를 재생 하 는 것 은 항상 소음 입 니 다.원인 을 모 르 고 나중에 wav 를 재생 하 는 것 은 괜 찮 습 니 다.
wav 는 압축 되 지 않 은 pcm 입 니 다.
package com.lp;


import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.AudioTrack.OnPlaybackPositionUpdateListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button play;
	private Button stop;
	
	private AudioTrack at;
	private AudioManager am;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        play = (Button)findViewById(R.id.main_play);
        stop = (Button)findViewById(R.id.main_stop);
        play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(am.isSpeakerphoneOn()){
					am.setSpeakerphoneOn(false);
				}
				setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
				am.setMode(AudioManager.MODE_IN_CALL);
				System.out.println(am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
				System.out.println("&&&&&&&&&&&&&");
				System.out.println(am.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
				
				int bufferSizeInBytes = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
				
				if(at==null){
					at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
					System.out.println("22222");
					new AudioTrackThread().start();
				}else{
					if(at.getPlayState()==AudioTrack.PLAYSTATE_PLAYING){
						System.out.println("111111111");
					}else{
						System.out.println("33333");
						at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
						new AudioTrackThread().start();
					}
				}
				
			}
        });
        
        stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				at.stop();
				at.release();
				am.setMode(AudioManager.MODE_NORMAL);
			}
		});
    }
    
    class AudioTrackThread extends Thread{

		@Override
		public void run() {
			byte[] out_bytes = new byte[44100];
			
			InputStream is = getResources().openRawResource(R.raw.start);    
			int length ;
			at.play();
			try {
				while((length = is.read(out_bytes))!=-1){
					System.out.println(length);
					at.write(out_bytes, 0, length);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(at.getPlayState()==AudioTrack.PLAYSTATE_PLAYING){
				at.stop();
				at.release();
				am.setMode(AudioManager.MODE_NORMAL);
			}
		}
    	
    }
				
}

좋은 웹페이지 즐겨찾기