Able to do a demo stream, not via jni but with android's media player. Need to integrate sqlite to save token, username, password, and uri

This commit is contained in:
kdeng00
2019-10-05 22:14:11 -04:00
parent 33ac4c6fcf
commit fe15ea221f
8 changed files with 576 additions and 350 deletions
+14 -1
View File
@@ -59,17 +59,29 @@ set (CURL_LIBRARY
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/${ANDROID_ABI}
)
set (BASS_LIBRARY
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/${ANDROID_ABI}
)
set (CURL_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/include/curl
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/include
)
set (BASS_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/include
)
add_library(boo STATIC IMPORTED)
#add_library(bass SHARED IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION
"${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/${ANDROID_ABI}/libcurl.a"
)
#set_target_properties(bass PROPERTIES IMPORTED_LOCATION
# "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/${ANDROID_ABI}/libbass.so"
# )
include_directories(${CURL_INCLUDE_DIR})
include_directories(${CURL_INCLUDE_DIR}) #${BASS_INCLUDE_DIR})
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
@@ -79,6 +91,7 @@ target_link_libraries(native-lib PRIVATE
nlohmann_json::nlohmann_json
boo
#bass
${log-lib}
z
)
+41 -9
View File
@@ -3,6 +3,7 @@
//
#include <iostream>
#include <filesystem>
#include <cstring>
#include <jni.h>
#include <string>
@@ -15,7 +16,7 @@
#include <android/sharedmem.h>
#include <sys/mman.h>
#include "nlohmann/json.hpp"
#include <nlohmann/json.hpp>
#include <curl/curl.h>
#include "model/Song.h"
@@ -24,16 +25,16 @@
#include "Tok.h"
model::Song retrieveSong(const std::string&);
model::Song retrieveSong(const std::string&, const std::string&);
std::string fetchToken(const std::string&, const std::string&, const std::string&);
model::Song retrieveSong(const std::string& token)
model::Song retrieveSong(const std::string& token, const std::string& baseUri)
{
repository::SongRepository songRepo;
model::Song song;
song.id = 2;
song.id = 1;
song.title = "Smooth";
song.album = "Amaze";
song.artist = "The Herb";
@@ -41,7 +42,7 @@ model::Song retrieveSong(const std::string& token)
song.duration = 420;
song.year = 420;
song = songRepo.retrieveSong(token, song.id);
song = songRepo.retrieveSong(token, baseUri, song);
return song;
}
@@ -60,6 +61,22 @@ std::string fetchToken(const std::string& username, const std::string& password,
}
void iterateDirectory(const std::string& path)
{
/**
auto somePath = std::filesystem::path(path);
for (auto dir: std::filesystem::directory_iterator(somePath)) {
auto foundPath = dir;
if (foundPath.is_directory()) {
auto dirPath = foundPath.path();
} else {
auto filePath = foundPath.path();
}
}
*/
}
extern "C"
JNIEXPORT jstring
JNICALL
@@ -86,13 +103,13 @@ JNICALL
Java_com_example_mear_activities_DemoStreamActivity_retrieveSong(
JNIEnv *env,
jobject thisObj,
jstring token
jstring token,
jstring apiUri
)
{
const std::string tok = env->GetStringUTFChars(token, NULL);
auto song = retrieveSong(tok);
std::string bank = "SBI Bank";
const std::string baseUri = env->GetStringUTFChars(apiUri, NULL);
auto song = retrieveSong(tok, baseUri);
jclass songClass = env->FindClass( "com/example/mear/models/Song");
jmethodID jconstructor = env->GetMethodID( songClass, "<init>", "()V" );
@@ -124,3 +141,18 @@ Java_com_example_mear_activities_DemoStreamActivity_retrieveSong(
return songObj;
}
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_pathIteratorDemo(
JNIEnv *env,
jobject thisObj,
jstring path
) {
const std::string pathStr = env->GetStringUTFChars(path, NULL);
iterateDirectory(pathStr);
}
+95 -1
View File
@@ -19,7 +19,7 @@ namespace repository {
model::Song SongRepository::retrieveSong(const std::string& token, const int id) {
std::string uri;
std::string uri("");
uri.append(std::to_string(id));
model::Song song;
@@ -63,6 +63,100 @@ namespace repository {
return song;
}
model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri,
const int id) {
std::string uri(baseUri);
uri.append("/api/v1/song/");
uri.append(std::to_string(id));
model::Song song;
CURL *curl;
CURLcode res;
struct curl_slist *chunk = NULL;
curl = curl_easy_init();
if (!curl) {
return song;
}
auto resp = std::make_unique<char[]>(10000);
std::string authInfo("Authorization: Bearer ");
authInfo.append(token);
chunk = curl_slist_append(chunk, authInfo.c_str());
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto s = nlohmann::json::parse(resp.get());
song.id = s["id"].get<int>();
song.title = s["title"].get<std::string>();
song.album = s["album"].get<std::string>();
song.artist = s["artist"].get<std::string>();
song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>();
return song;
}
return song;
}
model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri,
const model::Song& sng) {
std::string uri(baseUri);
uri.append("/api/v1/song/");
uri.append(std::to_string(sng.id));
model::Song song;
CURL *curl;
CURLcode res;
struct curl_slist *chunk = NULL;
curl = curl_easy_init();
if (!curl) {
return sng;
}
auto resp = std::make_unique<char[]>(10000);
std::string authInfo("Authorization: Bearer ");
authInfo.append(token);
chunk = curl_slist_append(chunk, authInfo.c_str());
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto s = nlohmann::json::parse(resp.get());
song.id = s["id"].get<int>();
song.title = s["title"].get<std::string>();
song.album = s["album"].get<std::string>();
song.artist = s["artist"].get<std::string>();
song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>();
return song;
}
return sng;
}
size_t SongRepository::respBodyRetriever(void* ptr, size_t size, size_t nmemb, char *e)
{
+2
View File
@@ -17,6 +17,8 @@ namespace repository {
std::vector<model::Song> fetchSongs(const std::string&, const std::string&);
model::Song retrieveSong(const std::string&, const int);
model::Song retrieveSong(const std::string&, const std::string&, const int);
model::Song retrieveSong(const std::string&, const std::string&, const model::Song&);
private:
static size_t respBodyRetriever(void*, size_t, size_t, char*);
};
@@ -1,6 +1,7 @@
package com.example.mear.activities
import android.os.Bundle
import android.os.Environment
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import com.example.mear.R
@@ -11,7 +12,7 @@ import org.jetbrains.anko.toast
import com.example.mear.models.Song
class DemoStreamActivity : AppCompatActivity() {
class DemoStreamActivity : BaseServiceActivity() {
companion object {
init {
@@ -24,7 +25,9 @@ class DemoStreamActivity : AppCompatActivity() {
private external fun logUser(usr: String, pass: String, api: String): String
private external fun retrieveSong(tok: String): Song
private external fun retrieveSong(tok: String, api: String): Song
//private external fun testSongStream(tok: String, id: Int)
private external fun pathIteratorDemo(path: String)
override fun onCreate(savedInstanceState: Bundle?) {
@@ -32,6 +35,8 @@ class DemoStreamActivity : AppCompatActivity() {
setContentView(R.layout.activity_demo_stream)
setSupportActionBar(toolbar)
doBindService()
demoStream.setOnClickListener {
toast("vacant").show()
}
@@ -57,8 +62,20 @@ class DemoStreamActivity : AppCompatActivity() {
var apiUriStr = apiUri.text.toString()
token = logUser(usernameStr, passwordStr, apiUriStr)
toast(token!!).show()
var s = retrieveSong(token!!);
try {
var s = retrieveSong(token!!, apiUriStr)
musicService!!.icarusPlaySong(this.applicationContext, token!!, apiUriStr, s)
val dir = Environment.getExternalStorageDirectory().absolutePath + "/music"
pathIteratorDemo(dir)
var currentTrack = musicService!!.getCurrentTrack()
val title = currentTrack.title
val artist = currentTrack.artist
}
catch (ex: Exception) {
val msg = ex.message
}
}
private fun validFields(): Boolean {
@@ -1,8 +1,10 @@
package com.example.mear.playback.service
import android.app.Service
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.net.Uri
import android.os.Binder
import android.os.Environment
import android.os.IBinder
@@ -16,6 +18,7 @@ import com.example.mear.constants.Interval
import com.example.mear.management.MusicFiles
import com.example.mear.management.TrackManager
import com.example.mear.models.PlayControls
import com.example.mear.models.Song
import com.example.mear.models.Track
import com.example.mear.repositories.RepeatRepository
import com.example.mear.repositories.ShuffleRepository
@@ -90,6 +93,46 @@ class MusicService: Service() {
}
}
fun icarusPlaySong(token: String, song: Song) {
var uriBase = "https://www.soaricarus.com/api/v1/song/stream/"
val id = song.id
uriBase += "$id"
var uri: Uri = Uri.parse(uriBase)
var hddr: MutableMap<String, String> = mutableMapOf()
hddr["Authorization"] = "Bearer $token"
try {
trackPlayer!!.reset()
trackPlayer!!.setDataSource(this, uri, hddr)
trackPlayer!!.prepare()
trackPlayer!!.start()
}
catch (ex: Exception) {
val msg = ex.message
}
}
fun icarusPlaySong(ctx: Context, token: String, uriBase: String, song: Song) {
var uriStr = uriBase
val id = song.id
uriStr += "/api/v1/song/stream/"
uriStr += "$id"
var uri: Uri = Uri.parse(uriStr)
var hddr: MutableMap<String, String> = mutableMapOf()
hddr["Authorization"] = "Bearer $token"
try {
trackPlayer!!.reset()
trackPlayer!!.setDataSource(ctx, uri, hddr)
trackPlayer!!.prepare()
trackPlayer!!.start()
}
catch (ex: Exception) {
val msg = ex.message
}
}
fun goToPosition(progress: Int) {
trackPlayer!!.seekTo(progress)
}