add user input
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / MyService.java
1 package ai.suanzi.rtmpclient;
2
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;
15
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 {
23         private String url;
24         public FfmpegRunnable(String _url){
25             this.url = _url;
26         }
27         @Override
28         public void run(){
29             Log.e(TAG, "Run ffmpeg, url: " + url);
30             isRunning = true;
31             ffmpeg.push(null, this.url);
32         }
33     }
34
35     /**
36      * id不可设置为0,否则不能设置为前台service
37      */
38     private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
39
40     /**
41      * Notification
42      */
43     public void createNotification(){
44         Log.e(TAG, "create notification");
45         //使用兼容版本
46         NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
47         //设置状态栏的通知图标
48         builder.setSmallIcon(R.mipmap.ic_launcher);
49         //设置通知栏横条的图标
50         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));
51         //禁止用户点击删除按钮删除
52         builder.setAutoCancel(false);
53         //禁止滑动删除
54         builder.setOngoing(true);
55         //右上角的时间显示
56         builder.setShowWhen(true);
57         //设置通知栏的标题内容
58         builder.setContentTitle("Rtmp Foreground Service!!!");
59         //创建通知
60         Notification notification = builder.build();
61         //设置为前台服务
62         startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification);
63     }
64
65
66
67     @Override
68     public IBinder onBind(Intent intent) {
69         return null;
70     }
71
72     @Override
73     public void onCreate() {
74         super.onCreate();
75         Log.e(TAG, "onCreate");
76     }
77
78     @Override
79     public void onDestroy() {
80         stopForeground(true);
81         Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
82         Log.e(TAG, "onDestroy");
83         super.onDestroy();
84
85     }
86
87     @Override
88     public void onStart(Intent intent, int startid){
89         super.onStart(intent, startid);
90         Log.e(TAG, "onStart");
91
92     }
93
94     @Override
95     public int onStartCommand(Intent intent, int flags, int startId) {
96         Log.e(TAG, "onStartCommand");
97
98         String url = intent.getExtras().getString("url");
99         Log.e(TAG, "Url is: " + url);
100         runnable = new FfmpegRunnable(url);
101         //this.url = url;
102         if (!isRunning) {
103             createNotification();
104             Toast.makeText(this, "Video stream pushed to " + url, Toast.LENGTH_LONG).show();
105             new Thread(runnable).start();
106         }
107         //super.onStartCommand(intent, flags, startId);
108         return START_STICKY;
109     }
110
111     @Override
112     public void onLowMemory(){
113         super.onLowMemory();
114         Log.e(TAG, "onLowMemory");
115     }
116 }