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

android AudioTrack不能播放awr

我用AudioTrack播放MP3 awr都是不成功的 只会传出一堆的噪声,但是播放wav是没有问题的,因为wav就相当于原生态的pcm

 

这时候 我有两个选择

 

1、从3gp网站上下载awr解码算法 用jni调用 把awr解码成pcm

2、可以用AudioRecord来录制pcm 并用AudioTrack来播放

 

我用的时后者

Java代码

package com.lp; 

 

 

import java.io.BufferedInputStream; 

import java.io.BufferedOutputStream; 

import java.io.DataInputStream; 

import java.io.DataOutputStream; 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import android.app.Activity; 

import android.media.AudioFormat; 

import android.media.AudioManager; 

import android.media.AudioRecord; 

import android.media.AudioTrack; 

import android.media.MediaRecorder; 

import android.os.AsyncTask; 

import android.os.Bundle; 

import android.os.Environment; 

import android.util.Log; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.TextView; 

 

public class MainActivity extends Activity implements OnClickListener{ 

     

    private TextView stateView; 

     

    private Button btnStart,btnStop,btnPlay,btnFinish; 

     

    private RecordTask recorder; 

    private PlayTask player; 

     

    private File audioFile; 

     

    private boolean isRecording=true, isPlaying=false;  

     

    private int frequence = 44100;  

    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO; 

    private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 

     

     

    public void onCreate(Bundle savedInstanceState){ 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main_pcm); 

         

        stateView = (TextView)this.findViewById(R.id.view_state); 

        stateView.setText("准备开始"); 

        btnStart = (Button)this.findViewById(R.id.btn_start); 

        btnStop = (Button)this.findViewById(R.id.btn_stop); 

        btnPlay = (Button)this.findViewById(R.id.btn_play); 

        btnFinish = (Button)this.findViewById(R.id.btn_finish); 

        btnFinish.setText("停止播放"); 

        btnPlay.setEnabled(false); 

        btnFinish.setEnabled(false); 

        btnStart.setOnClickListener(this); 

        btnStop.setOnClickListener(this); 

        btnPlay.setOnClickListener(this); 

        btnFinish.setOnClickListener(this); 

         

//      File fpath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/11"); 

//      fpath.mkdirs(); 

        try { 

            //audioFile = File.createTempFile("test", ".pcm", fpath); 

            audioFile = new File("/mnt/sdcard/1.pcm"); 

        } catch (Exception e) { 

            // TODO Auto-generated catch block 

            e.printStackTrace(); 

        }        

    } 

     

     

    public void onClick(View v){ 

        int id = v.getId(); 

        switch(id){ 

        case R.id.btn_start: 

             

            recorder = new RecordTask(); 

            recorder.execute(); 

             

            break; 

        case R.id.btn_stop: 

            this.isRecording = false; 

            System.out.println(isRecording); 

            break; 

        case R.id.btn_play: 

             

            player = new PlayTask(); 

            player.execute(); 

            break; 

        case R.id.btn_finish: 

            this.isPlaying = false; 

            break; 

             

        } 

    } 

     

    class RecordTask extends AsyncTask<Void, Integer, Void>{ 

        @Override 

        protected Void doInBackground(Void... arg0) { 

            //isRecording = true; 

            try { 

           

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