# Media Helper Module ## Import ```gradle dependencies { implementation project(':exportmedia') } ``` ## Init 需要先在Application中init mediaHelper類別才能使用 ```kotlin class App : Application() { private val mediaHelper = MediaHelper() override fun onCreate() { super.onCreate() mediaHelper.init(this) } } ``` ## Audio Editor 剪裁與編輯單音軌時間與音量設定 ```kotlin val audioEditorBuilder = AudioEditor.Builder(mediaHelper) audioEditorBuilder.editAudioFile = File() audioEditorBuilder.startTime = "00:00:12" audioEditorBuilder.endTime = "00:00:30" audioEditorBuilder.volume = .3f val audioEditor = audioEditorBuilder.build() audioEditor.output(File()) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer { override fun onComplete() {} override fun onSubscribe(d: Disposable) {} override fun onNext(t: String) {} override fun onError(e: Throwable) {} }) ``` ## Audio Looper 音軌循環播放設定 ```kotlin val audioLoopBuilder = AudioLooper.Builder(mediaHelper) audioLoopBuilder.loopAudioFile = File() audioLoopBuilder.loopDuration = 20000 //mSec val audioLooper = audioLoopBuilder.build() audioLooper.output(File()) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer { override fun onComplete() {} override fun onSubscribe(d: Disposable) {} override fun onNext(t: String) {} override fun onError(e: Throwable) {} }) ``` ## Audio Muxer 多音軌合併至單一音軌 ```kotlin val audioMuxBuilder = AudioMuxer.Builder(mediaHelper) for (file in inputFileArray) { audioMuxBuilder.addInputSource(file) } val audioMuxer = audioMuxBuilder.build() audioMuxer.output(File()) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer { override fun onComplete() {} override fun onSubscribe(d: Disposable) {} override fun onNext(t: String) {} override fun onError(e: Throwable) {} }) ``` ## Movie Maker 圖片素材與音軌素材編碼成mp4影片 ```kotlin val movieMakerBuilder = MovieMaker.Builder(mediaHelper) for (bitmap in bitmapArray) { movieMakerBuilder.addImage(bitmapArray.indexOf(bitmap),bitmap) } movieMakerBuilder.audioStartTime = "00:00:12" movieMakerBuilder.audioFile = File() val movieMaker = movieMakerBuilder.build() movieMaker.output(File()) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer { override fun onComplete() {} override fun onSubscribe(d: Disposable) {} override fun onNext(t: String) {} override fun onError(e: Throwable) {} }) ```