Search in sources :

Example 11 with Song

use of io.github.ryanhoo.music.data.model.Song in project StylishMusicPlayer by ryanhoo.

the class PlayListDetailsActivity method onAction.

// Adapter Action Callback
@Override
public void onAction(View actionView, final int position) {
    final Song song = mAdapter.getItem(position);
    PopupMenu actionMenu = new PopupMenu(this, actionView, Gravity.END | Gravity.BOTTOM);
    actionMenu.inflate(R.menu.music_action);
    actionMenu.getMenu().findItem(R.id.menu_item_delete).setVisible(!isFolder);
    actionMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()) {
                case R.id.menu_item_add_to_play_list:
                    new AddToPlayListDialogFragment().setCallback(new AddToPlayListDialogFragment.Callback() {

                        @Override
                        public void onPlayListSelected(PlayList playList) {
                            if (playList.getId() == mPlayList.getId())
                                return;
                            mPresenter.addSongToPlayList(song, playList);
                        }
                    }).show(getSupportFragmentManager().beginTransaction(), "AddToPlayList");
                    break;
                case R.id.menu_item_delete:
                    mDeleteIndex = position;
                    mPresenter.delete(song, mPlayList);
                    break;
            }
            return true;
        }
    });
    actionMenu.show();
}
Also used : AddToPlayListDialogFragment(io.github.ryanhoo.music.ui.playlist.AddToPlayListDialogFragment) PlayList(io.github.ryanhoo.music.data.model.PlayList) Song(io.github.ryanhoo.music.data.model.Song) MenuItem(android.view.MenuItem) PopupMenu(android.support.v7.widget.PopupMenu)

Example 12 with Song

use of io.github.ryanhoo.music.data.model.Song in project StylishMusicPlayer by ryanhoo.

the class LocalMusicPresenter method cursorToMusic.

private Song cursorToMusic(Cursor cursor) {
    String realPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
    File songFile = new File(realPath);
    Song song;
    if (songFile.exists()) {
        // Using song parsed from file to avoid encoding problems
        song = FileUtils.fileToMusic(songFile);
        if (song != null) {
            return song;
        }
    }
    song = new Song();
    song.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
    String displayName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
    if (displayName.endsWith(".mp3")) {
        displayName = displayName.substring(0, displayName.length() - 4);
    }
    song.setDisplayName(displayName);
    song.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
    song.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
    song.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
    song.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
    song.setSize(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
    return song;
}
Also used : Song(io.github.ryanhoo.music.data.model.Song) File(java.io.File)

Example 13 with Song

use of io.github.ryanhoo.music.data.model.Song in project StylishMusicPlayer by ryanhoo.

the class LocalMusicPresenter method onLoadFinished.

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Subscription subscription = Observable.just(cursor).flatMap(new Func1<Cursor, Observable<List<Song>>>() {

        @Override
        public Observable<List<Song>> call(Cursor cursor) {
            List<Song> songs = new ArrayList<>();
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                do {
                    Song song = cursorToMusic(cursor);
                    songs.add(song);
                } while (cursor.moveToNext());
            }
            return mRepository.insert(songs);
        }
    }).doOnNext(new Action1<List<Song>>() {

        @Override
        public void call(List<Song> songs) {
            Log.d(TAG, "onLoadFinished: " + songs.size());
            Collections.sort(songs, new Comparator<Song>() {

                @Override
                public int compare(Song left, Song right) {
                    return left.getDisplayName().compareTo(right.getDisplayName());
                }
            });
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Song>>() {

        @Override
        public void onStart() {
            mView.showProgress();
        }

        @Override
        public void onCompleted() {
            mView.hideProgress();
        }

        @Override
        public void onError(Throwable throwable) {
            mView.hideProgress();
            Log.e(TAG, "onError: ", throwable);
        }

        @Override
        public void onNext(List<Song> songs) {
            mView.onLocalMusicLoaded(songs);
            mView.emptyView(songs.isEmpty());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : Action1(rx.functions.Action1) Cursor(android.database.Cursor) Observable(rx.Observable) Song(io.github.ryanhoo.music.data.model.Song) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Example 14 with Song

use of io.github.ryanhoo.music.data.model.Song in project StylishMusicPlayer by ryanhoo.

the class FileUtils method fileToMusic.

public static Song fileToMusic(File file) {
    if (file.length() == 0)
        return null;
    MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    metadataRetriever.setDataSource(file.getAbsolutePath());
    final int duration;
    String keyDuration = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    // ensure the duration is a digit, otherwise return null song
    if (keyDuration == null || !keyDuration.matches("\\d+"))
        return null;
    duration = Integer.parseInt(keyDuration);
    final String title = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_TITLE, file.getName());
    final String displayName = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_TITLE, file.getName());
    final String artist = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_ARTIST, UNKNOWN);
    final String album = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_ALBUM, UNKNOWN);
    final Song song = new Song();
    song.setTitle(title);
    song.setDisplayName(displayName);
    song.setArtist(artist);
    song.setPath(file.getAbsolutePath());
    song.setAlbum(album);
    song.setDuration(duration);
    song.setSize((int) file.length());
    return song;
}
Also used : Song(io.github.ryanhoo.music.data.model.Song) MediaMetadataRetriever(android.media.MediaMetadataRetriever)

Example 15 with Song

use of io.github.ryanhoo.music.data.model.Song in project StylishMusicPlayer by ryanhoo.

the class MusicPlayerFragment method getCurrentSongDuration.

private int getCurrentSongDuration() {
    Song currentSong = mPlayer.getPlayingSong();
    int duration = 0;
    if (currentSong != null) {
        duration = currentSong.getDuration();
    }
    return duration;
}
Also used : Song(io.github.ryanhoo.music.data.model.Song)

Aggregations

Song (io.github.ryanhoo.music.data.model.Song)18 Subscription (rx.Subscription)4 CompositeSubscription (rx.subscriptions.CompositeSubscription)4 PlayList (io.github.ryanhoo.music.data.model.PlayList)3 Folder (io.github.ryanhoo.music.data.model.Folder)2 File (java.io.File)2 List (java.util.List)2 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 MediaMetadataRetriever (android.media.MediaMetadataRetriever)1 PopupMenu (android.support.v7.widget.PopupMenu)1 MenuItem (android.view.MenuItem)1 OnClick (butterknife.OnClick)1 FavoriteChangeEvent (io.github.ryanhoo.music.event.FavoriteChangeEvent)1 PlayListUpdatedEvent (io.github.ryanhoo.music.event.PlayListUpdatedEvent)1 PlaySongEvent (io.github.ryanhoo.music.event.PlaySongEvent)1 OnItemClickListener (io.github.ryanhoo.music.ui.base.adapter.OnItemClickListener)1 DefaultDividerDecoration (io.github.ryanhoo.music.ui.common.DefaultDividerDecoration)1 AddToPlayListDialogFragment (io.github.ryanhoo.music.ui.playlist.AddToPlayListDialogFragment)1 IOException (java.io.IOException)1