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.
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?
On 20 Apr 2018, at 18:23, Proxy Hossain <proxyh...@gmail.com> wrote:
Anyone please help me, i am stuck here for long time.
On 24 Apr 2018, at 18:16, Proxy Hossain <proxyh...@gmail.com> wrote:Problem solved.