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

JNI在Android开发中的应用之--用AudioRecord录音,用AudioTrack播放声

[java]
下面是主Activity. 
 
package com.example.hellojni; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
 
 
public class HelloJni extends Activity 

 
    Button startRecord; 
    Button stopRecord; 
    Button play; 
    static BufferedOutputStream bos; 
 
    static { 
        System.loadLibrary("hello-jni"); 
    } 
 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.main); 
 
        startRecord = (Button)findViewById(R.id.start); 
        stopRecord = (Button)findViewById(R.id.stop); 
        play  =(Button)findViewById(R.id.play); 
 
 
        startRecord.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                new Thread(){ 
                    public void run(){ 
 
                        initOutputStream(); 
 
                        startRecord(); 
                    } 
                }.start(); 
 
            } 
        }); 
 
 
        stopRecord.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                new Thread(){ 
                    public void run(){ 
                        stopRecord(); 
 
                        try{ 
                            Thread.sleep(1000*2); 
                            if(bos!=null){ 
                                try { 
                                    bos.close(); 
                                } catch (IOException e) { 
                                    // TODO Auto-generated catch block 
                                    e.printStackTrace(); 
                                } 
                            } 
                        }catch(Exception e){ 
 
                        } 
                    } 
                }.start(); 
 
            } 
        }); 
 
        play.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                new Thread(){public void run(){play();}}.start(); 
            } 
        }); 
 
    } 
 
 
    public void initOutputStream(){ 
            File file = new File("/sdcard/temp.pcm"); 
            try { 
                bos = new BufferedOutputStream(new FileOutputStream(file)); 
           

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