-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from kifio/improvments
Fix lifecycle issue, leads to crash in VIdeoSurfaceView and add saving video to sdcard.
- Loading branch information
Showing
73 changed files
with
2,016 additions
and
491 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.sherazkhilji.sample" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.sherazkhilji.sample" | ||
tools:ignore="GoogleAppIndexingWarning"> | ||
|
||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | ||
<uses-permission android:name="android.permission.CAMERA"/> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
|
||
<uses-feature | ||
android:glEsVersion="0x00020000" | ||
android:required="true"/> | ||
android:required="true" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:hardwareAccelerated="true" | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme"> | ||
android:theme="@style/Theme.AppCompat"> | ||
<activity | ||
android:name="com.videffects.sample.SamplePlayerActivity" | ||
android:name="com.videffects.sample.view.SamplePlayerActivity" | ||
android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation" | ||
android:label="@string/app_name" | ||
android:screenOrientation="landscape"> | ||
<!-- <intent-filter>--> | ||
<!-- <action android:name="android.intent.action.MAIN"/>--> | ||
<!-- <category android:name="android.intent.category.LAUNCHER"/>--> | ||
<!-- </intent-filter>--> | ||
</activity> | ||
|
||
<activity | ||
android:name="com.videffects.sample.view.AssetsGalleryActivity" | ||
android:screenOrientation="portrait"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
<category android:name="android.intent.category.LAUNCHER"/> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
|
||
<activity | ||
android:name="com.videffects.sample.view.VideoActivity" | ||
android:screenOrientation="portrait" /> | ||
|
||
</application> | ||
|
||
</manifest> |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions
167
app/src/main/java/com/videffects/sample/controller/VideoController.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.videffects.sample.controller | ||
|
||
import android.content.res.AssetFileDescriptor | ||
import android.media.MediaPlayer | ||
import android.media.MediaPlayer.MEDIA_ERROR_UNKNOWN | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.util.Log | ||
import android.widget.SeekBar | ||
import android.widget.Toast | ||
import androidx.annotation.RequiresApi | ||
import com.sherazkhilji.videffects.filter.AutoFixFilter | ||
import com.sherazkhilji.videffects.filter.GrainFilter | ||
import com.sherazkhilji.videffects.filter.HueFilter | ||
import com.sherazkhilji.videffects.filter.NoEffectFilter | ||
import com.sherazkhilji.videffects.interfaces.ConvertResultListener | ||
import com.sherazkhilji.videffects.interfaces.Filter | ||
import com.sherazkhilji.videffects.interfaces.ShaderInterface | ||
import com.sherazkhilji.videffects.model.Metadata | ||
import com.videffects.sample.interfaces.OnSelectShaderListener | ||
import com.videffects.sample.interfaces.ProgressChangeListener | ||
import com.videffects.sample.model.* | ||
import com.videffects.sample.view.ShaderChooserDialog | ||
import com.videffects.sample.view.VideoActivity | ||
import java.io.File | ||
|
||
class VideoController(private var activity: VideoActivity?, | ||
filename: String) { | ||
|
||
companion object { | ||
private const val TAG = "kVideoController" | ||
} | ||
|
||
private var filter: Filter = NoEffectFilter() | ||
|
||
private var assetFileDescriptor: AssetFileDescriptor = activity?.assets?.openFd(filename) | ||
?: throw RuntimeException("Asset not found") | ||
|
||
private var mediaPlayer: MediaPlayer = MediaPlayer() | ||
private var metadata: Metadata? = AssetsMetadataExtractor().extract(assetFileDescriptor) | ||
|
||
private val intensityChangeListener: ProgressChangeListener = object : ProgressChangeListener() { | ||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { | ||
filter.run { | ||
when (this) { | ||
is GrainFilter -> setIntensity(transformGrain(progress)) | ||
is HueFilter -> setIntensity(transformHue(progress)) | ||
is AutoFixFilter -> setIntensity(transformAutofix(progress)) | ||
else -> activity?.showToast("Changing intensity not implemented for selected effect in this demo") | ||
} | ||
} | ||
} | ||
} | ||
|
||
init { | ||
setupMediaPlayer() | ||
setupView() | ||
} | ||
|
||
private fun setupMediaPlayer() { | ||
mediaPlayer.isLooping = true | ||
mediaPlayer.setDataSource( | ||
assetFileDescriptor.fileDescriptor, | ||
assetFileDescriptor.startOffset, | ||
assetFileDescriptor.length | ||
) | ||
|
||
// https://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener | ||
mediaPlayer.setOnErrorListener { _, what, extra -> | ||
Log.d(TAG, "OnError! What: $what; Extra: $extra") | ||
false | ||
} | ||
|
||
mediaPlayer.setOnCompletionListener { | ||
Log.d(TAG, "OnComplete!") | ||
} | ||
} | ||
|
||
private fun setupView() { | ||
val metadata = this.metadata | ||
val activity = this.activity | ||
if (metadata != null && activity != null) { | ||
activity.setupVideoSurfaceView(mediaPlayer, metadata.width, metadata.height) | ||
activity.setupSeekBar(intensityChangeListener) | ||
} | ||
} | ||
|
||
fun chooseShader() { | ||
val videoWidth = metadata?.width?.toInt() ?: return | ||
val videoHeight = metadata?.height?.toInt() ?: return | ||
|
||
val dialog = ShaderChooserDialog.newInstance(videoWidth, videoHeight) | ||
dialog.setListener(object : OnSelectShaderListener { | ||
override fun onSelectShader(shader: Any) { | ||
when (shader) { | ||
is ShaderInterface -> { | ||
filter = NoEffectFilter() | ||
activity?.onSelectShader(shader) | ||
} | ||
is Filter -> { | ||
filter = shader | ||
activity?.onSelectFilter(shader) | ||
} | ||
else -> return | ||
} | ||
} | ||
}) | ||
|
||
activity?.let { | ||
dialog.show(it.supportFragmentManager, ShaderChooserDialog::class.java.simpleName) | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
fun saveVideo() { | ||
|
||
if (filter is NoEffectFilter) { | ||
activity?.showToast("Saving will work only with Filters.") | ||
} | ||
|
||
val parent = activity?.getExternalFilesDir(Environment.DIRECTORY_PICTURES) | ||
?: throw RuntimeException("Activity is destroyed!") | ||
val child = "out.mp4" | ||
val outPath = File(parent, child).toString() | ||
val assetConverterThread = AssetConverterThread( | ||
AssetsConverter(assetFileDescriptor), | ||
filter, | ||
outPath, | ||
object : ConvertResultListener { | ||
|
||
override fun onSuccess() { | ||
activity?.onFinishSavingVideo("Video successfully saved at $outPath") | ||
} | ||
|
||
override fun onFail() { | ||
activity?.onFinishSavingVideo("Video wasn't saved. Check log for details.") | ||
} | ||
} | ||
) | ||
|
||
activity?.onStartSavingVideo() | ||
assetConverterThread.start() | ||
} | ||
|
||
fun onPause() { | ||
mediaPlayer.pause() | ||
} | ||
|
||
fun onDestroy() { | ||
mediaPlayer.stop() | ||
mediaPlayer.release() | ||
assetFileDescriptor.close() | ||
activity = null | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
private class AssetConverterThread(private var assetsConverter: AssetsConverter, | ||
private var filter: Filter, | ||
private var outPath: String, | ||
private var listener: ConvertResultListener) : Thread() { | ||
|
||
override fun run() { | ||
super.run() | ||
assetsConverter.startConverter(filter, outPath, listener) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/videffects/sample/interfaces/OnSelectShaderListener.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.videffects.sample.interfaces | ||
|
||
interface OnSelectShaderListener { | ||
|
||
/** | ||
* Argument should implement ShaderInterface of Filter | ||
*/ | ||
fun onSelectShader(shader: Any) | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/videffects/sample/interfaces/ProgressChangeListener.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.videffects.sample.interfaces | ||
|
||
import android.widget.SeekBar | ||
|
||
/** | ||
* Implement placeholder for unused methods of OnSeekBarChangeListener | ||
*/ | ||
abstract class ProgressChangeListener : SeekBar.OnSeekBarChangeListener { | ||
|
||
override fun onStartTrackingTouch(seekBar: SeekBar?) { | ||
|
||
} | ||
|
||
override fun onStopTrackingTouch(seekBar: SeekBar?) { | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/videffects/sample/model/AssetsConverter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.videffects.sample.model; | ||
|
||
import android.content.res.AssetFileDescriptor; | ||
|
||
import com.sherazkhilji.videffects.model.Converter; | ||
import com.sherazkhilji.videffects.model.Metadata; | ||
|
||
import java.io.IOException; | ||
|
||
public class AssetsConverter extends Converter { | ||
|
||
public AssetsConverter(AssetFileDescriptor assetFileDescriptor) { | ||
setMetadata(assetFileDescriptor); | ||
try { | ||
videoExtractor.setDataSource( | ||
assetFileDescriptor.getFileDescriptor(), | ||
assetFileDescriptor.getStartOffset(), | ||
assetFileDescriptor.getLength()); | ||
audioExtractor.setDataSource( | ||
assetFileDescriptor.getFileDescriptor(), | ||
assetFileDescriptor.getStartOffset(), | ||
assetFileDescriptor.getLength()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void setMetadata(AssetFileDescriptor assetFileDescriptor) { | ||
Metadata metadata = new AssetsMetadataExtractor().extract(assetFileDescriptor); | ||
if (metadata != null) { | ||
width = (int) metadata.getWidth(); | ||
height = (int) metadata.getHeight(); | ||
bitrate = metadata.getBitrate(); | ||
} | ||
} | ||
} |
Oops, something went wrong.