Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Developer
Posts: 132
Registered: ‎08-24-2011
My Device: Z10 LE + Playbook
My Carrier: MTS Ukraine
Accepted Solution

Reading mp3 tags

hello there,

 

is there an easy way to read mp3 tags since Phonon is not supported right now? i've found  Taglib, but have no idea how to compile it for cascades and use it.

 

any help will be appreciated!

Thank you!
----------------------------
My Apps: Alchemy Game | Image Resizer | Jam! | Traffic Signs: USA | Radio Record
My Blog: http://bb10pro.com
Developer
Developer
Posts: 132
Registered: ‎08-24-2011
My Device: Z10 LE + Playbook
My Carrier: MTS Ukraine

Re: Reading mp3 tags

Here is a simple code. You can modify it as you wish

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TagReader.hpp"

#define MP3_FILENAME "/accounts/1000/shared/music/Dub_Fx.mp3"

typedef struct{
   char tag[3];
   char title[30];
   char artist[30];
   char album[30];
   char year[4];
   char comment[30];
   unsigned char genre;
} mp3Tag;

QString TagReader()
{

	FILE *fp = fopen(MP3_FILENAME, "rb");
	if (!fp)
	{
		perror("File open failed");
		return "1";
	}

	mp3Tag tag;
	// Seek to 128 bytes before the end of the file
	if (fseek(fp, -1 * sizeof(mp3Tag), SEEK_END) == -1)
	{
		perror("fseek failed");
		return "2";
	}

	// Read the tag
	if (fread(&tag, sizeof(mp3Tag), 1, fp) != 1)
	{
		fprintf(stderr, "Failed reading tag\n");
		return "3";
	}

	// Make sure we've got what we expect.
	if (memcmp(tag.tag, "TAG", 3) == 0)
	{
		// Found the tag where we expected
		printf("Title: %.30s\n", tag.title);
		printf("Artist: %.30s\n", tag.artist);
		printf("Album: %.30s\n", tag.album);
		printf("Year: %.4s\n", tag.year);

		if (tag.comment[28] == '\0')
		{
			printf("Comment: %.28s\n", tag.comment);
			printf("Track: %d\n", tag.comment[29]);
		}
		else
		{
			printf("Comment: %.30s\n", tag.comment);
		}
		printf("Genre: %d\n", tag.genre);
	}
	else
	{
		fprintf(stderr, "Failed to find TAG\n");
		return "4";
	}
	return tag.title;
}

 

Thank you!
----------------------------
My Apps: Alchemy Game | Image Resizer | Jam! | Traffic Signs: USA | Radio Record
My Blog: http://bb10pro.com
Developer
Posts: 17
Registered: ‎04-06-2013
My Device: Z10
My Carrier: Vodafone

Re: Reading mp3 tags

[ Edited ]

you can use the metaData in mediaplayer 

if you want the code ,my email: **removed**

 

 

 

 

 

 

Removed personal information to comply withCommunity GuidelinesandTerms and Conditions of Use.

Regular Contributor
Posts: 54
Registered: ‎04-05-2013
My Device: Q10
My Carrier: T-Mobile

Re: Reading mp3 tags

[ Edited ]

Does this work for id3v2 v 2.3 as well as 2.4?

 

Regular Contributor
Posts: 54
Registered: ‎04-05-2013
My Device: Q10
My Carrier: T-Mobile

Re: Reading mp3 tags

The code above works for id3v1 tags, but if you want to be able to see id3v2 tags, or tags of other media types, it is better to use the MediaPlayer class.

player.setSourceUrl(url),

player.prepare(),

and connect the player.metaDataChanged() signal to your slot, to get the metadata that way.

 

Use a different MediaPlayer instance from the one you are using to actually play media, so you can scan tracks in the background while you are playing music at the same time.

 

Developer
Posts: 88
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: Reading mp3 tags

After I play() the MediaPlayer;

how can I get the ID3 infor in c++.

The code

 

audioPlayer.metaData["artist"]
audioPlayer.metaData["duration"]
audioPlayer.metaData["title"]

 

is working fine in qml file. But

QvariantMap audioID3 = audioPlayer->metaData();

qDebug << "Artist: " << audioID3["artist"];

 Is not print to console "Artist: artist_name"

it only print "Artist Qvariant(0,)"

 

How can i get ID3 information??

Regular Contributor
Posts: 54
Registered: ‎04-05-2013
My Device: Q10
My Carrier: T-Mobile

Re: Reading mp3 tags

[ Edited ]

You can't qDebug a QVariant like that, you have to convert it to the proper type.

 

    const QVariantMap& vm = ... (metadata from the player or the slot argument)

 

    QString title = vm["title"].toString();
    if (!title.isEmpty()) ptr->setTrackTitle(title);

    QString artist = vm["artist"].toString();
    if (!artist.isEmpty()) ptr->setArtist(artist);

    QString album = vm["album"].toString();
    if (!album.isEmpty()) ptr->setAlbumTitle(album);

   

    int msecs = vm["duration"].toInt();
    QTime t;
    t=t.addMSecs(msecs);
    ptr->setTrackTime(t);

 

Developer
Posts: 88
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: Reading mp3 tags

Smiley Sad i have try it, bro

 

	MediaPlayer *mp = root->findChild<MediaPlayer*>("audioPlayer");
	mp->setSourceUrl(QUrl("/accounts/1000/shared/music/test.mp3"));
	mp->play();

	QVariantMap mp3ID3 = mp->metaData();
	QString artist = mp3ID3["artist"].toString();
	qDebug() "Artist" << artist;

 music had played, but I only get "Artist : "" " on console screen. In Qml

TextArea {
            text: audioPlayer.metaData["artist"]
        }

 it's ok. Can you give me some of your code that worked

Regular Contributor
Posts: 54
Registered: ‎04-05-2013
My Device: Q10
My Carrier: T-Mobile

Re: Reading mp3 tags

[ Edited ]

Sometimes the metadata takes a while to arrive, so you need to connect a signal

from the player's metaDataChanged() to your own slot and read from the variantmap that is passed into the slot.

 

TagReader::TagReader(QObject* parent)
: QObject(parent)
{
    m_isWaiting = false;
    connect (&m_player, SIGNAL(metaDataChanged (const QVariantMap &)),
            this, SLOT(updateMetaData(const QVariantMap&)));
}

 

When it is time to get some metadata, do this:

 

     m_player.setSourceUrl(url);
     m_player.prepare();

 

But you can't read the metadata yet.. Do it from the slot you connected to from the ctor.

 

void TagReader::updateMetaData(const QVariantMap& vm) {
    QUrl url = QUrl::fromLocalFile(vm["uri"].toString()); // assuming it is a local file here
    MetaDataObject* ptr = m_cache[url];

    QString title = vm["title"].toString();
    if (!title.isEmpty()) ptr->setTrackTitle(title);

    QString artist = vm["artist"].toString();
    if (!artist.isEmpty()) ptr->setArtist(artist);

    QString album = vm["album"].toString();
    if (!album.isEmpty()) ptr->setAlbumTitle(album);

    QString trackNumber = vm["track"].toString();
    if (!trackNumber.isEmpty()) ptr->setTrackNumber(trackNumber);

    QString comment = vm["description"].toString();
    if (!comment.isEmpty()) ptr->setComment(comment);

    int msecs = vm["duration"].toInt();
    QTime t;
    t=t.addMSecs(msecs);
    ptr->setTrackTime(t);

    QString genre = vm["genre"].toString();
    if (!genre.isEmpty()) ptr->setGenre(genre);


Developer
Posts: 88
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: Reading mp3 tags

I have try it. it's really worked for me. thankyou very much bro