-
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 #29 from batshalregmi/playback
Playback
- Loading branch information
Showing
9 changed files
with
128 additions
and
4 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
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/group3/spotifywrapped/utils/Playback.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,57 @@ | ||
package com.group3.spotifywrapped.utils; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.media3.common.MediaItem; | ||
import androidx.media3.exoplayer.ExoPlayer; | ||
|
||
import com.group3.spotifywrapped.CoreAppViews.LoginActivity; | ||
|
||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
|
||
public class Playback { | ||
UserTrackHistory uth; | ||
ExoPlayer player; | ||
|
||
public Playback(Context context) { | ||
player = new ExoPlayer.Builder(context).build(); | ||
uth = new UserTrackHistory(); | ||
} | ||
|
||
public void playSong(String url) { | ||
player.stop(); | ||
player.clearMediaItems(); | ||
player.addMediaItem(MediaItem.fromUri(url)); | ||
player.prepare(); | ||
player.play(); | ||
} | ||
|
||
private void initQueue() { | ||
uth.retrieveHistory(); | ||
List<String> trackIDs = uth.getTrackIDs(); | ||
|
||
// Makes a ton of API calls might need to limit this | ||
for (String trackID : trackIDs) { | ||
if (trackID != null) { | ||
player.addMediaItem(MediaItem.fromUri(getTrackURL(trackID))); | ||
} | ||
} | ||
} | ||
|
||
public String getTrackURL(String trackID) { | ||
try { | ||
JSONObject response = SpotifyApiHelper.callSpotifyApi("/tracks/" + trackID, LoginActivity.token, "GET"); | ||
return response.getString("preview_url"); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
public void startMedia() { | ||
initQueue(); | ||
player.prepare(); | ||
player.play(); | ||
} | ||
} |
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