Question

I use RTMP on Android to download a stream. If I run the library the first time everything works fine. On second time the app doesn't initiate the RTMP download :/

I searched the last three days and know that I can't load a native library twice or just unload it and that I have three options to handle my problem:

  1. Using a custom class loader (after System.gc() library was still loaded)
  2. Running a service in its own process (it didn't work. The library was still loaded after killing the service).
  3. Write a native library that loads the RTMP library via dlopen and closes it via dlclose.

I don't know any further option :/ I even don't know how to write the native library to load other library :/

I used this RTMP dump: https://github.com/eschriek/rtmpdump-android

Était-ce utile?

La solution

OK, I found found a way to do it :) Maybe it is not a beautiful one, but it works fine:

  1. Create a service:

    import android.app.Service;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.IBinder;
    
    public class rtmpdumpService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            config = this;
            String extras = "";
            if(intent != null){
    
                //Get needed information
                extras = intent.getExtras().getString("rtmp");
            }
            else {
                this.stopSelf();
            }
            doWork(extras);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
        public void doWork(String rtmp){
            //Do work here: for example rtmpdump
            Rtmpdump dump = new Rtmpdump();
            dump.parseString(params[0]);
            System.exit(0);
            this.stopSelf();
        }
    }
    
  2. Register in AndroidManifest as service with these attributes

    android:name=".rtmpdumpService"
    android:exported="false"
    android:process=":rtmp"
  3. Start service:

    Intent rtmpdumpIntent = new Intent(getApplicationContext(), rtmpdumpService.class);
                eSendIntent.putExtra("rtmp", "RTMP CODE");
                startService(rtmpdumpIntent);
    

Sometimes you have to wait until it finishes:

After the Service is started (startService(rtmpdumpIntent):

do {
    try {
        Thread.sleep(500);
    }
    catch (InterruptedException e) {
        //Log
    }
} while( isServiceRunning(rtmpdumpService.class) == true);

isServiceRunning function:

    private boolean isServiceRunning(Class cl) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (cl.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top