当前位置:操作系统 > 安卓/Android >>

android AudioTrack 播放音频

播放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]; 

             

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,