android AudioTrack 播放音頻 – Android移動開發技術文章_手機開發 Android移動開發教學課程

播放mp3的總是噪音,不知道原因,後來播放wav的倒是可以,

wav是沒有壓縮的pcm

 

Java代碼

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

            } 

        } 

         

    } 

                 

}   

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。