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();
}
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;
}
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);
}
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;
}
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;
}
Aggregations