0

正如问题所提到的,我正在重新使用相同的 Intent 对象,用不同的附加数据重新加载它,然后在 startService 中使用它。这个可以吗?还是我需要在每个 startService 之前执行“new Intent(context, ServiceClass)”?

我正在使用服务进行连续的处理流,所以如果每次需要将数据发送到服务时都必须创建一个新的 Intent,我会有点担心。

问题是目前,服务似乎只执行一次。所有后续的 startService 调用似乎都不会触发服务。可能是什么原因?

编辑:添加代码

这是主要活动:

public class Measurement extends Activity{

    AudioRecord recorder;   
    int iAudioBufferSize;
    boolean bRecording; 
    int iBytesRead;


    // UI stuff 
    Button bt_Measure;
    Button bt_Stop; 
    RadioButton rd_Live;
    RadioButton rd_DataCollection;
    RadioButton rd_SampleCollection;
    Thread recordThread;
    Intent sampleDataIntent;


    @Override
    public void onDestroy() {       
    super.onDestroy();
    if (recorder != null)
        recorder.release();     
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt_Measure = (Button) findViewById(R.id.bt_measure);
    bt_Measure.setEnabled(true);
    bt_Stop = (Button) findViewById(R.id.bt_stop);
    rd_Live = (RadioButton) findViewById(R.id.rd_live);
    rd_DataCollection = (RadioButton) findViewById(R.id.rd_datacollection);
    rd_SampleCollection = (RadioButton) findViewById(R.id.rd_samplecollection);
    rd_Live.setChecked(true);

    bRecording = false;
    int iSampleRate = AudioTrack
            .getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    iAudioBufferSize = AudioRecord.getMinBufferSize(iSampleRate,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    recorder = new AudioRecord(AudioSource.MIC, iSampleRate,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
            iAudioBufferSize);
    sampleDataIntent = new Intent(this, DetectionService.class);


    bt_Measure.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (rd_Live.isChecked()){ // radio button for Live processing
                if (!bRecording) {                  
                    bt_Measure.setEnabled(false);
                    bt_Stop.setEnabled(true);
                    try {
                        recorder.startRecording();
                        bRecording = true;

                        recordThread = new Thread() {
                            @Override
                            public void run() {
                                byte[] buffer = new byte[iAudioBufferSize];                                 
                                int iBufferReadResult;
                                int iBytesToRead = iAudioBufferSize;
                                iBytesRead = 0;
                                while (!interrupted()) {
                                    iBufferReadResult = recorder.read(buffer,
                                            0, iBytesToRead);                                                                                   
                                    sampleDataIntent.removeExtra("THE_DATA");
                                    sampleDataIntent.putExtra("THE_DATA", buffer);
                                    sampleDataIntent.putExtra("FRAME_SIZE", iBufferReadResult);
                                    startService(sampleDataIntent);                                                                                 
                                    iBytesRead = iBytesRead + iBufferReadResult;
                                }
                            }
                        };
                        recordThread.start();
                    } catch (Exception e) {
                        tv_Speed.setText(e.getLocalizedMessage());
                    }
                }
            }
        }
    });
    }
}

这是检测服务:

public class DetectionService extends IntentService {


ByteArrayOutputStream baos;
byte[] sampleBuffer;
byte[] lastSampleBuffer;
PeakFilter peakFilter1;
PeakFilter peakFilter2;
PeakFilter peakFilter3;
PeakFilter peakFilter4;

float fSum = 0;
short sAvgAmpl = 0;
short sMaxAvg = 0;
short sPeakAmpl = 0;


public DetectionService() {
    super("DetectionService");
    baos = new ByteArrayOutputStream();
    peakFilter1 = new PeakFilter(260, 20, 44100);
    peakFilter2 = new PeakFilter(260, 20, 44100);
    peakFilter3 = new PeakFilter(260, 20, 44100);
    peakFilter4 = new PeakFilter(260, 20, 44100);
}


@Override
protected void onHandleIntent(Intent sampleDataIntent) {
    try {
        baos.write(sampleDataIntent.getByteArrayExtra("THE_DATA"));
    } catch (IOException e1) {          
        e1.printStackTrace();
    }
    if (baos.size() > 44100 * 5) // 5 second frame
        sampleBuffer = baos.toByteArray();
    else
        return;

    try {

            // NON THREAD IMPEMENTATION
            // Threaded implementation has the same Try..Catch executed in
            // separate thread. Neither seems to be running more than once.

        short[] preFilterBuffer = new short[sampleBuffer.length/2];
        int iSample = 0;

        for (int i = 0, j = 0; i < preFilterBuffer.length; i++, j+=2){                          
            preFilterBuffer[iSample] = (short) (sampleBuffer[j+1] << 8 | sampleBuffer[j]);
            iSample++;
        }

        short[] FilteredSamples = new short[preFilterBuffer.length];            
        // Filters are not spawning threads. Purely Math processing.
        peakFilter1.filter(FilteredSamples, preFilterBuffer, preFilterBuffer.length);
        peakFilter1.amplify(FilteredSamples, FilteredSamples, 3);
        peakFilter2.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);
        peakFilter2.amplify(FilteredSamples, FilteredSamples, 3);
        peakFilter3.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);
        peakFilter3.amplify(FilteredSamples, FilteredSamples, 2);
        peakFilter4.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);

        // LOT OF MATH on filtered samples
        // ......
        // 
    } catch (Exception e) {         
        e.printStackTrace();            }


}
4

1 回答 1

0

您可以为您的服务添加方法,例如putData(...)... 启动服务后,如果它... 活着,我认为您只需将其用作对象。

于 2012-07-28T15:35:19.007 回答