zl程序教程

您现在的位置是:首页 >  APP

当前栏目

[android] 采用服务录制电话&服务的生命周期

2023-02-18 15:50:04 时间

根据上一节代码里,加入一个录音功能,上传到服务器,就能实现一个录制器

当手机处于通话状态时,开启录音机

获取MediaRecorder对象,通过new出来

调用MediaRecorder对象的setAudioSource()方法,设置音频源,

参数:MediaRecorder.AudioSource.MIC,参数是麦克风,默认只支持单向录音

调用MediaRecorder对象的setOutputFormat(),设置输出格式,

参数:MediaRecorder.OutputFormat.THREE_GPP

调用MediaRecorder对象的setAudoEncoder()方法,设置音频编码

参数:MediaRecorder.AudioEncoder.AMR_NB

调用MediaRecorder对象的setOutputFile(path)方法,设置文件保存路径

参数:”/sdcard/”+System.currentTimeMilis+”.3gp”

 

调用MediaRecorder对象的prepare()方法,准备

调用MediaRecorder对象的start()方法,开始

调用MediaRecorder对象的stop()方法,停止

调用MediaRecorder对象的reset()方法,重置

调用MediaRecorder对象的release()方法,释放对象

 

需要权限android.permission.RECORD_AUDIO

需要写文件android.permission.WRITE_EXTERNAL_STORGE

 

开启服务

获取Intent对象,new Intent(this,PhoneService.class),参数:上下文,字节码

调用startService(intent)方法,参数:Intent对象

关闭服务

获取Intent对象,new Intent(this,PhoneService.class),参数:上下文,字节码

调用stopService(intent)方法,参数:Intent对象

在应用管理器手工停止服务

 

服务放生命周期

onCreate()  ==> onStartCommand() ==> onStart() ==> onDestory()

服务只会被执行一次,如果多次调用,会从onStartCommand()开始运行

 

MainActivity.java(主界面)

 

package com.tsh.listentel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    // 开启服务
    public void start(View v) {
        Intent intent = new Intent(this, PhoneService.class);
        startService(intent);
    }

    // 关闭服务
    public void stop(View v) {
        Intent intent = new Intent(this, PhoneService.class);
        stopService(intent);
    }
}

 

PhoneService.java(监听服务)

 

package com.tsh.listentel;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    // 服务创建
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("服务创建");
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        tm.listen(new MyPhoneStateListener(),
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    // 内部类
    private class MyPhoneStateListener extends PhoneStateListener {
        private MediaRecorder recorder;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            try {
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("空闲状态");
                    //关闭
                    if (recorder != null) {
                        recorder.stop();
                        recorder.reset();
                        recorder.release();
                    }

                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("响铃状态");
                    //准备
                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile("/sdcard/"
                            + System.currentTimeMillis() + ".3gp");
                    recorder.prepare();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("通话状态");
                    //开录
                    if (recorder != null) {
                        recorder.start();
                    }
                    break;
                default:
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    // 服务销毁
    @Override
    public void onDestroy() {
        System.out.println("服务销毁");
        super.onDestroy();
    }

}