Publishing Sound(.mp/.3gp etc) File problem using MQTT

948 views
Skip to first unread message

Proxy Hossain

unread,
Apr 16, 2018, 12:30:40 PM4/16/18
to MQTT

Hello

Is it possible to transfer voice/sound file(mp3/wav etc) using mqtt protocol? if possible, how can i publish sound file to broker and how to receive that sound file from subscriber end as a sound file ? 


I tried to convert sound file(.mp3/.3gp) into byte array and send that byte array using publish method but i can not  receive that sound file from my subscribed  client.

if possible plz share an android sample app that transfer sound file through mqtt so that i can understand the coding process.

Nicholas Humfrey

unread,
Apr 17, 2018, 6:22:09 AM4/17/18
to mq...@googlegroups.com, Proxy Hossain
Hi Proxy,

A couple of years ago I wrote a "Push to Talk" script for sending short
audio messages over MQTT.

The idea is that you press a button to record up to 10 seconds of audio,
then it sends that audio to all the other subscribers, which play it
back immediately. You can also send oinks.

The source code is here:
https://github.com/njh/petey


nick.
> --
> To learn more about MQTT please visit http://mqtt.org
> ---
> You received this message because you are subscribed to the Google
> Groups "MQTT" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mqtt+uns...@googlegroups.com.
> To post to this group, send email to mq...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.

Proxy Hossain

unread,
Apr 17, 2018, 12:54:30 PM4/17/18
to MQTT
Actually i dont know ruby. So i couldn't understand your code.

I am following below tutorial to implement mqtt. 

Here message is sent over mqtt. Now i want to sent audio file instead of text.

I tried to convert sound file(.mp3/.3gp) into byte array and send that byte array using publish method but i can not receive that sound file from my subscribed client.

My publish code is

 public void publishMessage(@NonNull MqttAndroidClient client, @NonNull String path, int qos, @NonNull String topic)
        throws MqttException, UnsupportedEncodingException {

FileInputStream fis = new FileInputStream(path); // path is sound file path
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];

for (int readNum; (readNum = fis.read(b)) != -1;) {
    bos.write(b, 0, readNum);
}

byte[] bytes = bos.toByteArray();

 MqttMessage message = new MqttMessage(bytes);
    message.setId(320);
    message.setRetained(true);
    message.setQos(qos);
    client.publish(topic, message);
}

Now i couldn't able to receive this sound file in subscribed client.

Below code is the message notification code of my above mentioned AndroidKt tutorial which i followed

Actually "setMessageNotification()" function is called from "messageArrived()" function. Here messageArrived function received mqttMessage which is sent as byte array when message is published and convert published payload using new String().

 public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            setMessageNotification(s, new String(mqttMessage.getPayload()));
      }

Here message byte array is converted to string using String constructor. Now what should i do to convert "MqttMessage mqttMessage" to audio file.

private void setMessageNotification(@NonNull String topic, @NonNull String msg) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_message_black_24dp)
                    .setContentTitle(topic)
                    .setContentText(msg);
    Intent resultIntent = new Intent(this, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(100, mBuilder.build());
}

Incase of Audio file what i did is, i send payload as byte array to setMessageNotification_sound function and tried convert that byte array to audio file and play using Android media player but nothing happens.

public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            setMessageNotification_sound(s, mqttMessage.getPayload());
        }


private void setMessageNotification_sound(@NonNull String topic, byte[] mp3SoundByteArray) 
{

     // create temp file that will hold byte array
    File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir());
    tempMp3.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempMp3);
    fos.write(mp3SoundByteArray);
    fos.close();

    mediaPlayer.reset();

    FileInputStream fis = new FileInputStream(tempMp3);
    mediaPlayer.setDataSource(fis.getFD());

    mediaPlayer.prepare();
    mediaPlayer.start();
} catch (IOException ex) {
    String s = ex.toString();
    ex.printStackTrace();
}

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_message_black_24dp)
                    .setContentTitle(topic)
                    .setContentText("test");
    Intent resultIntent = new Intent(this, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(100, mBuilder.build());
}

Above method is for message transfer notification in notification panel.Now how i receive and play sound file in above method?



------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Message has been deleted

Proxy Hossain

unread,
Apr 20, 2018, 1:23:34 PM4/20/18
to MQTT
Anyone please help me, i am stuck here for long time.

Andy Stanford-Clark

unread,
Apr 21, 2018, 3:55:38 AM4/21/18
to 'Simon H' via MQTT
This is more of a programming question than an MQTT question.

You should debug this in the “usual way” : make a small file with just a few binary values in, pretend that is your mp3 file. Send it through your system from publisher to subscriber, write it to the file, then look at the content of the file (e.g. with hex dump: "od -ha" in unix) to see if it looks byte-for-byte, exactly what you sent.

If not, you’re doing your conversions wrong. 
It looks like you’re using pretty much the right approach - you just need to debug it.

Andy

On 20 Apr 2018, at 18:23, Proxy Hossain <proxyh...@gmail.com> wrote:

Anyone please help me, i am stuck here for long time.

Proxy Hossain

unread,
Apr 22, 2018, 2:03:05 PM4/22/18
to MQTT
Actually as per my understanding, i think i am doing write in code. But somehow it doesn't works.  That's  why i am posting my problem here so that someone can help me by figure out my  coding mistake.

In the time of  receiving message, directly plain message is sent to   setMessageNotification   function. Here byte array is converted to plain text using "new String (mqttMessage.getPayload())" where mqttMessage.getPayload() is  byte array. 

But in the time of receiving audio file, i directly sent byte array to  setMessageNotification   function using mqttMessage.getPayload() only as parameter. now in the  setMessageNotification  function  i converted byte array to audio file to play, but it doesn't work. Now i want to know am i doing anything wrong when sending byte array to setMessageNotification   function or doing anything wrong in converting byte array to audio file in setMessageNotification   function?
 

Proxy Hossain

unread,
Apr 24, 2018, 1:16:48 PM4/24/18
to MQTT
Problem solved.

Andy Stanford-Clark

unread,
Apr 24, 2018, 6:11:01 PM4/24/18
to mq...@googlegroups.com
are you going to tell us what it was?

On 24 Apr 2018, at 18:16, Proxy Hossain <proxyh...@gmail.com> wrote:

Problem solved.

Proxy Hossain

unread,
Apr 25, 2018, 2:47:44 PM4/25/18
to MQTT
Actually i was saving file in cache directory . As file explorer app on device has no access to the cache dir of app, so i didn't found my transferred file for which i thought there might be problem in my code.

After changing to save device external directory, it solves my problem.
Reply all
Reply to author
Forward
0 new messages