📄 mediarecorder.cpp
字号:
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 [ audioInput->bufferSize() ];#endif // Start the save process. startSave(); // Create the waveform display. contents->waveform->changeSettings( audioInput->frequency(), audioInput->channels() ); configureAction->setEnabled( FALSE ); contents->qualityCombo->setEnabled( FALSE ); contents->storageLocation->setEnabled( FALSE ); recordTime = 0; contents->progress->setTotalSteps( 120 ); contents->progress->setProgress( 0 ); contents->progress->setRecording(); recording = TRUE; contents->recordButton->setText( tr("Stop") ); contents->recordButton->setEnabled( TRUE ); contents->choosefileButton->setEnabled( FALSE ); contents->replayButton->setEnabled( FALSE ); contents->deleteButton->setEnabled( 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. audioInput->start(); processAudioData();}void MediaRecorder::stopRecording(){ audioInput->stop(); contents->waveform->reset(); configureAction->setEnabled( TRUE ); contents->qualityCombo->setEnabled( TRUE ); contents->storageLocation->setEnabled( TRUE ); contents->recordButton->setEnabled( FALSE ); recording = FALSE; contents->recordButton->setText( tr("Record") ); contents->choosefileButton->setEnabled( TRUE ); contents->replayButton->setEnabled( TRUE ); contents->deleteButton->setEnabled( TRUE ); // Turn off the recording light. setRecordLight( FALSE ); // Terminate the data save. endSave(); // Re-enable power save.#ifdef Q_WS_QWS QPEApplication::setTempScreenSaverMode( QPEApplication::Enable );#endif}void MediaRecorder::recordClicked(){ if ( recording ) { stopRecording(); } else { startRecording(); }}void MediaRecorder::startPlaying(){ // Do we have a decoder for this type of file? QString s=findplayfile->getfile();// QString name =s.left(s.length()-4); // DocLnk doc;// encoder = recorderPlugins->fromType// ( qualities[config->currentQuality()].mimeType,// qualities[config->currentQuality()].formatTag ); // doc.setName( name );// doc.setType( encoder->pluginMimeType() );// doc.setLocation( contents->storageLocation->documentPath() );// playFile = doc.file();// playFileLink = doc.linkFile();// playFile=playFile.left(playFile.length()-8)+playFile.right(4); chooseFile="/root/Documents//audio/x-wav/"+s; decoder = playerPlugins->fromFile( chooseFile ); if ( decoder == 0 || !( decoder->open( chooseFile ) ) ) { QMessageBox::critical( this, tr( "No playback plugin found" ), tr( "Voice Recorder was unable to\n" "locate a suitable plugin to\n" "play the selected format." ) ); return; } // Reconfigure the UI to reflect the current mode. configureAction->setEnabled( FALSE ); contents->qualityCombo->setEnabled( FALSE ); contents->storageLocation->setEnabled( FALSE ); recordTime = 0; samplesPlayed = 0; long samples = decoder->audioSamples( 0 ); long freq = decoder->audioFrequency( 0 ); samples = (samples + freq - 1) / freq; contents->progress->setTotalSteps( samples ); contents->progress->setProgress( 0 ); contents->progress->setPlaying(); playing = TRUE; contents->recordButton->setEnabled( FALSE ); contents->replayButton->setText( tr("Stop") ); contents->replayButton->setEnabled( TRUE ); contents->deleteButton->setEnabled( TRUE ); // Create the waveform display. contents->waveform->changeSettings( decoder->audioFrequency( 0 ), decoder->audioChannels( 0 ) ); // Open the audio output device. audioOutput = new AudioDevice( decoder->audioFrequency( 0 ), decoder->audioChannels( 0 ), 2 ); sampleBuffer = new short [ audioOutput->bufferSize() ]; QObject::connect( audioOutput, SIGNAL(completedIO()), this, SLOT(audioOutputDone()) ); // Force the first block to be played, to prime the device. audioOutputDone();}void MediaRecorder::stopPlaying(){ // Stop playing back the recorded sound if ( audioOutput ) { delete audioOutput; audioOutput = 0; } if ( decoder ) { decoder->close(); decoder = 0; } if ( sampleBuffer ) { delete[] sampleBuffer; sampleBuffer = 0; } // Return the UI to the default state. contents->waveform->reset(); configureAction->setEnabled( TRUE ); contents->qualityCombo->setEnabled( TRUE ); contents->storageLocation->setEnabled( TRUE ); contents->recordButton->setEnabled( TRUE ); playing = FALSE;// contents->choosefileButton->setEnabled( TRUE ); contents->replayButton->setEnabled( TRUE ); contents->replayButton->setText( tr("Play") ); contents->deleteButton->setEnabled( TRUE ); contents->progress->setProgress( 0 );}void MediaRecorder::choosefileClicked() { findplayfile =new Findplayfile(this); findplayfile->exec();// playFile=s.left(s.length()-4); contents->replayButton->setEnabled( TRUE ); contents->deleteButton->setEnabled( TRUE );}void MediaRecorder::replayClicked(){ if ( playing ) { stopPlaying(); } else { startPlaying(); }}void MediaRecorder::deleteClicked(){ QString s=findplayfile->getfile(); chooseFile="/root/Documents//audio/x-wav/"+s; if ( playing ) { stopPlaying(); }// DocLnk doc( lastSaved );// doc.setLinkFile( lastSavedLink ); DocLnk doc( chooseFile ); chooseFileLink="/root/Documents//audio/x-wav/"+s.left(s.length()-4)+".desktop"; doc.setLinkFile( chooseFileLink ); doc.removeFiles(); contents->replayButton->setEnabled( FALSE ); contents->deleteButton->setEnabled( FALSE );}void MediaRecorder::clearData(){#ifdef RECORD_THEN_SAVE samples->clear();#endif contents->waveform->reset(); configureAction->setEnabled( TRUE ); contents->recordButton->setEnabled( TRUE ); recordTime = 0; contents->progress->setTotalSteps( 120 ); contents->progress->setProgress( 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 = 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 = audioInput->read( sampleBuffer, audioInput->bufferSize() ); while ( result > 0 ) { contents->waveform->newSamples( sampleBuffer, result ); encoder->writeAudioSamples( sampleBuffer, (long)result ); result = audioInput->read( sampleBuffer, audioInput->bufferSize() ); }#endif // Update the record time if another second has elapsed. newTime = audioInput->position() / (long)(audioInput->frequency()); if ( newTime != recordTime ) { recordTime = newTime; if ( recordTime >= contents->progress->totalSteps() ) { // Change the resolution on the progress bar as we've // max'ed out the current limit. contents->progress->setTotalSteps (contents->progress->totalSteps() * 4); } contents->progress->setProgress( (int)recordTime ); } // Stop recording if we have hit the maximum record time. if ( recordTime >= maxRecordTime && !stopped ) { stopRecording(); }}void MediaRecorder::configure(){ config->processPopup(); contents->qualityCombo->setCurrentItem( config->currentQuality() ); qualityChanged( config->currentQuality() );}void MediaRecorder::noPluginError(){ QMessageBox::critical( this, tr( "No plugin found" ), tr( "Voice Recorder was unable to\n" "locate a suitable plugin to\n" "record in the selected format." ) );}#define RECORD_LIGHT_ID ((int)0x56526563) // "VRec"void MediaRecorder::setRecordLight( bool enable ){ if ( enable ) { QCopEnvelope( "Qt/Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << Resource::loadPixmap( "record-light" ); recordLightState = TRUE; lightTimer->start( 500 ); } else { QCopEnvelope( "Qt/Tray", "remove(int)" ) << RECORD_LIGHT_ID; recordLightState = FALSE; lightTimer->stop(); }}void MediaRecorder::recordLightBlink(){ recordLightState = !recordLightState; if ( recordLightState ) { QCopEnvelope( "Qt/Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << Resource::loadPixmap( "record-light" ); } else { QCopEnvelope( "Qt/Tray", "setIcon(int,QPixmap)" ) << RECORD_LIGHT_ID << Resource::loadPixmap( "record-blank" ); }}void MediaRecorder::closeEvent( QCloseEvent *e ){ // Shut down recording or playback. if ( recording ) { stopRecording(); } else if ( playing ) { stopPlaying(); } // Disable the "Play" and "Delete" buttons so that if we // are restarted in "fast load" mode, we will return to // the initial "nothing is recorded" state in the UI. contents->replayButton->setEnabled( FALSE ); contents->deleteButton->setEnabled( FALSE ); e->accept();}void MediaRecorder::audioOutputDone(){ // Read the next block of samples. long samplesRead = 0; if ( !decoder->audioReadSamples( sampleBuffer, 2, 1024, samplesRead, 0 ) ) { stopPlaying(); return; } if ( samplesRead <= 0 ) { stopPlaying(); return; } // Write the samples to the audio output device. audioOutput->write( (char *)sampleBuffer, (int)( samplesRead * 4 ) ); // Update the waveform display. contents->waveform->newSamples( sampleBuffer, samplesRead ); // Update the playback time if another second has elapsed. samplesPlayed += samplesRead; long newTime = samplesPlayed / (long)(decoder->audioFrequency( 0 )); if ( newTime != recordTime ) { recordTime = newTime; if ( recordTime > contents->progress->totalSteps() ) { recordTime = contents->progress->totalSteps(); } contents->progress->setProgress( (int)recordTime ); }}void MediaRecorder::traySocket( const QCString& msg, const QByteArray &data ){ QDataStream stream( data, IO_ReadOnly ); int id = 0; QPoint p; if ( msg == "popup(int,QPoint)" ) { stream >> id >> p; } else if ( msg == "clicked(int,QPoint)" || msg == "doubleClicked(int,QPoint)" ) { stream >> id >> p; } if ( id == RECORD_LIGHT_ID ) { if ( this->isVisible() ) this->raise(); else this->showMaximized(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -