-
Notifications
You must be signed in to change notification settings - Fork 0
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 #36 from milospi/feature/deezer-integration-final
Added deezer integration
- Loading branch information
Showing
15 changed files
with
296 additions
and
44 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
67 changes: 67 additions & 0 deletions
67
android/src/main/java/group07/beatbattle/android/AndroidAudioPlayer.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,67 @@ | ||
| package group07.beatbattle.android; | ||
|
|
||
| import android.content.Context; | ||
| import android.media.AudioAttributes; | ||
| import android.media.MediaPlayer; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
|
|
||
| import group07.beatbattle.audio.AudioPlayer; | ||
|
|
||
| public class AndroidAudioPlayer implements AudioPlayer { | ||
|
|
||
| private final Context context; | ||
| private MediaPlayer mediaPlayer; | ||
| private boolean muted = false; | ||
|
|
||
| public AndroidAudioPlayer(Context context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void play(String url) { | ||
| stop(); | ||
| if (url == null || url.isEmpty()) return; | ||
| try { | ||
| mediaPlayer = new MediaPlayer(); | ||
| mediaPlayer.setAudioAttributes(new AudioAttributes.Builder() | ||
| .setUsage(AudioAttributes.USAGE_GAME) | ||
| .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | ||
| .build()); | ||
| mediaPlayer.setDataSource(url); | ||
| float vol = muted ? 0f : 1f; | ||
| mediaPlayer.setVolume(vol, vol); | ||
| mediaPlayer.setOnPreparedListener(MediaPlayer::start); | ||
| mediaPlayer.setOnErrorListener((mp, what, extra) -> { | ||
| Gdx.app.error("AndroidAudioPlayer", "MediaPlayer error what=" + what + " extra=" + extra); | ||
| return false; | ||
| }); | ||
| mediaPlayer.prepareAsync(); | ||
| } catch (Exception e) { | ||
| Gdx.app.error("AndroidAudioPlayer", "Failed to play: " + url, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| if (mediaPlayer != null) { | ||
| try { mediaPlayer.stop(); } catch (IllegalStateException ignored) {} | ||
| mediaPlayer.release(); | ||
| mediaPlayer = null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setMuted(boolean muted) { | ||
| this.muted = muted; | ||
| if (mediaPlayer != null) { | ||
| float vol = muted ? 0f : 1f; | ||
| mediaPlayer.setVolume(vol, vol); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dispose() { | ||
| stop(); | ||
| } | ||
| } |
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
67 changes: 67 additions & 0 deletions
67
android/src/main/java/group07/beatbattle/android/DeezerMusicService.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,67 @@ | ||
| package group07.beatbattle.android; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
|
|
||
| import group07.beatbattle.model.Song; | ||
| import group07.beatbattle.service.MusicService; | ||
| import group07.beatbattle.service.MusicServiceCallback; | ||
|
|
||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.InputStreamReader; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class DeezerMusicService implements MusicService { | ||
|
|
||
| private static final String CHART_URL = "https://api.deezer.com/chart/0/tracks?limit=50"; | ||
|
|
||
| @Override | ||
| public void fetchTracks(int count, MusicServiceCallback callback) { | ||
| new Thread(() -> { | ||
| try { | ||
| HttpURLConnection conn = (HttpURLConnection) new URL(CHART_URL).openConnection(); | ||
| conn.setRequestMethod("GET"); | ||
| conn.setConnectTimeout(10_000); | ||
| conn.setReadTimeout(10_000); | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(conn.getInputStream()))) { | ||
| String line; | ||
| while ((line = reader.readLine()) != null) sb.append(line); | ||
| } | ||
| conn.disconnect(); | ||
|
|
||
| JSONObject json = new JSONObject(sb.toString()); | ||
| JSONArray data = json.getJSONArray("data"); | ||
|
|
||
| List<Song> songs = new ArrayList<>(); | ||
| for (int i = 0; i < data.length(); i++) { | ||
| JSONObject track = data.getJSONObject(i); | ||
| String id = String.valueOf(track.getLong("id")); | ||
| String title = track.getString("title"); | ||
| String artist = track.getJSONObject("artist").getString("name"); | ||
| String preview = track.optString("preview", ""); | ||
| String cover = track.getJSONObject("album").optString("cover_medium", ""); | ||
| if (!preview.isEmpty()) { | ||
| songs.add(new Song(id, title, artist, preview, cover)); | ||
| } | ||
| } | ||
|
|
||
| Collections.shuffle(songs); | ||
| List<Song> result = new ArrayList<>(songs.subList(0, Math.min(count, songs.size()))); | ||
|
|
||
| Gdx.app.postRunnable(() -> callback.onSuccess(result)); | ||
|
|
||
| } catch (Exception e) { | ||
| Gdx.app.postRunnable(() -> callback.onFailure(e.getMessage())); | ||
| } | ||
| }).start(); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package group07.beatbattle.audio; | ||
|
|
||
| public interface AudioPlayer { | ||
| void play(String url); | ||
| void stop(); | ||
| void setMuted(boolean muted); | ||
| void dispose(); | ||
| } |
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
Oops, something went wrong.