Created
March 6, 2017 18:40
-
-
Save Kaushal28/d6cee31fc4549c374f62f5e7aedde6b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package purvil12c.musicplayer; | |
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.widget.Toast; | |
import java.io.BufferedInputStream; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetSocketAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.SocketException; | |
import java.util.Calendar; | |
/** | |
* Created by Kaushal28 on 2/24/2017. | |
*/ | |
public class waitForTime extends AsyncTask<Void,Void,Void> { | |
private long timeToPlay; | |
Context context; | |
double lag; | |
waitForTime(Context c){ | |
context = c; | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) { | |
//lag = GroupPlayFragment.getAverageLag(); | |
lag = sendAcceptanceMsg.offset*1000; | |
long finalTimeToPlay = timeToPlay + (long)lag; | |
System.out.println("*****************************"+finalTimeToPlay+"**********************"); | |
Toast.makeText(context,finalTimeToPlay+"",Toast.LENGTH_LONG).show(); | |
GatherActivity.readyMediaPlayer(context); | |
Intent intentAlarm = new Intent(context, startPlayingSong.class); | |
intentAlarm.putExtra("client_or_host","client"); | |
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); | |
alarmManager.set(AlarmManager.RTC_WAKEUP, finalTimeToPlay, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); | |
} | |
@Override | |
protected Void doInBackground(Void... params) { | |
try{ | |
ServerSocket ss = new ServerSocket(); | |
ss.setReuseAddress(true); | |
ss.bind(new InetSocketAddress(5010)); | |
//now here you will get absolute time to play the received song. | |
System.out.println("Waiting for Command to play"); | |
Socket socket = ss.accept(); | |
System.out.println("Accepted!!, Now Playing..."); | |
////// | |
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); | |
// final String connectedDevice = dis.readUTF(); | |
timeToPlay = dis.readLong(); | |
System.out.println("song will play at: " + timeToPlay); | |
//now trigger the play when current time is equal to timeToPlay variable received above! | |
//so, now set alarm at received time, in post execute. | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
//udp broadcast | |
// try { | |
// Log.i("***** UDP client: ", "starting"); | |
// final String text; | |
// int server_port = 6668; | |
// byte[] message = new byte[2048]; | |
// DatagramPacket p = new DatagramPacket(message, message.length); | |
// DatagramSocket s = new DatagramSocket(server_port); | |
// Log.i("***** UDP client: ", "about to wait to receive"); | |
// s.receive(p); // blocks until something is received | |
// Log.i("***** UDP client: ", "received"); | |
// text = new String(message, 0, p.getLength()); | |
// Log.i("*UDP client message: ", text); | |
// | |
// Handler handler = new Handler(context.getMainLooper()); | |
// handler.post( new Runnable(){ | |
// public void run(){ | |
// Toast.makeText(context, text,Toast.LENGTH_LONG).show(); | |
// } | |
// }); | |
// | |
// Log.d("Udp tutorial","message:" + text); | |
// s.close(); | |
// } catch ( SocketException e) { | |
// Log.i("***** UDP client has: ", "Socket Exception"); | |
// } catch (IOException e){ | |
// Log.i("UDPclient has Exception", "e: " + e); | |
// } | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment