📄 mediarecorder.cpp
字号:
delete io; io = 0; // Clear the data for another recording. clearData();}void MediaRecorder::startRecording(){ if ( config == 0 ) switchToRecorder(); if (!requestMode) recordQuality = qualities[config->currentQuality()]; // Bail out if we don't have a plugin for the selected format. if (recorderPlugins->fromType(recordQuality.mimeType, recordQuality.formatTag) == 0) { noPluginError(); return; } // Disable power save while recording so that the device // doesn't suspend while a long-term recording session // is in progress.#ifdef Q_WS_QWS QtopiaApplication::setTemporaryScreenSaverMode(QtopiaApplication::DisableSuspend);#endif // Configure and open device m_audioInput->setFrequency(recordQuality.frequency); m_audioInput->setChannels(recordQuality.channels); m_audioInput->open(QIODevice::ReadOnly); qDebug("channels: %d", m_audioInput->channels()); qDebug("frequency: %d", m_audioInput->frequency()); qDebug("bufferSize: %d", MR_BUFSIZE); // TODO: move to ctor connect(m_audioInput, SIGNAL(readyRead()), this, SLOT(processAudioData())); // Create the sample buffer, for recording the data temporarily.#ifdef RECORD_THEN_SAVE if (samples) delete samples; samples = new SampleBuffer(audioInput->bufferSize());#else if (sampleBuffer) delete[] sampleBuffer; sampleBuffer = new short[MR_BUFSIZE];#endif // Start the save process. if (startSave()) { // Create the waveform display. contents->waveform->changeSettings( m_audioInput->frequency(), m_audioInput->channels()); if (configureAction) configureAction->setEnabled(false); contents->qualityCombo->setEnabled( false ); contents->storageLocation->setEnabled( false ); recordTime = 0; contents->progress->setMaximum( 120 ); contents->progress->setValue( 0 ); contents->progress->setRecording(); recording = true; setContextKey( false ); contents->recordButton->setText( tr("Stop") ); contents->recordButton->setEnabled( true ); setReplayEnabled( false ); setDeleteEnabled( false ); // Turn on the recording light. setRecordLight(true); // Some audio devices may start sending us data immediately, but // others may need an initial "read" to start the ball rolling. // Processing at least one data block will prime the device. processAudioData(); }}void MediaRecorder::stopRecordingNoSwitch(){ m_audioInput->close(); contents->waveform->reset(); if (configureAction) configureAction->setEnabled( true ); contents->qualityCombo->setEnabled( true ); contents->storageLocation->setEnabled( true ); contents->recordButton->setEnabled( false ); recording = false; setContextKey( true ); contents->recordButton->setText( tr("Record") ); setReplayEnabled( true ); setDeleteEnabled( true ); // Turn off the recording light. setRecordLight( false ); // Terminate the data save. endSave(); // Re-enable power save.#ifdef Q_WS_QWS QtopiaApplication::setTemporaryScreenSaverMode(QtopiaApplication::Enable);#endif // If we were in request mode, then send the response and quit the app. if (requestMode) { qDebug() << "Respond to id:" << responseId; QCopEnvelope e(responseChannel, "valueSupplied(QString,QString)"); e << responseId << lastSaved; requestMode = false; qApp->quit(); }}void MediaRecorder::stopRecording(){ stopRecordingNoSwitch(); switchToFileSelector();}void MediaRecorder::recordClicked(){ if (recording) { stopRecording(); } else { startRecording(); }}void MediaRecorder::startPlaying(){ // Reconfigure the UI to reflect the current mode. if (configureAction) configureAction->setEnabled( false ); contents->qualityCombo->setEnabled( false ); contents->storageLocation->setEnabled( false ); recordTime = 0; samplesPlayed = 0; // Disable power save while playing so that the device // doesn't suspend before the file finishes.#ifdef Q_WS_QWS QtopiaApplication::setTemporaryScreenSaverMode(QtopiaApplication::DisableSuspend);#endif contents->progress->setValue( 0 ); contents->progress->setPlaying(); playing = true; setContextKey( false ); contents->recordButton->setEnabled( false ); contents->replayButton->setText( tr("Stop") ); setReplayEnabled( true ); setReplayEnabled( true );#ifdef QTOPIA4_TODO // Create the waveform display. contents->waveform->changeSettings( decoder->audioFrequency( 0 ), decoder->audioChannels( 0 ) );#endif startWhenAudioDeviceReady = true;}void MediaRecorder::audioDeviceReady(){ audioDeviceIsReady = true; if (startWhenAudioDeviceReady) // Force the first block to be played, to prime the device. QTimer::singleShot(1, this, SLOT(audioOutputDone())); // ready signal generated by audiodevice, it's no longer reentrant}void MediaRecorder::audioDeviceError(){ audioDeviceIsReady = false;// QMessageBox::critical(0, tr("Audio Error"), tr("<qt>Error initialising audio.</qt>")); QTimer::singleShot(1, this, SLOT(stopPlaying())); // error signal generated by audiodevice, it's no longer reentrant}void MediaRecorder::stopPlayingNoSwitch(){ // Stop playing back the recorded sound if (m_sound) { delete m_sound; m_sound = 0; } if ( sampleBuffer ) { delete[] sampleBuffer; sampleBuffer = 0; } // Re-enable power save (that was disabled in startPlaying()).#ifdef Q_WS_QWS QtopiaApplication::setTemporaryScreenSaverMode(QtopiaApplication::Enable);#endif // Ensure UI initialized getContentsWidget(); // Return the UI to the default state. contents->waveform->reset(); if ( configureAction ) configureAction->setEnabled( true ); contents->qualityCombo->setEnabled( true ); contents->storageLocation->setEnabled( true ); contents->recordButton->setEnabled( true ); playing = false; setReplayEnabled( true ); setContextKey( true ); contents->replayButton->setText( tr("Play") ); setReplayEnabled( true ); contents->progress->setValue( 0 );}void MediaRecorder::stopPlaying(){ stopPlayingNoSwitch(); switchToFileSelector();}void MediaRecorder::replayClicked(){ if ( playing ) { stopPlaying(); } else { startPlaying(); }}void MediaRecorder::deleteClicked(){ if (playing) { stopPlaying(); } QContent doc(lastSaved); doc.removeFiles(); setReplayEnabled(false); setDeleteEnabled(false);}void MediaRecorder::clearData(){#ifdef RECORD_THEN_SAVE samples->clear();#endif contents->waveform->reset(); if ( configureAction ) configureAction->setEnabled( true ); contents->recordButton->setEnabled( true ); recordTime = 0; contents->progress->setMaximum( 120 ); contents->progress->setValue( 0 );}void MediaRecorder::processAudioData(){ int result; long newTime; bool stopped = false;#ifdef RECORD_THEN_SAVE short *buf; unsigned int length; if ( samples->nextWriteBuffer( buf, length ) ) { // Read the next block of samples into the write buffer. result = m_audioInput->read(buf, length); samples->commitWriteBuffer( (unsigned int)result ); // Update the waveform display. contents->waveform->newSamples( buf, result ); } else { // The sample buffer is out of space, so stop recording. stopRecording(); stopped = true; }#else result = m_audioInput->read(reinterpret_cast<char*>(sampleBuffer), MR_BUFSIZE); result /= sizeof(short) * m_audioInput->channels(); contents->waveform->newSamples(sampleBuffer, result); encoder->writeAudioSamples(sampleBuffer, (long)result); m_position += result;#endif // Update the record time if another second has elapsed. newTime = m_position / (long)(m_audioInput->frequency()); if (newTime != recordTime) { recordTime = newTime; if (recordTime >= contents->progress->maximum()) { // Change the resolution on the progress bar as we've // max'ed out the current limit. contents->progress->setMaximum(contents->progress->maximum() * 4); } contents->progress->setValue((int) recordTime); } // Stop recording if we have hit the maximum record time. /* if (recordTime >= maxRecordTime && !stopped) { stopRecording(); } */}void MediaRecorder::configure(){ config->processPopup(); contents->qualityCombo->setCurrentIndex( config->currentQuality() ); qualityChanged( config->currentQuality() );}void MediaRecorder::noPluginError(){ QMessageBox::critical( this, tr( "No plugin found" ), tr( "<qt>Voice Recorder was unable to " "locate a suitable plugin to " "record in the selected format.</qt>" ) );}#define RECORD_LIGHT_ID ((int)0x56526563) // "VRec"void MediaRecorder::setRecordLight( bool enable ){ if (enable) { recordLightState = true;#ifndef QTOPIA_PHONE QCopEnvelope( "Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << QPixmap( ":image/record-light" ); lightTimer->start( 500 );#endif } else { recordLightState = false;#ifndef QTOPIA_PHONE QCopEnvelope( "Tray", "remove(int)" ) << RECORD_LIGHT_ID; lightTimer->stop();#endif }}void MediaRecorder::recordLightBlink(){ recordLightState = !recordLightState;#ifndef QTOPIA_PHONE if ( recordLightState ) { QCopEnvelope( "Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << QPixmap( ":image/record-light" ); } else { QCopEnvelope( "Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << QPixmap( ":image/record-blank" ); }#endif}void MediaRecorder::closeEvent(QCloseEvent *e){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -