×
Namespaces

Variants
Actions

Live radio with Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Windows PC
Compatibility
Platform(s): Windows
Symbian
Article
Keywords: Live Radio
Created: somnathbanik (16 Apr 2014)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview - ActiveQt Framework

This article shows how to embed Windows Media Player 10 into an application using ActiveQt technology and to use it to play Live BBC Radio. Note that this application will only run on Windows desktop platforms.

The Qt for Windows Desktop allows us to combine ActiveX and Qt using ActiveQt Framework. It consists of two modules

1. The QAxContainer allows us to embed ActiveX controls in Qt Application using COM objects. 
2. The QAxServer module allows us to export custom COM objects and ActiveX controls written using Qt.

To know more about ActiveQt and its sample example please visit here ActiveQt Examples

LiveRadioQt.png


LiveRadioQt.pro

Add CONFIG += qaxcontainer to the .pro file

QT       += core gui
TARGET = LiveRadioQt
TEMPLATE = app
CONFIG += qaxcontainer
SOURCES += main.cpp\
liveradioqt.cpp
HEADERS += liveradioqt.h
FORMS += liveradioqt.ui
OTHER_FILES += \
images/bbcws.jpeg \
images/bbca.jpeg \
images/bbc7.jpeg \
images/bbc6.jpeg \
images/bbc5live.jpeg \
images/bbc5.jpeg \
images/bbc4.jpeg \
images/bbc3.jpeg \
images/bbc2.jpeg \
images/bbc1x.jpeg \
images/bbc1.jpeg \
images/BackgroundImage.jpg \
images/bg.jpg
RESOURCES += \
LiveRadioQtResource.qrc

liveradioqt.h

#ifndef LIVERADIOQT_H
#define LIVERADIOQT_H
 
#include <QMainWindow>
#include "QAxBase"
#include "QAxWidget"
#include "QToolButton"
#include "QSlider"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QBoxLayout"
#include "QMessageBox"
#include "QFileDialog"
#include "QDesktopServices"
#include "QPushButton"
#include "QDesktopWidget"
 
namespace Ui {
class LiveRadioQt;
}
 
class LiveRadioQt : public QWidget
{
Q_OBJECT
Q_ENUMS(ReadyStateConstants)
public:
explicit LiveRadioQt(QWidget *parent = 0);
~LiveRadioQt();
 
private:
Ui::LiveRadioQt *ui;
 
 
public:
enum PlayStateConstants { Stopped = 0, Paused = 1, Playing = 2 };
enum ReadyStateConstants { Uninitialized = 0, Loading = 1,
Interactive = 3, Complete = 4 };
 
protected:
void timerEvent(QTimerEvent *event);
 
private slots:
void onPlayStateChange(int oldState, int newState);
void onReadyStateChange(ReadyStateConstants readyState);
void onPositionChange(double oldPos, double newPos);
void sliderValueChanged(int newValue);
void openFile();
 
private:
QAxWidget *wmp;
QAxBase *base;
 
QToolButton *openButton;
QToolButton *playPauseButton;
QToolButton *stopButton;
QSlider *seekSlider;
QString fileFilters;
int updateTimer;
void CreateMeidaPlayerUI();
void SetBackgroundImage();
 
public slots:
void PlayRadio1();
void PlayRadio1x();
void PlayRadio2();
void PlayRadio3();
void PlayRadio4();
void PlayRadio5();
void PlayRadio6();
void PlayRadio7();
void PlayRadioAsia();
void PlayRadioNews();
void PlayRadio5Live();
 
};
 
#endif // LIVERADIOQT_H

Our main application window is LiveRadioQt. The LiveRadioQt class inherits from QWidget. The Q_ENUMS() macro (just below Q_OBJECT) is necessary to tell moc that the ReadyStateConstants type used in the onReadyStateChange() slot is an enum type. In the private section, we declare a QAxWidget * data member.


liveradioqt.cpp

#include "liveradioqt.h"
#include "ui_liveradioqt.h"
 
LiveRadioQt::LiveRadioQt(QWidget *parent) :
QWidget(parent)
{
// ui->setupUi(this);
this->setWindowTitle("BBC Radio Listen Live");
 
 
wmp = new QAxWidget;
 
wmp->setControl("{6BF52A52-394A-11d3-B153-00C04F79FAA6}"); //WMP 10
// wmp->setControl("{22D6F312-B0F6-11D0-94AB-0080C74C7E95}"); //WMP 6.4
 
wmp->setProperty("ShowControls", false);
 
wmp->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(wmp, SIGNAL(PlayStateChange(int, int)),
this, SLOT(onPlayStateChange(int, int)));
connect(wmp, SIGNAL(ReadyStateChange(ReadyStateConstants)),
this, SLOT(onReadyStateChange(ReadyStateConstants)));
connect(wmp, SIGNAL(PositionChange(double, double)),
this, SLOT(onPositionChange(double, double)));
 
SetBackgroundImage();
CreateMeidaPlayerUI();
}
 
LiveRadioQt::~LiveRadioQt()
{
delete ui;
}
void LiveRadioQt::timerEvent(QTimerEvent *event)
{
if (event->timerId() == updateTimer) {
double curPos = wmp->property("CurrentPosition").toDouble();
onPositionChange(-1, curPos);
} else {
QWidget::timerEvent(event);
}
}
 
void LiveRadioQt::onPlayStateChange(int oldState, int newState)
{
 
}
 
void LiveRadioQt:: onReadyStateChange(ReadyStateConstants readyState)
{
 
}
 
void LiveRadioQt:: onPositionChange(double oldPos, double newPos)
{
 
}
 
void LiveRadioQt:: sliderValueChanged(int newValue)
{
 
}
 
void LiveRadioQt:: openFile()
{
 
 
 
}
 
void LiveRadioQt::CreateMeidaPlayerUI()
{
 
QSize IconSize(105,90);
 
QPushButton *ButtonRadio1 = new QPushButton(tr(""));
ButtonRadio1->setIcon(QIcon(":/images/bbc1.jpeg"));
ButtonRadio1->setIconSize(IconSize);
ButtonRadio1->setDefault(true);
ButtonRadio1->setEnabled(true);
ButtonRadio1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio1x = new QPushButton(tr(""));
ButtonRadio1x->setIcon(QIcon(":/images/bbc1x.jpeg"));
ButtonRadio1x->setIconSize(IconSize);
ButtonRadio1x->setDefault(true);
ButtonRadio1x->setEnabled(true);
ButtonRadio1x->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio2 = new QPushButton(tr(""));
ButtonRadio2->setIcon(QIcon(":/images/bbc2.jpeg"));
ButtonRadio2->setIconSize(IconSize);
ButtonRadio2->setDefault(true);
ButtonRadio2->setEnabled(true);
ButtonRadio2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio3 = new QPushButton(tr(""));
ButtonRadio3->setIcon(QIcon(":/images/bbc3.jpeg"));
ButtonRadio3->setIconSize(IconSize);
ButtonRadio3->setDefault(true);
ButtonRadio3->setEnabled(true);
ButtonRadio3->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio4 = new QPushButton(tr(""));
ButtonRadio4->setIcon(QIcon(":/images/bbc4.jpeg"));
ButtonRadio4->setIconSize(IconSize);
ButtonRadio4->setDefault(true);
ButtonRadio4->setEnabled(true);
ButtonRadio4->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio5 = new QPushButton(tr(""));
ButtonRadio5->setIcon(QIcon(":/images/bbc5.jpeg"));
ButtonRadio5->setIconSize(IconSize);
ButtonRadio5->setDefault(true);
ButtonRadio5->setEnabled(true);
ButtonRadio5->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio6 = new QPushButton(tr(""));
ButtonRadio6->setIcon(QIcon(":/images/bbc6.jpeg"));
ButtonRadio6->setIconSize(IconSize);
ButtonRadio6->setDefault(true);
ButtonRadio6->setEnabled(true);
ButtonRadio6->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio7 = new QPushButton(tr(""));
ButtonRadio7->setIcon(QIcon(":/images/bbc7.jpeg"));
ButtonRadio7->setIconSize(IconSize);
ButtonRadio7->setDefault(true);
ButtonRadio7->setEnabled(true);
ButtonRadio7->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadioAsia = new QPushButton(tr(""));
ButtonRadioAsia->setIcon(QIcon(":/images/bbca.jpeg"));
ButtonRadioAsia->setIconSize(IconSize);
ButtonRadioAsia->setDefault(true);
ButtonRadioAsia->setEnabled(true);
ButtonRadioAsia->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadioNews = new QPushButton(tr(""));
ButtonRadioNews->setIcon(QIcon(":/images/bbcws.jpeg"));
ButtonRadioNews->setIconSize(IconSize);
ButtonRadioNews->setDefault(true);
ButtonRadioNews->setEnabled(true);
ButtonRadioNews->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
QPushButton *ButtonRadio5Live = new QPushButton(tr(""));
ButtonRadio5Live->setIcon(QIcon(":/images/bbc5live.jpeg"));
ButtonRadio5Live->setIconSize(IconSize);
ButtonRadio5Live->setDefault(true);
ButtonRadio5Live->setEnabled(true);
ButtonRadio5Live->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
 
 
 
QVBoxLayout *iPlayerLayout = new QVBoxLayout(); // player layout
iPlayerLayout->addWidget(wmp);
 
 
 
// button layout
QHBoxLayout *iButtonLayout1 = new QHBoxLayout();
QHBoxLayout *iButtonLayout2 = new QHBoxLayout();
QHBoxLayout *iButtonLayout3 = new QHBoxLayout();
 
 
//add button to the layout 1
iButtonLayout1->addWidget(ButtonRadio1);
iButtonLayout1->addWidget(ButtonRadio1x);
iButtonLayout1->addWidget(ButtonRadio2);
iButtonLayout1->addWidget(ButtonRadio3);
iButtonLayout1->setAlignment(Qt::AlignCenter);
 
 
// add buttons to layout 2
iButtonLayout2->addWidget(ButtonRadio4);
iButtonLayout2->addWidget(ButtonRadio5);
iButtonLayout2->addWidget(ButtonRadio5Live);
iButtonLayout2->addWidget(ButtonRadio6);
iButtonLayout2->setAlignment(Qt::AlignCenter);
 
//add buttons to layout 3
iButtonLayout3->addWidget(ButtonRadio7);
iButtonLayout3->addWidget(ButtonRadioAsia);
iButtonLayout3->addWidget(ButtonRadioNews);
 
iButtonLayout3->setAlignment(Qt::AlignLeft);
 
 
 
 
 
QVBoxLayout *iMainLayout = new QVBoxLayout();
iMainLayout->addLayout(iPlayerLayout);
iMainLayout->addLayout(iButtonLayout1);
iMainLayout->addLayout(iButtonLayout2);
iMainLayout->addLayout(iButtonLayout3);
 
//display layout
this->setLayout(iMainLayout);
 
 
// Signal and slots
connect(ButtonRadio1,SIGNAL(clicked()),this,SLOT(PlayRadio1()));
connect(ButtonRadio1x,SIGNAL(clicked()),this,SLOT(PlayRadio1x()));
connect(ButtonRadio2,SIGNAL(clicked()),this,SLOT(PlayRadio2()));
connect(ButtonRadio3,SIGNAL(clicked()),this,SLOT(PlayRadio3()));
connect(ButtonRadio4,SIGNAL(clicked()),this,SLOT(PlayRadio4()));
connect(ButtonRadio5,SIGNAL(clicked()),this,SLOT(PlayRadio5()));
connect(ButtonRadio6,SIGNAL(clicked()),this,SLOT(PlayRadio6()));
connect(ButtonRadio7,SIGNAL(clicked()),this,SLOT(PlayRadio7()));
connect(ButtonRadioAsia,SIGNAL(clicked()),this,SLOT(PlayRadioAsia()));
connect(ButtonRadioNews,SIGNAL(clicked()),this,SLOT(PlayRadioNews()));
connect(ButtonRadio5Live,SIGNAL(clicked()),this,SLOT(PlayRadio5Live()));
 
 
}
void LiveRadioQt::PlayRadio1()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/radio1/radio1_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio1x()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/1xtra/1xtra_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio2()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/radio2/radio2_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio3()
{
QString link = "mms://wmlive.bbc.net.uk/wms/bbc_ami/radio3/radio3_bb_live_int_eq1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio4()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/radio4/radio4_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio5()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/radio5/5spxtra_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio6()
{
QString link = "mms://wmlive.bbc.net.uk/wms/bbc_ami/6music/6music_bb_live_int_eq1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio7()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/bbc7/bbc7_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadioAsia()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/asiannet/asiannet_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadioNews()
{
QString link = "mms://livewmstream-ws.bbc.co.uk.edgestreams.net/reflector:47971";
wmp->setProperty("URL",link); // auto play
}
void LiveRadioQt::PlayRadio5Live()
{
QString link = "mms://wm-live.bbc.net.uk/wms/bbc_ami/radio5/radio5_bb_live_int_ep1_sl0";
wmp->setProperty("URL",link); // auto play
}
 
 
void LiveRadioQt::SetBackgroundImage()
{
//Using QPalette you can set background image as follows.
QPalette p = palette();
 
//Load image to QPixmap, Give full path of image
QPixmap pixmap1(":/images/bg.jpg");
 
//resize image if it is larger than screen size.
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
QSize size(rect.width() , rect.height());
//resize as per your requirement..
QPixmap pixmap(pixmap1.scaled(size));
 
p.setBrush(QPalette::Background, pixmap);
setPalette(p);
}


In Constructor we embed the Windows Media ActiveX Control using QAxWidget. The QAxContainer module consists of three classes: QAxObject encapsulates a COM object, QAxWidget encapsulates an ActiveX control, and QAxBase implements the core COM functionality for QAxObject and QAxWidget. We call setControl() on the QAxWidget with the class ID of the Windows Media Player 10. This will create an instance of the required component. This will help us to access all the methods, events etc of ActiveX control through Qt properties. The COM data types are automatically converted into the corresponding Qt types.

About Windows Media Player Compatibility Layer

The compatibility layer translates this method to use the 9 Series or later object model URL property instead, it AutoStart the file/url. 9 Series or later URL property is asynchronous

Player9.URL = "mms://www.server.com/file.wmv";


Please Note: The BBC Live Radio Links that I have used in this application are just for testing purpose and should not be used commercially.



Source Code

The full source code presented in this article is available here File:LiveRadioQt.zip



--somnathbanik

This page was last modified on 11 October 2012, at 04:17.
101 page views in the last 30 days.
×