Search in sources :

Example 21 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MainActivity method handlePlaybackIntent.

private void handlePlaybackIntent(@Nullable Intent intent) {
    if (intent == null) {
        return;
    }
    Uri uri = intent.getData();
    String mimeType = intent.getType();
    boolean handled = false;
    if (intent.getAction() != null && intent.getAction().equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) {
        final List<Song> songs = SearchQueryHelper.getSongs(this, intent.getExtras());
        if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) {
            MusicPlayerRemote.openAndShuffleQueue(songs, true);
        } else {
            MusicPlayerRemote.openQueue(songs, 0, true);
        }
        handled = true;
    }
    if (uri != null && uri.toString().length() > 0) {
        MusicPlayerRemote.playFromUri(uri);
        handled = true;
    } else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
        final long id = parseIdFromIntent(intent, "playlistId", "playlist");
        if (id >= 0) {
            int position = intent.getIntExtra("position", 0);
            List<Song> songs = new ArrayList<>(PlaylistSongLoader.getPlaylistSongList(this, id));
            MusicPlayerRemote.openQueue(songs, position, true);
            handled = true;
        }
    } else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
        final long id = parseIdFromIntent(intent, "albumId", "album");
        if (id >= 0) {
            int position = intent.getIntExtra("position", 0);
            MusicPlayerRemote.openQueue(AlbumLoader.getAlbum(this, id).songs, position, true);
            handled = true;
        }
    } else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
        final long id = parseIdFromIntent(intent, "artistId", "artist");
        if (id >= 0) {
            int position = intent.getIntExtra("position", 0);
            MusicPlayerRemote.openQueue(ArtistLoader.getArtist(this, id).getSongs(), position, true);
            handled = true;
        }
    }
    if (handled) {
        setIntent(new Intent());
    }
}
Also used : Song(com.kabouzeid.gramophone.model.Song) ArrayList(java.util.ArrayList) List(java.util.List) Intent(android.content.Intent) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint)

Example 22 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method setShuffleMode.

public void setShuffleMode(final int shuffleMode) {
    PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(SAVED_SHUFFLE_MODE, shuffleMode).apply();
    switch(shuffleMode) {
        case SHUFFLE_MODE_SHUFFLE:
            this.shuffleMode = shuffleMode;
            ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition());
            position = 0;
            break;
        case SHUFFLE_MODE_NONE:
            this.shuffleMode = shuffleMode;
            long currentSongId = getCurrentSong().id;
            playingQueue = new ArrayList<>(originalPlayingQueue);
            int newPosition = 0;
            for (Song song : getPlayingQueue()) {
                if (song.id == currentSongId) {
                    newPosition = getPlayingQueue().indexOf(song);
                }
            }
            position = newPosition;
            break;
    }
    handleAndSendChangeInternal(SHUFFLE_MODE_CHANGED);
    notifyChange(QUEUE_CHANGED);
}
Also used : Song(com.kabouzeid.gramophone.model.Song) Point(android.graphics.Point)

Example 23 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method onStartCommand.

@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    if (intent != null) {
        if (intent.getAction() != null) {
            restoreQueuesAndPositionIfNecessary();
            String action = intent.getAction();
            switch(action) {
                case ACTION_TOGGLE_PAUSE:
                    if (isPlaying()) {
                        pause();
                    } else {
                        play();
                    }
                    break;
                case ACTION_PAUSE:
                    pause();
                    break;
                case ACTION_PLAY:
                    play();
                    break;
                case ACTION_PLAY_PLAYLIST:
                    Playlist playlist = intent.getParcelableExtra(INTENT_EXTRA_PLAYLIST);
                    int shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode());
                    if (playlist != null) {
                        List<Song> playlistSongs;
                        if (playlist instanceof AbsCustomPlaylist) {
                            playlistSongs = ((AbsCustomPlaylist) playlist).getSongs(getApplicationContext());
                        } else {
                            // noinspection unchecked
                            playlistSongs = (List) PlaylistSongLoader.getPlaylistSongList(getApplicationContext(), playlist.id);
                        }
                        if (!playlistSongs.isEmpty()) {
                            if (shuffleMode == SHUFFLE_MODE_SHUFFLE) {
                                int startPosition = 0;
                                if (!playlistSongs.isEmpty()) {
                                    startPosition = new Random().nextInt(playlistSongs.size());
                                }
                                openQueue(playlistSongs, startPosition, true);
                                setShuffleMode(shuffleMode);
                            } else {
                                openQueue(playlistSongs, 0, true);
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.playlist_is_empty, Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), R.string.playlist_is_empty, Toast.LENGTH_LONG).show();
                    }
                    break;
                case ACTION_REWIND:
                    back(true);
                    break;
                case ACTION_SKIP:
                    playNextSong(true);
                    break;
                case ACTION_STOP:
                case ACTION_QUIT:
                    pendingQuit = false;
                    quit();
                    break;
                case ACTION_PENDING_QUIT:
                    pendingQuit = true;
                    break;
            }
        }
    }
    return START_NOT_STICKY;
}
Also used : AbsCustomPlaylist(com.kabouzeid.gramophone.model.AbsCustomPlaylist) Playlist(com.kabouzeid.gramophone.model.Playlist) Song(com.kabouzeid.gramophone.model.Song) AbsCustomPlaylist(com.kabouzeid.gramophone.model.AbsCustomPlaylist) Random(java.util.Random) Point(android.graphics.Point)

Example 24 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method sendPublicIntent.

// to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch
private void sendPublicIntent(@NonNull final String what) {
    final Intent intent = new Intent(what.replace(PHONOGRAPH_PACKAGE_NAME, MUSIC_PACKAGE_NAME));
    final Song song = getCurrentSong();
    intent.putExtra("id", song.id);
    intent.putExtra("artist", song.artistName);
    intent.putExtra("album", song.albumName);
    intent.putExtra("track", song.title);
    intent.putExtra("duration", song.duration);
    intent.putExtra("position", (long) getSongProgressMillis());
    intent.putExtra("playing", isPlaying());
    intent.putExtra("scrobbling_source", PHONOGRAPH_PACKAGE_NAME);
    sendStickyBroadcast(intent);
}
Also used : Song(com.kabouzeid.gramophone.model.Song) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 25 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class CardPlayerFragment method updateLyrics.

private void updateLyrics() {
    if (updateLyricsAsyncTask != null)
        updateLyricsAsyncTask.cancel(false);
    final Song song = MusicPlayerRemote.getCurrentSong();
    updateLyricsAsyncTask = new AsyncTask<Void, Void, Lyrics>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            lyrics = null;
            playerAlbumCoverFragment.setLyrics(null);
            toolbar.getMenu().removeItem(R.id.action_show_lyrics);
        }

        @Override
        protected Lyrics doInBackground(Void... params) {
            String data = MusicUtil.getLyrics(song);
            if (TextUtils.isEmpty(data)) {
                return null;
            }
            return Lyrics.parse(song, data);
        }

        @Override
        protected void onPostExecute(Lyrics l) {
            lyrics = l;
            playerAlbumCoverFragment.setLyrics(lyrics);
            if (lyrics == null) {
                if (toolbar != null) {
                    toolbar.getMenu().removeItem(R.id.action_show_lyrics);
                }
            } else {
                Activity activity = getActivity();
                if (toolbar != null && activity != null)
                    if (toolbar.getMenu().findItem(R.id.action_show_lyrics) == null) {
                        int color = ToolbarContentTintHelper.toolbarContentColor(activity, Color.TRANSPARENT);
                        Drawable drawable = ImageUtil.getTintedVectorDrawable(activity, R.drawable.ic_comment_text_outline_white_24dp, color);
                        toolbar.getMenu().add(Menu.NONE, R.id.action_show_lyrics, Menu.NONE, R.string.action_show_lyrics).setIcon(drawable).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                    }
            }
        }

        @Override
        protected void onCancelled(Lyrics s) {
            onPostExecute(null);
        }
    }.execute();
}
Also used : Song(com.kabouzeid.gramophone.model.Song) AsyncTask(android.os.AsyncTask) Drawable(android.graphics.drawable.Drawable) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) AbsSlidingMusicPanelActivity(com.kabouzeid.gramophone.ui.activities.base.AbsSlidingMusicPanelActivity) Activity(android.app.Activity) Lyrics(com.kabouzeid.gramophone.model.lyrics.Lyrics)

Aggregations

Song (com.kabouzeid.gramophone.model.Song)37 Drawable (android.graphics.drawable.Drawable)11 Intent (android.content.Intent)8 Bitmap (android.graphics.Bitmap)7 Point (android.graphics.Point)6 NonNull (androidx.annotation.NonNull)6 Activity (android.app.Activity)5 RemoteViews (android.widget.RemoteViews)5 BitmapPaletteWrapper (com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper)5 AbsSlidingMusicPanelActivity (com.kabouzeid.gramophone.ui.activities.base.AbsSlidingMusicPanelActivity)5 Context (android.content.Context)4 AsyncTask (android.os.AsyncTask)4 TextView (android.widget.TextView)4 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)4 Palette (androidx.palette.graphics.Palette)4 PendingIntent (android.app.PendingIntent)3 View (android.view.View)3 SleepTimerDialog (com.kabouzeid.gramophone.dialogs.SleepTimerDialog)3 ArrayList (java.util.ArrayList)3 ContentValues (android.content.ContentValues)2