Skip to content

Commit

Permalink
feat: add the components
Browse files Browse the repository at this point in the history
  • Loading branch information
brookmg committed May 4, 2021
1 parent da63475 commit b21201b
Show file tree
Hide file tree
Showing 16 changed files with 586 additions and 10 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
targetSdkVersion 30
versionCode 1
versionName "1.0"

multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -34,6 +34,11 @@ android {

dependencies {

api project(':exorecord')
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.brookmg.exorecord">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ExoRecord">
android:theme="@style/Theme.ExoRecord"
android:fullBackupContent="true">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/dev/brookmg/exorecord/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.brookmg.exorecord

import android.app.Application
import dev.brookmg.exorecord.lib.ExoRecord

class App : Application() {

companion object {
lateinit var instance: App
val exoRecordInstance: ExoRecord by lazy { ExoRecord(instance) }
}

override fun onCreate() {
super.onCreate()
instance = this
}
}
99 changes: 98 additions & 1 deletion app/src/main/java/dev/brookmg/exorecord/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,108 @@
package dev.brookmg.exorecord

import androidx.appcompat.app.AppCompatActivity
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.DefaultRenderersFactory
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.audio.*
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.source.hls.HlsExtractorFactory
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

companion object {
const val FORMAT_MP3 = "mp3"
const val FORMAT_MP4 = "mp4"
const val FORMAT_M3U = "m3u"
const val FORMAT_M3U8 = "m3u8"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val streamUrl = "https://stream.live.vc.bbcmedia.co.uk/bbc_radio_one?s=1619514803&e=1619529203&h=f378f4ca18759ebfa5fd1d674c794cfc"

val mainMediaSource: MediaSource
val uri = Uri.parse(streamUrl)
val lastPath = uri.lastPathSegment

val bandwidthMeter = DefaultBandwidthMeter.Builder(applicationContext).build()
val trackSelectionFactory = AdaptiveTrackSelection.Factory()
val httpDataSourceFactory = DefaultHttpDataSourceFactory(
"-- Audio Test --",
bandwidthMeter,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true
)

val dataSourceFactory = DefaultDataSourceFactory(this, bandwidthMeter, httpDataSourceFactory)

if (lastPath == null || lastPath.isEmpty()) return
mainMediaSource = if (lastPath.contains(FORMAT_M3U8) ||
lastPath.contains(FORMAT_M3U)) {
HlsMediaSource.Factory(dataSourceFactory)
.setAllowChunklessPreparation(true)
.setExtractorFactory(HlsExtractorFactory.DEFAULT)
.createMediaSource(MediaItem.fromUri(uri))
} else {
ProgressiveMediaSource.Factory(dataSourceFactory, DefaultExtractorsFactory()).createMediaSource(
MediaItem.fromUri(uri)
)
}

val renderersFactory = object : DefaultRenderersFactory(this) {
override fun buildAudioSink(
context: Context, enableFloatOutput: Boolean,
enableAudioTrackPlaybackParams: Boolean, enableOffload: Boolean
): AudioSink {
return DefaultAudioSink(
AudioCapabilities.DEFAULT_AUDIO_CAPABILITIES,
DefaultAudioSink.DefaultAudioProcessorChain(App.exoRecordInstance.exoRecordProcessor),
enableFloatOutput, enableAudioTrackPlaybackParams, enableOffload
)
}
}

val trackSelector = DefaultTrackSelector(applicationContext, trackSelectionFactory)
val exoPlayer = SimpleExoPlayer.Builder(applicationContext, renderersFactory)
.setTrackSelector(trackSelector)
.setBandwidthMeter(bandwidthMeter)
.build()

exoPlayer.setMediaSource(mainMediaSource)
exoPlayer.prepare()

findViewById<Button>(R.id.button).setOnClickListener { exoPlayer.playWhenReady = true }
findViewById<Button>(R.id.button2).setOnClickListener { exoPlayer.stop() }

findViewById<Button>(R.id.button_rec).setOnClickListener {
CoroutineScope(Dispatchers.Main).launch {
App.exoRecordInstance.startRecording()
}
}

findViewById<Button>(R.id.button_rec_stop).setOnClickListener {
CoroutineScope(Dispatchers.Main).launch {
Log.e("CONVERSION", App.exoRecordInstance.stopRecording(true).toString())
}
}
}
}
57 changes: 55 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="@string/exorecord_nothing"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
android:text="@string/start"
tools:layout_editor_absoluteX="203dp"
tools:layout_editor_absoluteY="412dp" />

<Button
android:id="@+id/button2"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tview"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/stop"
tools:layout_editor_absoluteX="169dp"
tools:layout_editor_absoluteY="509dp" />

<Button
android:id="@+id/button_rec"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#a00000"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
android:text="@string/start_recording"
tools:layout_editor_absoluteX="203dp"
tools:layout_editor_absoluteY="412dp" />

<Button
android:id="@+id/button_rec_stop"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#a00000"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/stop_recording"
tools:layout_editor_absoluteX="169dp"
tools:layout_editor_absoluteY="509dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">ExoRecord</string>
<string name="exorecord_nothing">ExoRecord --- --- --- NOTHING!</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="start_recording">Start Recording</string>
<string name="stop_recording">Stop Recording</string>
</resources>
4 changes: 4 additions & 0 deletions exorecord/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.brookmg.exoplayer
package dev.brookmg.exorecord.lib

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -19,6 +19,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.brookmg.exoplayer.test", appContext.packageName)
assertEquals("dev.brookmg.exorecord.lib.test", appContext.packageName)
}
}
2 changes: 1 addition & 1 deletion exorecord/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.brookmg.exoplayer">
package="dev.brookmg.exorecord.lib">

</manifest>
16 changes: 16 additions & 0 deletions exorecord/src/main/java/dev/brookmg/exorecord/lib/ExoRecord.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.brookmg.exorecord.lib

import android.app.Application

/**
* It's wise to pass the application context here
*/
class ExoRecord(private val application: Application) : IExoRecord{

val exoRecordProcessor: ExoRecordProcessor by lazy { ExoRecordProcessor(applicationContext = application) }

override suspend fun startRecording() = exoRecordProcessor.startRecording()

override suspend fun stopRecording(saveAsAAC: Boolean): IExoRecord.Record = exoRecordProcessor.stopRecording(saveAsAAC)

}
Loading

0 comments on commit b21201b

Please sign in to comment.