Android Library that can stream Mjpeg Video and save video frames in images with ForeGround Service(Kotlin)

Faizk
2 min readJul 21, 2020

--

Recently I was working on one project where I need to show a custom view in my Android application. After some digging, I came across libvlc of VLC and mjpeg library. You can find lots of mjpeg library in Github. But what I want to do in my application is not only show custom view but also to save the frames in jpeg format in some directory. That was easily handled using InputStreams.

I also wanted my application to run in the background, i.e, if the app is in the background, images will still be saved in the file directory. Integrating foreground service do the job.

I Created my own Android Library which can do all these things. You just need to import the dependencies. This library is a wrapper library around the https://github.com/perthcpe23/android-mjpeg-view which is written in Java. My library is written in Kotlin with additional features.

USAGES

- Able to stream video in an android application.
- Requires only Http URL to make connection.
- Ability to save video frames in images in the directory Mjpeg.
- Can save the image in the background(Foreground service integration).

Installation

Add Jitpack to your project build.gradle file

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
}

Then add this dependency to your app build.gradle file.

dependencies {
implementation 'com.github.faizkhan12:mjpeg-view-android-kotlin:v1.0.0
}

Add a view to XML layout:

<com.faizkhan.mjpegviewer.MjpegView
android:id="@+id/mjpegid"
android:layout_width="wrap_content"
android:layout_height="473dp"
/>

Add 2 buttons for startService and stopService

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startService"
android:text="Start Service"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stopService"
android:text="Stop Service"
android:layout_marginLeft="120dp"
/>

Specify mjpeg source and start streaming

Kotlin
private var view: MjpegView? = null
view = findViewById(R.id.mjpegid)
view!!.isAdjustHeight = true
view!!.mode1 = MjpegView.MODE_FIT_WIDTH
view!!.setUrl("<<Your HTTP URL>>")
view!!.isRecycleBitmap1 = true
view!!.startStream()
// when user leaves application
viewer!!.stopStream();

Start Foreground Service

val serviceIntent = Intent(this, ForegroundService::class.java)
startService(serviceIntent)

Stop Foreground Service

val stopIntent = Intent(this, ForegroundService::class.java)
stopService(stopIntent)

This is pretty much ou have to do to make use of this library. Remember it is open source and everyone is welcome to contribute to this project.

P.S

All samples from the article are available in the library repository.

https://github.com/faizkhan12/mjpeg-view-android-kotlin

--

--