diff options
Diffstat (limited to 'doc/src/snippets/multimedia-snippets')
-rw-r--r-- | doc/src/snippets/multimedia-snippets/audio.cpp | 210 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/audiorecorder.cpp | 215 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/camera.cpp | 82 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/media.cpp | 236 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/multimedia-snippets.pro | 21 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/player.cpp | 317 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/soundeffect.qml | 63 | ||||
-rw-r--r-- | doc/src/snippets/multimedia-snippets/video.cpp | 129 |
8 files changed, 1273 insertions, 0 deletions
diff --git a/doc/src/snippets/multimedia-snippets/audio.cpp b/doc/src/snippets/multimedia-snippets/audio.cpp new file mode 100644 index 000000000..d58bc5f53 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/audio.cpp @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* Audio related snippets */ +#include <QFile> +#include <QTimer> +#include <QDebug> + +#include "qaudiodeviceinfo.h" +#include "qaudioinput.h" +#include "qaudiooutput.h" + +class AudioInputExample : public QObject { + Q_OBJECT +public: + void setup(); + + +public Q_SLOTS: + void stopRecording(); + void stateChanged(QAudio::State newState); + +private: + //! [Audio input class members] + QFile destinationFile; // class member. + QAudioInput* audio; // class member. + //! [Audio input class members] +}; + + +void AudioInputExample::setup() +//! [Audio input setup] +{ + destinationFile.setFileName("/tmp/test.raw"); + destinationFile.open( QIODevice::WriteOnly | QIODevice::Truncate ); + + QAudioFormat format; + // set up the format you want, eg. + format.setFrequency(8000); + format.setChannels(1); + format.setSampleSize(8); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::UnSignedInt); + + QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice(); + if (!info.isFormatSupported(format)) { + qWarning()<<"default format not supported try to use nearest"; + format = info.nearestFormat(format); + } + + audio = new QAudioInput(format, this); + connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(stateChanged(QAudio::State))); + + QTimer::singleShot(3000, this, SLOT(stopRecording())); + audio->start(&destinationFile); + // Records audio for 3000ms +} +//! [Audio input setup] + +//! [Audio input stop recording] +void AudioInputExample::stopRecording() +{ + audio->stop(); + destinationFile.close(); + delete audio; +} +//! [Audio input stop recording] + +//! [Audio input state changed] +void AudioInputExample::stateChanged(QAudio::State newState) +{ + switch (newState) { + case QAudio::StoppedState: + if (audio->error() != QAudio::NoError) { + // Error handling + } else { + // Finished recording + } + break; + + // ... + } +} +//! [Audio input state changed] + + +class AudioOutputExample : public QObject { + Q_OBJECT +public: + void setup(); + +public Q_SLOTS: + void stateChanged(QAudio::State newState); + +private: + //! [Audio output class members] + QFile sourceFile; // class member. + QAudioOutput* audio; // class member. + //! [Audio output class members] +}; + + +void AudioOutputExample::setup() +//! [Audio output setup] +{ + sourceFile.setFileName("/tmp/test.raw"); + sourceFile.open(QIODevice::ReadOnly); + + QAudioFormat format; + // Set up the format, eg. + format.setFrequency(8000); + format.setChannels(1); + format.setSampleSize(8); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::UnSignedInt); + + QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); + if (!info.isFormatSupported(format)) { + qWarning() << "raw audio format not supported by backend, cannot play audio."; + return; + } + + audio = new QAudioOutput(format, this); + connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(stateChanged(QAudio::State))); + audio->start(&sourceFile); +} +//! [Audio output setup] + +//! [Audio output state changed] +void AudioOutputExample::stateChanged(QAudio::State newState) +{ + switch (newState) { + case QAudio::IdleState: + // Finished playing (no more data) + audio->stop(); + sourceFile.close(); + delete audio; + break; + + case QAudio::StoppedState: + // Stopped for other reasons + if (audio->error() != QAudio::NoError) { + // Error handling + } + break; + + // ... + } +} +//! [Audio output state changed] + +void AudioDeviceInfo() +{ + //! [Setting audio format] + QAudioFormat format; + format.setFrequency(44100); + // ... other format parameters + format.setSampleType(QAudioFormat::SignedInt); + + QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); + + if (!info.isFormatSupported(format)) + format = info.nearestFormat(format); + //! [Setting audio format] + + //! [Dumping audio formats] + foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) + qDebug() << "Device name: " << deviceInfo.deviceName(); + //! [Dumping audio formats] +} diff --git a/doc/src/snippets/multimedia-snippets/audiorecorder.cpp b/doc/src/snippets/multimedia-snippets/audiorecorder.cpp new file mode 100644 index 000000000..fd92026c6 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/audiorecorder.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include <qaudiocapturesource.h> +#include <qmediarecorder.h> +#include <qmediaservice.h> + +#include <QtMultimediaKit/qaudioformat.h> + +#include "audiorecorder.h" + +AudioRecorder::AudioRecorder() +{ +//! [create-objs-1] + audiosource = new QAudioCaptureSource; + capture = new QMediaRecorder(audiosource); +//! [create-objs-1] + + // set a default file + capture->setOutputLocation(QUrl("test.raw")); + + QWidget *window = new QWidget; + QGridLayout* layout = new QGridLayout; + + QLabel* deviceLabel = new QLabel; + deviceLabel->setText("Devices"); + deviceBox = new QComboBox(this); + deviceBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); + + QLabel* codecLabel = new QLabel; + codecLabel->setText("Codecs"); + codecsBox = new QComboBox(this); + codecsBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); + + QLabel* qualityLabel = new QLabel; + qualityLabel->setText("Quality"); + qualityBox = new QComboBox(this); + qualityBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); + +//! [device-list] + for(int i = 0; i < audiosource->deviceCount(); i++) + deviceBox->addItem(audiosource->name(i)); +//! [device-list] + +//! [codec-list] + QStringList codecs = capture->supportedAudioCodecs(); + for(int i = 0; i < codecs.count(); i++) + codecsBox->addItem(codecs.at(i)); +//! [codec-list] + + qualityBox->addItem("Low"); + qualityBox->addItem("Medium"); + qualityBox->addItem("High"); + + connect(capture, SIGNAL(durationChanged(qint64)), this, SLOT(updateProgress(qint64))); + connect(capture, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(stateChanged(QMediaRecorder::State))); + + layout->addWidget(deviceLabel,0,0,Qt::AlignHCenter); + connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); + layout->addWidget(deviceBox,0,1,1,3,Qt::AlignLeft); + + layout->addWidget(codecLabel,1,0,Qt::AlignHCenter); + connect(codecsBox,SIGNAL(activated(int)),SLOT(codecChanged(int))); + layout->addWidget(codecsBox,1,1,Qt::AlignLeft); + + layout->addWidget(qualityLabel,1,2,Qt::AlignHCenter); + connect(qualityBox,SIGNAL(activated(int)),SLOT(qualityChanged(int))); + layout->addWidget(qualityBox,1,3,Qt::AlignLeft); + + fileButton = new QPushButton(this); + fileButton->setText(tr("Output File")); + connect(fileButton,SIGNAL(clicked()),SLOT(selectOutputFile())); + layout->addWidget(fileButton,3,0,Qt::AlignHCenter); + + button = new QPushButton(this); + button->setText(tr("Record")); + connect(button,SIGNAL(clicked()),SLOT(toggleRecord())); + layout->addWidget(button,3,3,Qt::AlignHCenter); + + recTime = new QLabel; + recTime->setText("0 sec"); + layout->addWidget(recTime,4,0,Qt::AlignHCenter); + + window->setLayout(layout); + setCentralWidget(window); + window->show(); + + active = false; +} + +AudioRecorder::~AudioRecorder() +{ + delete capture; + delete audiosource; +} + +void AudioRecorder::updateProgress(qint64 pos) +{ + currentTime = pos; + if(currentTime == 0) currentTime = 1; + QString text = QString("%1 secs").arg(currentTime/1000); + recTime->setText(text); +} + +void AudioRecorder::stateChanged(QMediaRecorder::State state) +{ + qWarning()<<"stateChanged() "<<state; +} + +void AudioRecorder::deviceChanged(int idx) +{ +//! [get-device] + for(int i = 0; i < audiosource->deviceCount(); i++) { + if(deviceBox->itemText(idx).compare(audiosource->name(i)) == 0) + audiosource->setSelectedDevice(i); + } +//! [get-device] +} + +void AudioRecorder::codecChanged(int idx) +{ + Q_UNUSED(idx); + //capture->setAudioCodec(codecsBox->itemText(idx)); +} + +void AudioRecorder::qualityChanged(int idx) +{ + Q_UNUSED(idx); + /* + if(capture->audioCodec().compare("audio/pcm") == 0) { + if(qualityBox->itemText(idx).compare("Low") == 0) { + // 8000Hz mono is 8kbps + capture->setAudioBitrate(8); + } else if(qualityBox->itemText(idx).compare("Medium") == 0) { + // 22050Hz mono is 44.1kbps + capture->setAudioBitrate(44); + } else if(qualityBox->itemText(idx).compare("High") == 0) { + // 44100Hz mono is 88.2kbps + capture->setAudioBitrate(88); + } + } + */ +} + +//! [toggle-record] +void AudioRecorder::toggleRecord() +{ + if(!active) { + recTime->setText("0 sec"); + currentTime = 0; + capture->record(); + + button->setText(tr("Stop")); + active = true; + } else { + capture->stop(); + button->setText(tr("Record")); + active = false; + } +} +//! [toggle-record] + +void AudioRecorder::selectOutputFile() +{ + QStringList fileNames; + + QFileDialog dialog(this); + + dialog.setFileMode(QFileDialog::AnyFile); + if (dialog.exec()) + fileNames = dialog.selectedFiles(); + + if(fileNames.size() > 0) + capture->setOutputLocation(QUrl(fileNames.first())); +} diff --git a/doc/src/snippets/multimedia-snippets/camera.cpp b/doc/src/snippets/multimedia-snippets/camera.cpp new file mode 100644 index 000000000..453b44fce --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/camera.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* Camera snippets */ + +#include "qcamera.h" +#include "qcameraviewfinder.h" +#include "qmediarecorder.h" +#include "qcameraimagecapture.h" + +void camera() +{ + QCamera *camera = 0; + QCameraViewfinder *viewfinder = 0; + QMediaRecorder *recorder = 0; + QCameraImageCapture *imageCapture = 0; + + //! [Camera] + camera = new QCamera; + + viewfinder = new QCameraViewfinder(); + viewfinder->show(); + + camera->setViewfinder(viewfinder); + + recorder = new QMediaRecorder(camera); + imageCapture = new QCameraImageCapture(camera); + + camera->setCaptureMode(QCamera::CaptureStillImage); + camera->start(); + //! [Camera] + + //! [Camera keys] + //on half pressed shutter button + camera->searchAndLock(); + + //on shutter button pressed + imageCapture->capture(); + + //on shutter button released + camera->unlock(); + //! [Camera keys] + +} diff --git a/doc/src/snippets/multimedia-snippets/media.cpp b/doc/src/snippets/multimedia-snippets/media.cpp new file mode 100644 index 000000000..6ef60f7e7 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/media.cpp @@ -0,0 +1,236 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* Media related snippets */ +#include <QFile> +#include <QTimer> + +#include "qaudiocapturesource.h" +#include "qmediaplaylist.h" +#include "qmediarecorder.h" +#include "qmediaservice.h" +#include "qmediaimageviewer.h" +#include "qmediaimageviewer.h" +#include "qmediaplayercontrol.h" +#include "qmediaplayer.h" +#include "qradiotuner.h" +#include "qvideowidget.h" +#include "qcameraimagecapture.h" + +class MediaExample : public QObject { + Q_OBJECT + + void AudioCaptureSource(); + void MediaControl(); + void MediaImageViewer(); + void MediaPlayer(); + void RadioTuna(); + void MediaRecorder(); + void EncoderSettings(); + void ImageEncoderSettings(); + +private: + // Common naming + QMediaService *mediaService; + QVideoWidget *videoWidget; + QWidget *widget; + QMediaPlayer *player; + QMediaPlaylist *playlist; + QMediaContent video; + QMediaRecorder *recorder; + QMediaImageViewer *viewer; + QCameraImageCapture *imageCapture; + QAudioCaptureSource *audioSource; + QString fileName; + QRadioTuner *radio; + QMediaContent image1; + QMediaContent image2; + QMediaContent image3; + + static const int yourRadioStationFrequency = 11; +}; + +void MediaExample::AudioCaptureSource() +{ + //! [Audio capture source] + QAudioCaptureSource* audioSource = new QAudioCaptureSource; + QMediaRecorder* recorder = new QMediaRecorder(audioSource); + + recorder->setOutputLocation(QUrl("test.raw")); + //! [Audio capture source] + + Q_UNUSED(audioSource); +} + + +void MediaExample::MediaControl() +{ + { + //! [Request control] + QMediaPlayerControl *control = qobject_cast<QMediaPlayerControl *>( + mediaService->requestControl("com.nokia.Qt.QMediaPlayerControl/1.0")); + //! [Request control] + Q_UNUSED(control); + } + + { + //! [Request control templated] + QMediaPlayerControl *control = mediaService->requestControl<QMediaPlayerControl *>(); + //! [Request control templated] + Q_UNUSED(control); + } +} + + +void MediaExample::EncoderSettings() +{ + //! [Audio encoder settings] + QAudioEncoderSettings audioSettings; + audioSettings.setCodec("audio/mpeg"); + audioSettings.setChannelCount(2); + + recorder->setEncodingSettings(audioSettings); + //! [Audio encoder settings] + + //! [Video encoder settings] + QVideoEncoderSettings videoSettings; + videoSettings.setCodec("video/mpeg2"); + videoSettings.setResolution(640, 480); + + recorder->setEncodingSettings(audioSettings, videoSettings); + //! [Video encoder settings] +} + +void MediaExample::ImageEncoderSettings() +{ + //! [Image encoder settings] + QImageEncoderSettings imageSettings; + imageSettings.setCodec("image/jpeg"); + imageSettings.setResolution(1600, 1200); + + imageCapture->setEncodingSettings(imageSettings); + //! [Image encoder settings] +} + +void MediaExample::MediaImageViewer() +{ + //! [Binding] + viewer = new QMediaImageViewer(this); + + videoWidget = new QVideoWidget; + viewer->bind(videoWidget); + videoWidget->show(); + //! [Binding] + + //! [Playlist] + playlist = new QMediaPlaylist(this); + playlist->setPlaybackMode(QMediaPlaylist::Loop); + playlist->addMedia(image1); + playlist->addMedia(image2); + playlist->addMedia(image3); + + viewer->setPlaylist(playlist); + viewer->setTimeout(5000); + viewer->play(); + //! [Playlist] +} + +void MediaExample::MediaPlayer() +{ + //! [Player] + player = new QMediaPlayer; + connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); + player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); + player->setVolume(50); + player->play(); + //! [Player] + + //! [Movie playlist] + playlist = new QMediaPlaylist; + playlist->addMedia(QUrl("http://example.com/movie1.mp4")); + playlist->addMedia(QUrl("http://example.com/movie2.mp4")); + playlist->addMedia(QUrl("http://example.com/movie3.mp4")); + playlist->setCurrentIndex(1); + + player = new QMediaPlayer; + player->setPlaylist(playlist); + + videoWidget = new QVideoWidget; + player->setVideoOutput(videoWidget); + videoWidget->show(); + + player->play(); + //! [Movie playlist] +} + +void MediaExample::MediaRecorder() +{ + //! [Media recorder] + // Audio only recording + audioSource = new QAudioCaptureSource; + recorder = new QMediaRecorder(audioSource); + + QAudioEncoderSettings audioSettings; + audioSettings.setCodec("audio/vorbis"); + audioSettings.setQuality(QtMultimediaKit::HighQuality); + + recorder->setEncodingSettings(audioSettings); + + recorder->setOutputLocation(QUrl::fromLocalFile(fileName)); + recorder->record(); + //! [Media recorder] +} + +void MediaExample::RadioTuna() +{ + //! [Radio tuner] + radio = new QRadioTuner; + connect(radio, SIGNAL(frequencyChanged(int)), this, SLOT(freqChanged(int))); + if (radio->isBandSupported(QRadioTuner::FM)) { + radio->setBand(QRadioTuner::FM); + radio->setFrequency(yourRadioStationFrequency); + radio->setVolume(100); + radio->start(); + } + //! [Radio tuner] +} + + diff --git a/doc/src/snippets/multimedia-snippets/multimedia-snippets.pro b/doc/src/snippets/multimedia-snippets/multimedia-snippets.pro new file mode 100644 index 000000000..c85631c58 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/multimedia-snippets.pro @@ -0,0 +1,21 @@ +# Doc snippets - compiled for truthiness + +TEMPLATE = lib +TARGET = qtmmksnippets +include(../../../../features/basic_examples_setup.pri) + +INCLUDEPATH += ../../../../src/global \ + ../../../../src/multimediakit \ + ../../../../src/multimediakit/audio \ + ../../../../src/multimediakit/video \ + ../../../../src/multimediakit/effects + +CONFIG += console + +qtAddLibrary(QtMultimediaKit) + +SOURCES += \ + audio.cpp \ + video.cpp \ + camera.cpp \ + media.cpp diff --git a/doc/src/snippets/multimedia-snippets/player.cpp b/doc/src/snippets/multimedia-snippets/player.cpp new file mode 100644 index 000000000..db9373437 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/player.cpp @@ -0,0 +1,317 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "player.h" + +#include "playercontrols.h" +#include "playlistmodel.h" +#include "videowidget.h" + +#include <qmediaservice.h> +#include <qmediaplaylist.h> + +#include <QtGui> + +Player::Player(QWidget *parent) + : QWidget(parent) + , videoWidget(0) + , coverLabel(0) + , slider(0) + , colorDialog(0) +{ +//! [create-objs] + player = new QMediaPlayer; + playlist = new QMediaPlaylist(player); +//! [create-objs] + + connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64))); + connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64))); + connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged())); + connect(playlist, SIGNAL(playlistPositionChanged(int)), SLOT(playlistPositionChanged(int))); + connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), + this, SLOT(statusChanged(QMediaPlayer::MediaStatus))); + connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int))); + + videoWidget = new VideoWidget(player); + + playlistModel = new PlaylistModel(this); + playlistModel->setPlaylist(playlist); + + playlistView = new QListView; + playlistView->setModel(playlistModel); + playlistView->setCurrentIndex(playlistModel->index(playlist->currentPosition(), 0)); + + connect(playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(jump(QModelIndex))); + + slider = new QSlider(Qt::Horizontal); + slider->setRange(0, player->duration() / 1000); + + connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int))); + + QPushButton *openButton = new QPushButton(tr("Open")); + + connect(openButton, SIGNAL(clicked()), this, SLOT(open())); + + PlayerControls *controls = new PlayerControls; + controls->setState(player->state()); + controls->setVolume(player->volume()); + controls->setMuted(controls->isMuted()); + + connect(controls, SIGNAL(play()), player, SLOT(play())); + connect(controls, SIGNAL(pause()), player, SLOT(pause())); + connect(controls, SIGNAL(stop()), player, SLOT(stop())); + connect(controls, SIGNAL(next()), playlist, SLOT(next())); + connect(controls, SIGNAL(previous()), playlist, SLOT(previous())); + connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int))); + connect(controls, SIGNAL(changeMuting(bool)), player, SLOT(setMuted(bool))); + connect(controls, SIGNAL(changeRate(qreal)), player, SLOT(setPlaybackRate(qreal))); + + connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), + controls, SLOT(setState(QMediaPlayer::State))); + connect(player, SIGNAL(volumeChanged(int)), controls, SLOT(setVolume(int))); + connect(player, SIGNAL(mutingChanged(bool)), controls, SLOT(setMuted(bool))); + + QPushButton *fullScreenButton = new QPushButton(tr("FullScreen")); + fullScreenButton->setCheckable(true); + + if (videoWidget != 0) { + connect(fullScreenButton, SIGNAL(clicked(bool)), videoWidget, SLOT(setFullScreen(bool))); + connect(videoWidget, SIGNAL(fullScreenChanged(bool)), + fullScreenButton, SLOT(setChecked(bool))); + } else { + fullScreenButton->setEnabled(false); + } + + QPushButton *colorButton = new QPushButton(tr("Color Options...")); + if (videoWidget) + connect(colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); + else + colorButton->setEnabled(false); + + QBoxLayout *displayLayout = new QHBoxLayout; + if (videoWidget) + displayLayout->addWidget(videoWidget, 2); + else + displayLayout->addWidget(coverLabel, 2); + displayLayout->addWidget(playlistView); + + QBoxLayout *controlLayout = new QHBoxLayout; + controlLayout->setMargin(0); + controlLayout->addWidget(openButton); + controlLayout->addStretch(1); + controlLayout->addWidget(controls); + controlLayout->addStretch(1); + controlLayout->addWidget(fullScreenButton); + controlLayout->addWidget(colorButton); + + QBoxLayout *layout = new QVBoxLayout; + layout->addLayout(displayLayout); + layout->addWidget(slider); + layout->addLayout(controlLayout); + + setLayout(layout); + + metaDataChanged(); +} + +Player::~Player() +{ + delete playlist; + delete player; +} + +void Player::open() +{ + QStringList fileNames = QFileDialog::getOpenFileNames(); + foreach (QString const &fileName, fileNames) + playlist->appendItem(QUrl::fromLocalFile(fileName)); +} + +void Player::durationChanged(qint64 duration) +{ + slider->setMaximum(duration / 1000); +} + +void Player::positionChanged(qint64 progress) +{ + slider->setValue(progress / 1000); +} + +void Player::metaDataChanged() +{ + //qDebug() << "update metadata" << player->metaData(QtMultimediaKit::Title).toString(); + if (player->isMetaDataAvailable()) { + setTrackInfo(QString("%1 - %2") + .arg(player->metaData(QtMultimediaKit::AlbumArtist).toString()) + .arg(player->metaData(QtMultimediaKit::Title).toString())); + + if (coverLabel) { + QUrl url = player->metaData(QtMultimediaKit::CoverArtUrlLarge).value<QUrl>(); + + coverLabel->setPixmap(!url.isEmpty() + ? QPixmap(url.toString()) + : QPixmap()); + } + } +} + +void Player::jump(const QModelIndex &index) +{ + if (index.isValid()) { + playlist->setCurrentPosition(index.row()); + } +} + +void Player::playlistPositionChanged(int currentItem) +{ + playlistView->setCurrentIndex(playlistModel->index(currentItem, 0)); +} + +void Player::seek(int seconds) +{ + player->setPosition(seconds * 1000); +} + +void Player::statusChanged(QMediaPlayer::MediaStatus status) +{ + switch (status) { + case QMediaPlayer::UnknownMediaStatus: + case QMediaPlayer::NoMedia: + case QMediaPlayer::LoadedMedia: + case QMediaPlayer::BufferingMedia: + case QMediaPlayer::BufferedMedia: +#ifndef QT_NO_CURSOR + unsetCursor(); +#endif + setStatusInfo(QString()); + break; + case QMediaPlayer::LoadingMedia: +#ifndef QT_NO_CURSOR + setCursor(QCursor(Qt::BusyCursor)); +#endif + setStatusInfo(tr("Loading...")); + break; + case QMediaPlayer::StalledMedia: +#ifndef QT_NO_CURSOR + setCursor(QCursor(Qt::BusyCursor)); +#endif + break; + case QMediaPlayer::EndOfMedia: +#ifndef QT_NO_CURSOR + unsetCursor(); +#endif + setStatusInfo(QString()); + QApplication::alert(this); + break; + case QMediaPlayer::InvalidMedia: +#ifndef QT_NO_CURSOR + unsetCursor(); +#endif + setStatusInfo(player->errorString()); + break; + } +} + +void Player::bufferingProgress(int progress) +{ + setStatusInfo(tr("Buffering %4%%").arg(progress)); +} + +void Player::setTrackInfo(const QString &info) +{ + trackInfo = info; + + if (!statusInfo.isEmpty()) + setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo)); + else + setWindowTitle(trackInfo); + +} + +void Player::setStatusInfo(const QString &info) +{ + statusInfo = info; + + if (!statusInfo.isEmpty()) + setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo)); + else + setWindowTitle(trackInfo); +} + +void Player::showColorDialog() +{ + if (!colorDialog) { + QSlider *brightnessSlider = new QSlider(Qt::Horizontal); + brightnessSlider->setRange(-100, 100); + brightnessSlider->setValue(videoWidget->brightness()); + connect(brightnessSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setBrightness(int))); + connect(videoWidget, SIGNAL(brightnessChanged(int)), brightnessSlider, SLOT(setValue(int))); + + QSlider *contrastSlider = new QSlider(Qt::Horizontal); + contrastSlider->setRange(-100, 100); + contrastSlider->setValue(videoWidget->contrast()); + connect(contrastSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setContrast(int))); + connect(videoWidget, SIGNAL(contrastChanged(int)), contrastSlider, SLOT(setValue(int))); + + QSlider *hueSlider = new QSlider(Qt::Horizontal); + hueSlider->setRange(-100, 100); + hueSlider->setValue(videoWidget->hue()); + connect(hueSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setHue(int))); + connect(videoWidget, SIGNAL(hueChanged(int)), hueSlider, SLOT(setValue(int))); + + QSlider *saturationSlider = new QSlider(Qt::Horizontal); + saturationSlider->setRange(-100, 100); + saturationSlider->setValue(videoWidget->saturation()); + connect(saturationSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setSaturation(int))); + connect(videoWidget, SIGNAL(saturationChanged(int)), saturationSlider, SLOT(setValue(int))); + + QFormLayout *layout = new QFormLayout; + layout->addRow(tr("Brightness"), brightnessSlider); + layout->addRow(tr("Contrast"), contrastSlider); + layout->addRow(tr("Hue"), hueSlider); + layout->addRow(tr("Saturation"), saturationSlider); + + colorDialog = new QDialog(this); + colorDialog->setWindowTitle(tr("Color Options")); + colorDialog->setLayout(layout); + } + colorDialog->show(); +} diff --git a/doc/src/snippets/multimedia-snippets/soundeffect.qml b/doc/src/snippets/multimedia-snippets/soundeffect.qml new file mode 100644 index 000000000..080e378ec --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/soundeffect.qml @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [complete snippet] +import Qt 4.7 +import QtMultimediaKit 1.1 + +Text { + text: "Click Me!"; + font.pointSize: 24; + width: 150; height: 50; + + //! [play sound on click] + SoundEffect { + id: playSound + source: "soundeffect.wav" + } + MouseArea { + id: playArea + anchors.fill: parent + onPressed: { playSound.play() } + } + //! [play sound on click] +} +//! [complete snippet] diff --git a/doc/src/snippets/multimedia-snippets/video.cpp b/doc/src/snippets/multimedia-snippets/video.cpp new file mode 100644 index 000000000..d300f32a6 --- /dev/null +++ b/doc/src/snippets/multimedia-snippets/video.cpp @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* Video related snippets */ +#include "qvideorenderercontrol.h" +#include "qmediaservice.h" +#include "qmediaplayer.h" +#include "qabstractvideosurface.h" +#include "qvideowidgetcontrol.h" +#include "qvideowindowcontrol.h" +#include "qgraphicsvideoitem.h" + +#include <QFormLayout> +#include <QGraphicsView> + +class VideoExample : public QObject { + Q_OBJECT +public: + void VideoGraphicsItem(); + void VideoRendererControl(); + void VideoWidget(); + void VideoWindowControl(); + void VideoWidgetControl(); + +private: + // Common naming + QMediaService *mediaService; + QVideoWidget *videoWidget; + QWidget *widget; + QFormLayout *layout; + QAbstractVideoSurface *myVideoSurface; + QMediaPlayer *player; + QMediaContent video; + QGraphicsView *graphicsView; +}; + +void VideoExample::VideoRendererControl() +{ + //! [Video renderer control] + QVideoRendererControl *rendererControl = mediaService->requestControl<QVideoRendererControl *>(); + rendererControl->setSurface(myVideoSurface); + //! [Video renderer control] +} + +void VideoExample::VideoWidget() +{ + //! [Video widget] + player = new QMediaPlayer; + + videoWidget = new QVideoWidget; + + player->setVideoOutput(videoWidget); + player->setMedia(QUrl("http://example.com/movie.mp4")); + + videoWidget->show(); + player->play(); + //! [Video widget] +} + +void VideoExample::VideoWidgetControl() +{ + //! [Video widget control] + QVideoWidgetControl *widgetControl = mediaService->requestControl<QVideoWidgetControl *>(); + layout->addWidget(widgetControl->videoWidget()); + //! [Video widget control] +} + +void VideoExample::VideoWindowControl() +{ + //! [Video window control] + QVideoWindowControl *windowControl = mediaService->requestControl<QVideoWindowControl *>(); + windowControl->setWinId(widget->winId()); + windowControl->setDisplayRect(widget->rect()); + windowControl->setAspectRatioMode(Qt::KeepAspectRatio); + //! [Video window control] +} + +void VideoExample::VideoGraphicsItem() +{ + //! [Video graphics item] + player = new QMediaPlayer(this); + + QGraphicsVideoItem *item = new QGraphicsVideoItem; + player->setVideoOutput(item); + graphicsView->scene()->addItem(item); + graphicsView->show(); + + player->setMedia(video); + player->play(); + //! [Video graphics item] +} |