⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediarecorder.cpp

📁 基于linux的录音程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // 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   	if( sign == 1){       		mydir.setPath( "/Documents/audio/x-wav/" );              	mydir.setNameFilter("man*.wav");        }	if( sign == 2){                mydir.setPath( "/Documents/audio/x-wav/" );               	mydir.setNameFilter("woman*.wav");        }	if( sign == 3){                mydir.setPath( "/Documents/audio/x-wav/" );               mydir.setNameFilter("boy*.wav");        }        countnow = mydir.count();        QString timestr;        if( sign == 1 || sign == 3) {                timestr = mydir[ countnow-1 ].mid(22,4)                        + mydir[ countnow-1 ].mid(3,9) + " "                        + mydir[ countnow-1 ].mid(13,2) + ":"                        + mydir[ countnow-1 ].mid(16,2) + ":"                        + mydir[ countnow-1 ].mid(19,2) ;        }        if( sign == 2 ) {                timestr = mydir[ countnow-1 ].mid(24,4)                        + mydir[ countnow-1 ].mid(5,9) + " "                        + mydir[ countnow-1 ].mid(15,2) + ":"                        + mydir[ countnow-1 ].mid(18,2) + ":"                        + mydir[ countnow-1 ].mid(21,2) ;        }        contents->timeLabel->setText( timestr );        contents->disLabel->setNum( countnow );        contents->upButton->setEnabled( FALSE );	contents->downButton->setEnabled( FALSE );	}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);	//    QDir mydir;//    mydir.setPath("/Documents/audio/x-wav/");//    mydir.setNameFilter("man*.wav");  		    chooseFile="/Documents/audio/x-wav/" + mydir[ countnow-1 ];        decoder = playerPlugins->fromFile( chooseFile );    if ( decoder == 0 || !( decoder->open( chooseFile ) ) ) {        QMessageBox::critical( this, QString::fromLocal8Bit("警告"),			       QString::fromLocal8Bit("未找到录音文件") );	return;    }    // Reconfigure the UI to reflect the current mode.    configureAction->setEnabled( FALSE );    recordTime = 0;    samplesPlayed = 0;    long samples = decoder->audioSamples( 0 );    long freq = decoder->audioFrequency( 0 );    samples = (samples + freq - 1) / freq;    playing = TRUE;    contents->recordButton->setEnabled( FALSE );//    contents->playButton->setText( tr("Stop") );    contents->playButton->setEnabled( FALSE );     contents->stopButton->setEnabled( TRUE );    contents->delButton->setEnabled( FALSE );    // Open the audio output device.    audioOutput = new AudioDevice( decoder->audioFrequency( 0 ),				   decoder->audioChannels( 0 ), 2 );    sampleBuffer = new short [ audioOutput->bufferSize() ];unsigned int leftVolume=(int)0xFFFF,rightVolume=(int)0xFFFF;bool muted=FALSE;audioOutput->setVolume(leftVolume,rightVolume,muted);//QMessageBox::critical( this, tr( "No playback plugin found" ),tr("%d"),leftVolume);    printf("<1> %d %d!!!",leftVolume,rightVolume);     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.    configureAction->setEnabled( TRUE );    contents->recordButton->setEnabled( TRUE );    playing = FALSE;//    contents->choosefileButton->setEnabled( TRUE );    contents->playButton->setEnabled( TRUE );    contents->stopButton->setEnabled( FALSE );//    contents->playButton->setText( tr("Play") );    contents->delButton->setEnabled( TRUE );}void MediaRecorder::replayClicked(){//    if ( playing ) {//	stopPlaying();//    } else {	startPlaying();//    }}void MediaRecorder::deleteClicked(){    chooseFile = "/Documents/audio/x-wav/" + mydir[ countnow-1 ];    if ( playing ) {	stopPlaying();    }//    DocLnk doc( lastSaved );//    doc.setLinkFile( lastSavedLink );    DocLnk doc( chooseFile ); 		    QString str(mydir[ countnow-1 ] );        chooseFileLink = "/Documents/audio/x-wav/" 		+ str.left(str.length()-4) 		+ ".desktop";    doc.setLinkFile( chooseFileLink );    doc.removeFiles();   		if( sign == 1){                mydir.setPath( "/Documents/audio/x-wav/" );                mydir.setNameFilter("man*.wav");        }        if( sign == 2){                mydir.setPath( "/Documents/audio/x-wav/" );                mydir.setNameFilter("woman*.wav");        }        if( sign == 3){                mydir.setPath( "/Documents/audio/x-wav/" );               mydir.setNameFilter("boy*.wav");        }	countnow = mydir.count();        contents->disLabel->setNum( countnow );        contents->upButton->setEnabled( FALSE );        if( countnow == 0 ){		contents->playButton->setEnabled( FALSE );    		contents->delButton->setEnabled( FALSE );		contents->timeLabel->setText( "" );		contents->downButton->setEnabled( FALSE );	}	else{		QString timestr;        	if( sign == 1 || sign == 3) {                	timestr = mydir[ countnow-1 ].mid(21,4)                        	+ mydir[ countnow-1 ].mid(4,7) + " "                        	+ mydir[ countnow-1 ].mid(12,2) + ":"                        	+ mydir[ countnow-1 ].mid(15,2) + ":"                        	+ mydir[ countnow-1 ].mid(18,2) ;        	}        	if( sign == 2 ) {                	timestr = mydir[ countnow-1 ].mid(23,4)                        	+ mydir[ countnow-1 ].mid(6,7) + " "                        	+ mydir[ countnow-1 ].mid(14,2) + ":"                        	+ mydir[ countnow-1 ].mid(17,2) + ":"                        	+ mydir[ countnow-1 ].mid(20,2) ;        	}		contents->playButton->setEnabled( TRUE );    		contents->delButton->setEnabled( TRUE );        	contents->timeLabel->setText( timestr );		if( countnow == 1 ) {			contents->downButton->setEnabled( FALSE );		}		else{				contents->downButton->setEnabled( TRUE );		}	}	}void MediaRecorder::downClicked(){	countnow -=1;	QString timestr;	if( sign == 1 || sign == 3) {                timestr = mydir[ countnow-1 ].mid(21,4)			+ mydir[ countnow-1 ].mid(4,7) + " "                        + mydir[ countnow-1 ].mid(12,2) + ":"                        + mydir[ countnow-1 ].mid(15,2) + ":"                        + mydir[ countnow-1 ].mid(18,2) ;        }        if( sign == 2 ) {               	timestr = mydir[ countnow-1 ].mid(23,4)                        + mydir[ countnow-1 ].mid(6,7) + " "                        + mydir[ countnow-1 ].mid(14,2) + ":"                        + mydir[ countnow-1 ].mid(17,2) + ":"                        + mydir[ countnow-1 ].mid(20,2) ;        }	contents->timeLabel->setText( timestr );        contents->disLabel->setNum( countnow );        if( countnow > 1 ) {                contents->upButton->setEnabled( TRUE );                contents->downButton->setEnabled( TRUE );        }        if( countnow == 1 ) {                contents->upButton->setEnabled( TRUE );                contents->downButton->setEnabled( FALSE );        }	}void MediaRecorder::upClicked(){	countnow += 1;	QString timestr;	if( sign == 1 || sign == 3) {		timestr = mydir[ countnow-1 ].mid(21,4)                        + mydir[ countnow-1 ].mid(4,7) + " "                        + mydir[ countnow-1 ].mid(12,2) + ":"                        + mydir[ countnow-1 ].mid(15,2) + ":"                        + mydir[ countnow-1 ].mid(18,2) ;	}	if( sign == 2 ) {		timestr = mydir[ countnow-1 ].mid(23,4)                        + mydir[ countnow-1 ].mid(6,7) + " "                        + mydir[ countnow-1 ].mid(14,2) + ":"                        + mydir[ countnow-1 ].mid(17,2) + ":"                        + mydir[ countnow-1 ].mid(20,2) ;	}        contents->timeLabel->setText( timestr );	contents->disLabel->setNum( countnow );			if( countnow < (int)mydir.count() ) {		contents->upButton->setEnabled( TRUE );		contents->downButton->setEnabled( TRUE );	}	if( countnow == (int)mydir.count() ) {		contents->upButton->setEnabled( FALSE );                contents->downButton->setEnabled( TRUE );	}	}void MediaRecorder::clearData(){#ifdef RECORD_THEN_SAVE    samples->clear();#endif    configureAction->setEnabled( TRUE );    contents->recordButton->setEnabled( TRUE );    recordTime = 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.    } else {	// The sample buffer is out of space, so stop recording.	stopRecording();	stopped = TRUE;    }#else    result = audioInput->read( sampleBuffer, audioInput->bufferSize() );    while ( result > 0 ) {	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, QString::fromLocal8Bit("警告"),			   QString::fromLocal8Bit("未找到插件") );}#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->playButton->setEnabled( FALSE );    contents->delButton->setEnabled( FALSE );    contents->stopButton->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 + -