-
Notifications
You must be signed in to change notification settings - Fork 63
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 #34 from Tim-W/master
MusicDAO first prototype
- Loading branch information
Showing
41 changed files
with
1,273 additions
and
3 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
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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,71 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
apply plugin: 'org.jlleitschuh.gradle.ktlint' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.3" | ||
|
||
viewBinding { | ||
enabled = true | ||
} | ||
|
||
defaultConfig { | ||
minSdkVersion 24 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles 'consumer-rules.pro' | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
// To inline the bytecode built with JVM target 1.8 into | ||
// bytecode that is being built with JVM target 1.6. (e.g. navArgs) | ||
|
||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
allWarningsAsErrors = true | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation project(':common') | ||
implementation project(':ipv8-android') | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation "androidx.preference:preference:1.1.0" | ||
implementation 'androidx.core:core-ktx:1.2.0' | ||
implementation 'androidx.legacy:legacy-support-v4:1.0.0' | ||
implementation 'com.google.android.material:material:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2' | ||
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2' | ||
implementation 'com.turn:ttorrent-core:1.5' | ||
implementation files('libs/jlibtorrent-1.2.5.0.jar') | ||
implementation files('libs/jlibtorrent-android-arm64-1.2.5.0.jar') | ||
implementation files('libs/jlibtorrent-android-arm-1.2.5.0.jar') | ||
implementation files('libs/jlibtorrent-android-x86-1.2.5.0.jar') | ||
implementation files('libs/jlibtorrent-android-x86_64-1.2.5.0.jar') | ||
implementation('com.github.TorrentStream:TorrentStream-Android:2.5.0') { | ||
exclude group: 'com.frostwire' | ||
} | ||
} |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
musicdao/src/androidTest/java/com/example/musicdao/ExampleInstrumentedTest.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,24 @@ | ||
package com.example.musicdao | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.example.musicdao.test", appContext.packageName) | ||
} | ||
} |
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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.musicdao"> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
</manifest> |
163 changes: 163 additions & 0 deletions
163
musicdao/src/main/java/com/example/musicdao/AudioPlayer.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,163 @@ | ||
package com.example.musicdao | ||
|
||
import android.R | ||
import android.content.Context | ||
import android.media.AudioAttributes | ||
import android.media.MediaPlayer | ||
import android.os.Handler | ||
import android.widget.LinearLayout | ||
import android.widget.SeekBar | ||
import android.widget.Toast | ||
import androidx.core.net.toUri | ||
import kotlinx.android.synthetic.main.music_app_main.* | ||
import java.io.File | ||
|
||
lateinit var instance: AudioPlayer | ||
|
||
/** | ||
* Implements an Android MediaPlayer. Is a singleton. | ||
*/ | ||
class AudioPlayer(context: Context, private val musicService: MusicService) : LinearLayout(context), | ||
MediaPlayer.OnPreparedListener, | ||
MediaPlayer.OnErrorListener, SeekBar.OnSeekBarChangeListener { | ||
private val mediaPlayer: MediaPlayer = MediaPlayer() | ||
private var interestedFraction: Float = 0F | ||
private val bufferInfo = musicService.bufferInfo | ||
private val progressBar = musicService.progressBar | ||
private val seekBar = musicService.seekBar | ||
private val playButton = musicService.playButtonAudioPlayer | ||
|
||
init { | ||
progressBar.max = 100 | ||
progressBar.progress = 0 | ||
bufferInfo.text = "No track currently playing" | ||
seekBar.setOnSeekBarChangeListener(this) | ||
|
||
// Handle playing and pausing tracks | ||
this.playButton.setOnClickListener { | ||
if (mediaPlayer.isPlaying) { | ||
this.playButton.setImageResource(R.drawable.ic_media_play) | ||
mediaPlayer.pause() | ||
} else { | ||
this.playButton.setImageResource(R.drawable.ic_media_pause) | ||
mediaPlayer.start() | ||
} | ||
} | ||
|
||
followSeekBarWithTrack() | ||
} | ||
|
||
/** | ||
* This function updates the seek bar location every second with the playing position | ||
*/ | ||
private fun followSeekBarWithTrack() { | ||
val mHandler = Handler() | ||
musicService.runOnUiThread(object : Runnable { | ||
override fun run() { | ||
val mCurrentPosition: Int = mediaPlayer.currentPosition / 1000 | ||
seekBar.progress = mCurrentPosition | ||
mHandler.postDelayed(this, 1000) | ||
} | ||
}) | ||
} | ||
|
||
companion object { | ||
fun getInstance(context: Context, musicService: MusicService): AudioPlayer { | ||
if (!::instance.isInitialized) { | ||
createInstance( | ||
context, | ||
musicService | ||
) | ||
} | ||
return instance | ||
} | ||
|
||
@Synchronized | ||
private fun createInstance(context: Context, musicService: MusicService) { | ||
instance = | ||
AudioPlayer(context, musicService) | ||
} | ||
} | ||
|
||
/** | ||
* Reset internal state to prepare for playing a track | ||
*/ | ||
fun prepareNextTrack() { | ||
if (mediaPlayer.isPlaying) mediaPlayer.stop() | ||
seekBar.progress = 0 | ||
playButton.setImageResource(R.drawable.ic_media_play) | ||
playButton.isClickable = false | ||
playButton.isActivated = false | ||
playButton.isEnabled = false | ||
} | ||
|
||
fun setAudioResource(file: File) { | ||
prepareNextTrack() | ||
mediaPlayer.reset() | ||
mediaPlayer.apply { | ||
setOnPreparedListener(this@AudioPlayer) | ||
setOnErrorListener(this@AudioPlayer) | ||
setAudioAttributes( | ||
AudioAttributes.Builder() | ||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | ||
.build() | ||
) | ||
setDataSource(context, file.toUri()) | ||
prepareAsync() | ||
} | ||
} | ||
|
||
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { | ||
mp?.reset() | ||
var message = "" | ||
when (what) { | ||
MediaPlayer.MEDIA_ERROR_IO -> { | ||
message = "Media error: IO" | ||
} | ||
MediaPlayer.MEDIA_ERROR_MALFORMED -> { | ||
message = "Media error: malformed" | ||
} | ||
MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK -> { | ||
message = "Media error: invalid for progressive playback" | ||
} | ||
MediaPlayer.MEDIA_ERROR_SERVER_DIED -> { | ||
message = "Media error: server died" | ||
} | ||
MediaPlayer.MEDIA_ERROR_TIMED_OUT -> { | ||
message = "Media error: timed out" | ||
} | ||
MediaPlayer.MEDIA_ERROR_UNKNOWN -> { | ||
message = "Media error: unknown" | ||
} | ||
MediaPlayer.MEDIA_ERROR_UNSUPPORTED -> { | ||
message = "Media error: unsupported" | ||
} | ||
} | ||
val toast = Toast.makeText(context, message, Toast.LENGTH_SHORT) | ||
toast.show() | ||
return true | ||
} | ||
|
||
override fun onPrepared(mp: MediaPlayer) { | ||
this.playButton.isClickable = true | ||
this.playButton.isActivated = true | ||
this.playButton.isEnabled = true | ||
// Directly play the track when it is prepared | ||
this.playButton.callOnClick() | ||
} | ||
|
||
/** | ||
* This enables seeking through the track | ||
*/ | ||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { | ||
if (!fromUser) return | ||
interestedFraction = (progress.toFloat() / 100.toFloat()) | ||
val duration = mediaPlayer.duration | ||
val seekMs: Int = (duration * interestedFraction).toInt() | ||
mediaPlayer.seekTo(seekMs) | ||
} | ||
|
||
override fun onStartTrackingTouch(seekBar: SeekBar?) {} | ||
|
||
override fun onStopTrackingTouch(seekBar: SeekBar?) {} | ||
} |
11 changes: 11 additions & 0 deletions
11
musicdao/src/main/java/com/example/musicdao/MainFragment.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,11 @@ | ||
package com.example.musicdao | ||
|
||
import androidx.fragment.app.Fragment | ||
import nl.tudelft.trustchain.common.ui.BaseFragment | ||
|
||
/** | ||
* A simple [Fragment] subclass. | ||
* Use the [MainFragment.newInstance] factory method to | ||
* create an instance of this fragment. | ||
*/ | ||
class MainFragment : BaseFragment(R.layout.music_app_main) |
Oops, something went wrong.