1 package ai.suanzi.rtmpclient;
3 import android.app.Service;
4 import android.content.Intent;
5 import android.os.Handler;
6 import android.os.HandlerThread;
7 import android.os.IBinder;
8 import android.os.Looper;
9 import android.util.Log;
10 import android.widget.Toast;
11 import android.support.v4.app.NotificationCompat;
12 import android.graphics.BitmapFactory;
13 import android.app.Notification;
14 import android.os.Message;
16 public class MyService extends Service {
17 private static final String TAG = "MyService";
18 private Ffmpeg ffmpeg = Ffmpeg.getInstance();
19 private Boolean isRunning = false;
20 //private String url = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
21 private FfmpegRunnable runnable;
22 private class FfmpegRunnable implements Runnable {
24 public FfmpegRunnable(String _url){
29 Log.e(TAG, "Run ffmpeg, url: " + url);
31 ffmpeg.push(null, this.url);
36 * id不可设置为0,否则不能设置为前台service
38 private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
43 public void createNotification(){
44 Log.e(TAG, "create notification");
46 NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
48 builder.setSmallIcon(R.mipmap.ic_launcher);
50 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));
52 builder.setAutoCancel(false);
54 builder.setOngoing(true);
56 builder.setShowWhen(true);
58 builder.setContentTitle("Rtmp Foreground Service!!!");
60 Notification notification = builder.build();
62 startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification);
68 public IBinder onBind(Intent intent) {
73 public void onCreate() {
75 Log.e(TAG, "onCreate");
79 public void onDestroy() {
81 Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
82 Log.e(TAG, "onDestroy");
88 public void onStart(Intent intent, int startid){
89 super.onStart(intent, startid);
90 Log.e(TAG, "onStart");
95 public int onStartCommand(Intent intent, int flags, int startId) {
96 Log.e(TAG, "onStartCommand");
98 String url = intent.getExtras().getString("url");
99 Log.e(TAG, "Url is: " + url);
100 runnable = new FfmpegRunnable(url);
103 createNotification();
104 Toast.makeText(this, "Video stream pushed to " + url, Toast.LENGTH_LONG).show();
105 new Thread(runnable).start();
107 //super.onStartCommand(intent, flags, startId);
112 public void onLowMemory(){
114 Log.e(TAG, "onLowMemory");