android AudioTrack 播放音频

来源:未知 责任编辑:智问网络 发表时间:2013-10-22 20:07 点击:

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

            } 

        } 

         

    } 

                 

}   

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Android 完全退出程序
    • 原创:Android应用开发-Andorid歌词秀,含源码
    • android 屏幕保护
    • Android手机软件汉化教程---第四课 dex文件汉化
    • 众多Android 开源项目推荐,给力工作给力学习
    • Android Audio代码分析4
    • Android得到已安装的应用程序信息!
    • Android开发者指南(29) —— USB Host and Accessory
    • Android成长的幕后推手:工程师鲁宾
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1