java - Live Streaming of a music file in Android -
for learning purpose, tried creating client , server. 1 person sends udp packets of music file , other person receives it. created such app , packets being transmitted wanted. problem when after receiving packet, try play it, sound played nothing noise. meaningless sound comes speakers.
to test process of sending , receiving packets, tried replacing audio file text file , check contents of packets @ receiver side. noticed packets received highly random. mean not in correct order.
i know in udp packets can reach destination in order there no error control or anything. question how should correct sound out of speakers. if packets transmitted in order abc , received in order bca sound never correct. there way can convert byte array of packets meaningful sound.
i know one solution use tcp tcp not live streaming. for purposes live streaming , video conferencing, udp used. correct way gather , play received packets. appreciated.
code sender:
inputstream songinputstream = new fileinputstream("/sdcard/song.mp3"); byte[] buffer = new byte[1024]; try { clientsocket = new datagramsocket(); } catch (socketexception e1) { // todo auto-generated catch block e1.printstacktrace(); } while ((songinputstream.read(buffer, 0, buffer.length)) > -1) { datagrampacket packet = new datagrampacket(buffer, buffer.length, hostinet, serverport); try { clientsocket.send(packet); log.e(tag, "packet sent " + serviceinfo.getservicename().tostring()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
code on receiver side:
byte[] buffer = new byte[1024]; datagrampacket packet = new datagrampacket(buffer, buffer.length); { try { datagramsocket.receive(packet); log.e(tag, "recieved packet ip " + packet.getaddress().tostring() + " , port = " + packet.getport() ); buffer = packet.getdata(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } while (true);
now buffer contains packets sent sender not coming in correct order. how play sound using transmitted packets.
Comments
Post a Comment