关于android 进程CPU占用率高的原因分析

*记录开发中的问题,方便以后自已回忆,2017-07-17 记录
在开发一个语音的线程中,发现有如下事情,系统越来越慢,也越来越卡,打到
adb shell 输 入 top 查看系统资源,发现 CPU占用率节节升高.我的去。*
最终发现是线程没有关闭,而不停的创建新的线程 ,

private class TrackThred extends Thread {

    private byte[] TTSResult;
    private volatile boolean stop = false;
    private List<byte[]> bytes = new ArrayList<>();
    private boolean isTrack = true;
    private void setTTS(byte[] ttsResult) {
        bytes.add(ttsResult);
    }

    @Override
    public void run() {
        super.run();
        audioTrack.play();
        //audioTrack.getPlaybackHeadPosition();
        int i = 0;
        **while (isTrack&&!stop) {**  //这个地方没有加控制开关,在重新 new TrackThred 时发现多次创建的问题
            if(i < bytes.size()-1) {
                TTSResult = bytes.get(i);
                audioTrack.write(TTSResult, 0, TTSResult.length);
                i++;
            }
        }
    }

    public void onStop() {
        isTrack = false;
        audioTrack.stop();

    }

}

130|root@MagicBox_M16S:/ # top | grep “tvtaobao”
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 0% S 86 1646844K 108816K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 3% S 86 1646780K 109228K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 16% R 104 1703404K 152892K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 2 18% S 109 1730100K 183492K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 2% S 112 1722144K 172400K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 3% S 112 1722784K 169596K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 11% S 115 1727064K 155580K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47760K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 6% S 121 1718892K 166352K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 11% S 124 1768548K 190136K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 0% S 124 1768548K 189696K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 124 1768548K 189916K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 0% S 124 1768612K 190068K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 0% S 124 1768776K 190020K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 124 1768776K 190096K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 4% S 124 1768740K 190064K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 16% S 125 1769604K 193096K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 13% S 125 1769644K 193108K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 13% S 125 1769876K 193340K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769876K 193340K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769876K 193276K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769684K 193056K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 23% S 124 1768628K 193192K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 28% S 124 1768628K 196344K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 28% S 124 1768820K 197860K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 25% S 124 1768820K 197880K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 25% S 122 1766516K 197704K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 25% S 122 1766556K 197704K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 25% S 119 1763452K 198348K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 31% S 119 1763580K 198468K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 1 40% S 120 1765848K 207696K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 39% S 120 1764440K 202820K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 37% S 120 1764440K 199952K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 36% S 120 1764248K 199964K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 37% S 120 1764312K 200072K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 37% S 120 1764440K 200168K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 43% S 120 1764440K 200136K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 50% S 120 1767884K 205048K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 52% S 120 1764288K 202728K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 49% S 120 1764288K 202660K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 48% S 120 1764288K 202944K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 50% S 120 1764352K 202820K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 50% S 120 1764480K 202956K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 52% S 120 1764480K 202924K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 63% R 121 1769292K 205708K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 68% S 121 1765536K 202396K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 61% S 121 1765384K 202184K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765384K 202144K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765384K 202156K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 62% S 121 1765448K 202188K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765612K 202316K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 75% S 122 1770352K 207652K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 76% R 121 1765384K 204012K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 73% S 122 1766552K 204028K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 71% S 122 1766552K 203972K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 74% S 122 1766552K 201912K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 83% S 122 1766400K 201744K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 83% S 122 1766400K 201744K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767520K 201604K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 84% S 123 1767456K 202580K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767456K 202520K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 84% S 123 1767456K 203044K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767456K 203056K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 80% S 123 1767520K 203088K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 85% S 123 1767456K 202812K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 1 83% S 124 1768552K 205384K fg u0_a121 com.yunos.tvtaobao
^C
package com.yunos.tvtaobao.activity.AudioDemo;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Handler;
import android.widget.Toast;

import com.alibaba.idst.nls.NlsClient;
import com.alibaba.idst.nls.NlsListener;
import com.alibaba.idst.nls.internal.protocol.NlsRequest;
import com.alibaba.idst.nls.internal.protocol.NlsRequestProto;
import com.yunos.juhuasuan.common.Log;
import com.yunos.tv.lib.LOG;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

/**
* Created by yugangjiang on 2017/7/7.
*/

public class Nlscontent {
private static final String TAG = “Nlscontent”;
private TrackThred thred;
private NlsClient mNlsClient;
private NlsRequest mNlsRequest;
private static String str_tts;
private Context context;
public Handler m_handler;
public List m_list = new ArrayList<>();
public static String current_key =”“;
public static int current_pos = 0 ;
public static int audioTrack_pos = 0 ;
public static int audioTrack_pos_count = 0;
//定义
public Boolean TTS_open = false;
public Boolean is_replay_true = false;
int iMinBufSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,
iMinBufSize, AudioTrack.MODE_STREAM) ; //使用audioTrack播放返回的pcm数据

private static Nlscontent nlscontent = null;

public static Nlscontent getInstance() {
    if (nlscontent == null) {
        synchronized (Nlscontent.class) {
            if (nlscontent == null)
                nlscontent = new Nlscontent();
        }
    }

    return nlscontent;
}

public void getStrList(String inputString, int size,List<String> list) {
    if(inputString.length() < size)
    {
        list.add(inputString);
    }else
    {
        list.add(inputString.substring(0,size));
        getStrList(inputString.substring(size,inputString.length()),size,list);
    }
    //return getStrList(inputString, size);
}

public  void init(Context context,Handler m_handler)
{
    this.context = context;
    this.m_handler = m_handler;
    TTS_open = true;
    mNlsRequest = initNlsRequest();
    String appkey = "nls-service-streaming";     //请设置简介页面的Appkey
    mNlsRequest.setApp_key(appkey);    //appkey请从 简介页面的appkey列表中获取
    mNlsRequest.initTts();               //初始化tts请求
    NlsClient.openLog(true);
    NlsClient.configure(context.getApplicationContext()); //全局配置
    mNlsClient = NlsClient.newInstance(context, mRecognizeListener, null ,mNlsRequest);
}

//重播
public  Runnable replay_Run = new Runnable() {
    @Override
    public void run() {
        if(is_replay_true) {
            play_String(current_key);
        }
    }
};




private NlsRequest initNlsRequest() {
    NlsRequestProto proto = new NlsRequestProto();
    proto.setApp_user_id("user_id"); //设置用户名
    return new NlsRequest(proto);
}


public void play_String(String m_TTs)
{
    TTS_open = true;
    mNlsRequest.setTtsEncodeType("pcm"); //返回语音数据格式,支持pcm,wav.alaw
    mNlsRequest.setTtsVolume(100);   //音量大小默认50,阈值0-100
    mNlsRequest.setTtsSpeechRate(-100);//语速,阈值-500~500
    mNlsClient.PostTtsRequest(m_TTs); //用户输入文本
    Log.d(TAG,"playTts ===" + m_TTs + "   length: " + m_TTs.length());
    str_tts = m_TTs;
    current_key = m_TTs;
    mNlsRequest.authorize("lbxirhq7cO1Gc2dG", "terZhCBs8Wv8Gswx1pHevS7zwEN9N2");      //请替换为用户申请到的数加认证key和密钥
}
public void playTts(String user_input){
    if(user_input.length()<=0)
    {
        return;
    }
    current_pos = 0 ;
    m_list.clear();
    getStrList(user_input,200,m_list);
    play_String(m_list.get(0));
}
public  Runnable play_run = new Runnable() {
    @Override
    public void run() {
            //如果第一次要播放
          if(audioTrack!=null)
          {

             if(audioTrack.getPlayState()==AudioTrack.PLAYSTATE_STOPPED)
             {
                 audioTrack.setPlaybackHeadPosition(0);
             }
          }
    }
};
private void destroyThread(TrackThred thread) {
    try {
        if (null != thread && Thread.State.RUNNABLE == thread .getState()) {
            try {
                Thread.sleep(500);
                thread.interrupt();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        BlockingQueue bq;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        thread = null;
    }
}

public int getAudioPosition()
{
    if(audioTrack !=null)
    {
        return audioTrack.getPlaybackHeadPosition();
    }
    return  -1;

}
public void stopTTS() {
    Log.d(TAG,"stopTTS ===");
    if (thred != null && thred.isAlive()) {
        thred.onStop();
        thred = null;
    }
}
private NlsListener mRecognizeListener = new NlsListener() {
    @Override
    public void onTtsResult(int status, byte[] ttsResult){
        Log.w(TAG, "NlsListener onTtsResult status : " + status);
        switch (status) {
            case NlsClient.ErrorCode.TTS_BEGIN :
                if(thred!=null)
                    thred.stop = true;
                thred = new TrackThred();
                Log.e(TAG, "tts begin");
                break;
            case NlsClient.ErrorCode.TTS_TRANSFERRING :
                Log.e(TAG,"tts transferring"+ttsResult.length);
                thred.setTTS(ttsResult);
                break;
            case NlsClient.ErrorCode.TTS_OVER :
                Log.e(TAG,"tts over");

// Nlscontent.getInstance().m_handler.post(m_Runnable);
thred.start();
is_replay_true = false; //走到这个地方了一定是播放成功了
// initNlsRequest();
break;
case NlsClient.ErrorCode.CONNECT_ERROR :
Log.e(TAG,”tts error”);
break;
case NlsClient.ErrorCode.SERVER_HANDLING_ERROR:
Log.e(TAG,”network—-500”);
break;
}
}

    public void onRecognizingResult(int status, NlsListener.RecognizedResult result) {
        Log.d(TAG," onRecognizingResult  status==  " + status + " result : "+ result);
        if((status == 1 || status == 500 || status == 530 || status ==408) && (result == null))
        {
            //如果因为sockert的原因,没有播成功,就重播一下...
            is_replay_true = true;
            m_handler.postDelayed(replay_Run,1000);
        }
    }

    public void onServiceStatChanged(boolean isStreamAvailable, boolean isRpcAvailable) {
        Log.d(TAG," onServiceStatChanged isStreamAvailable : "  + isStreamAvailable + " isRpcAvailable : "+ isRpcAvailable);
    }
};

public int getTTSState() {
    return thred.getState().ordinal();
}

private class TrackThred extends Thread {

    private byte[] TTSResult;
    private volatile boolean stop = false;
    private List<byte[]> bytes = new ArrayList<>();
    private boolean isTrack = true;
    private void setTTS(byte[] ttsResult) {
        bytes.add(ttsResult);
    }

    @Override
    public void run() {
        super.run();
        audioTrack.play();
        //audioTrack.getPlaybackHeadPosition();
        int i = 0;
        while (isTrack&&!stop) {
            if(i < bytes.size()-1) {
                TTSResult = bytes.get(i);
                audioTrack.write(TTSResult, 0, TTSResult.length);
                i++;
            }
        }
    }

    public void onStop() {
        isTrack = false;
        audioTrack.stop();

    }

}

}

发布了4 篇原创文章 · 获赞 0 · 访问量 6807
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览