Created
August 22, 2015 23:18
-
-
Save sstzhp/27bf134e32374a294b2e to your computer and use it in GitHub Desktop.
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
package laffhelm.nm; | |
import android.app.Activity; | |
import android.app.FragmentManager; | |
import android.app.FragmentTransaction; | |
import android.content.BroadcastReceiver; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.content.ServiceConnection; | |
import android.media.AudioManager; | |
import android.media.MediaMetadataEditor; | |
import android.media.MediaMetadataRetriever; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.ArrayAdapter; | |
import android.widget.Toast; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import android.app.AlertDialog; | |
import android.app.ActionBar; | |
import android.app.Dialog; | |
import android.app.DialogFragment; | |
import android.widget.ImageButton; | |
import android.view.View; | |
import android.content.DialogInterface; | |
import android.app.TimePickerDialog; | |
import android.app.TimePickerDialog.OnTimeSetListener; | |
import java.util.*; | |
import android.widget.TimePicker; | |
import android.widget.*; | |
interface Communicator | |
{ | |
public void playback_mode(int id, boolean status); | |
public void song_operations(int id); | |
public void open_song(int position); | |
public ArrayList<Song> get_song_list(); | |
public void set_progress(int i); | |
public void goToPlayer(); | |
public String get_artist(); | |
public String get_album(); | |
String get_title(); | |
public byte[] get_album_art(); | |
public int get_song_id(); | |
public int get_duration(); | |
public int get_elapsed(); | |
public boolean is_playing(); | |
} | |
public class MainActivity extends Activity implements Communicator | |
{ | |
public PlayerFragment playerFrag; | |
public SongListFragment songListFragment; | |
public MiniPlayerFragment miniPlayerFragment; | |
public ArrayList<Song> songFiles; | |
public MusicService musicSvc; | |
public Intent playIntent; | |
public boolean musicBound = false; | |
public SongCompletedListener songCompletedListener; | |
public FragmentManager manager; | |
public final int IDD_DIALOG = 1; | |
public final int IDD_DIALOG_TIMER = 2; | |
public AlertDialog.Builder builder; | |
public ImageButton ibPlaylistSelector; | |
public boolean timer; | |
public TimerTask taskSts; | |
public Timer timerSTS; | |
public int thour; | |
public int tmin; | |
public FragmentTransaction transaction; | |
public IntentFilter intentFilter; | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_player); | |
ibPlaylistSelector = (ImageButton) findViewById(R.id.ibPlaylistSelector); | |
timer = false; | |
if (savedInstanceState == null) | |
{ | |
playerFrag = new PlayerFragment(); | |
songListFragment = new SongListFragment(); | |
miniPlayerFragment = new MiniPlayerFragment(); | |
songFiles = (ArrayList<Song>) getIntent().getSerializableExtra("songs"); | |
Collections.sort(songFiles); | |
} | |
if (playIntent == null) | |
{ | |
playIntent = new Intent(this, MusicService.class); | |
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE); | |
startService(playIntent); | |
} | |
ActionBar actbar = getActionBar(); | |
actbar.hide(); | |
} | |
public class Task extends TimerTask { | |
@Override | |
public void run() { | |
finish(); | |
} | |
} | |
public void setTimer(boolean status) | |
{ | |
if(!timer && status) | |
{ | |
taskSts = new Task(); | |
timerSTS = new Timer(); | |
showDialog(IDD_DIALOG_TIMER); | |
}else if(timer && !status) | |
{ | |
timerSTS.cancel(); | |
timerSTS.purge(); | |
timerSTS = null; | |
Toast.makeText(getApplicationContext(), "таймер выключен.", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public void PlaylistSelectorShow(View view) | |
{ | |
showDialog(IDD_DIALOG); | |
} | |
ServiceConnection musicConnection = new ServiceConnection() | |
{ | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
MusicService.MusicBinder binder = (MusicService.MusicBinder) service; | |
//вызов сервиса | |
musicSvc = binder.getService(); | |
//вызов списка песен | |
musicSvc.setList(songFiles); | |
musicSvc.setSong(0); | |
musicBound = true; | |
show_list(); | |
} | |
public void onServiceDisconnected(ComponentName name) { | |
musicBound = false; | |
} | |
}; | |
//меню приложения | |
public boolean onCreateOptionsMenu(Menu menu) | |
{ | |
getMenuInflater().inflate(R.menu.player, menu); | |
return true; | |
} | |
public boolean onOptionsItemSelected(MenuItem item) | |
{ | |
switch (item.getItemId()) | |
{ | |
/* case R.id.action_settings: | |
break;*/ | |
case R.id.action_rescan: | |
Intent rescan = new Intent(MainActivity.this, SplashActivity.class); | |
rescan.putExtra("rescan", true); | |
startActivity(rescan); | |
finish(); | |
break; | |
case R.id.action_about: | |
Intent about = new Intent(MainActivity.this, About.class); | |
startActivity(about); | |
break; | |
case R.id.action_exit: | |
finish(); | |
break; | |
} | |
return false; | |
} | |
public void playback_mode(int id, boolean status) | |
{ | |
switch (id) | |
{ | |
case R.id.tbRep: | |
musicSvc.repeatSongs(status); | |
break; | |
case R.id.tbShuf: | |
musicSvc.shuffleSongs(status); | |
break; | |
case R.id.tbTimer: | |
setTimer(status); | |
break; | |
} | |
} | |
public void song_operations(int id) | |
{ | |
switch (id) | |
{ | |
case R.id.bPlay: | |
togglePlayPause(); | |
break; | |
case R.id.bNext: | |
musicSvc.nextSong(); | |
updateSongInfo(); | |
break; | |
case R.id.bPrev: | |
musicSvc.prevSong(); | |
updateSongInfo(); | |
break; | |
case R.id.bBrowse: | |
show_list(); | |
break; | |
} | |
} | |
protected Dialog onCreateDialog(int id) | |
{ | |
switch (id) | |
{ | |
case IDD_DIALOG: | |
final String[] mCatsName ={"default."}; | |
builder = new AlertDialog.Builder(this); | |
builder.setTitle("выбор плейлиста."); // заголовок для диалога | |
builder.setItems(mCatsName, new DialogInterface.OnClickListener() | |
{ | |
public void onClick(DialogInterface dialog, int item) { | |
} | |
}); | |
builder.setCancelable(true); | |
return builder.create(); | |
case IDD_DIALOG_TIMER: | |
TimePickerDialog tpd = new TimePickerDialog(this, myCallBack, thour, tmin, true); | |
return tpd; | |
default: | |
return null; | |
} | |
} | |
OnTimeSetListener myCallBack = new OnTimeSetListener() { | |
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { | |
thour = hourOfDay; | |
tmin = minute; | |
if(thour > 0 && tmin > 0) | |
{ | |
timerSTS.schedule( taskSts, (((thour * 60) + tmin) * 60) * 1000); | |
} | |
if(thour == 00 && tmin > 0) | |
{ | |
timerSTS.schedule( taskSts, (tmin * 60) * 1000); | |
} | |
if(thour == 00 && tmin == 00) | |
{ | |
Toast.makeText(getApplicationContext(), "неправильно выбрано время.", Toast.LENGTH_SHORT).show(); | |
playerFrag.tbTimer.setChecked(false); | |
} | |
if(thour != 00 && tmin != 00) | |
{ | |
Toast.makeText(getApplicationContext(), thour + " : " + tmin, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}; | |
public void updateSongInfo() | |
{ | |
if (playerFrag.isVisible()) | |
{ | |
playerFrag.updateAlbumArt(); | |
playerFrag.updateTags(); | |
playerFrag.setMaxDuration(musicSvc.getDuration()); | |
} else if (miniPlayerFragment.isVisible()) | |
{ | |
songListFragment.refreshList(); | |
} | |
} | |
public void togglePlayPause() | |
{ | |
musicSvc.togglePlayPause(); | |
} | |
public void open_song(int position) | |
{ | |
musicSvc.setStartIndex(position); | |
musicSvc.setSong(position); | |
transaction = manager.beginTransaction(); | |
transaction.remove(miniPlayerFragment); | |
transaction.remove(songListFragment); | |
transaction.add(R.id.container, playerFrag); | |
transaction.addToBackStack(null); | |
transaction.commit(); | |
musicSvc.playSong(); | |
} | |
public void show_list() | |
{ | |
manager = getFragmentManager(); | |
if (manager.getBackStackEntryCount() > 0) | |
{ | |
manager.popBackStack(); | |
} | |
else | |
{ | |
transaction = manager.beginTransaction(); | |
transaction.remove(playerFrag); | |
transaction.add(R.id.container, songListFragment); | |
transaction.add(R.id.container, miniPlayerFragment); | |
transaction.commit(); | |
} | |
} | |
public ArrayList<Song> get_song_list() | |
{ | |
return songFiles; | |
} | |
protected void onDestroy() | |
{ | |
stopService(playIntent); | |
unbindService(musicConnection); | |
musicSvc = null; | |
super.onDestroy(); | |
} | |
public void set_progress(int i) | |
{ | |
musicSvc.seekTo(i); | |
} | |
//переход в плеер | |
public void goToPlayer() | |
{ | |
transaction = manager.beginTransaction(); | |
transaction.remove(miniPlayerFragment); | |
transaction.remove(songListFragment); | |
transaction.add(R.id.container, playerFrag); | |
transaction.addToBackStack(null); | |
transaction.commit(); | |
} | |
public String get_artist() | |
{ | |
return songFiles.get(musicSvc.playingIndex()).getArtist(); | |
} | |
public String get_album() | |
{ | |
return songFiles.get(musicSvc.playingIndex()).getAlbum(); | |
} | |
public String get_title() | |
{ | |
return songFiles.get(musicSvc.playingIndex()).getTitle(); | |
} | |
public byte[] get_album_art() | |
{ | |
return songFiles.get(musicSvc.playingIndex()).getAlbum_Art(getBaseContext()); | |
} | |
public int get_song_id() | |
{ | |
return musicSvc.playingIndex(); | |
} | |
public int get_duration() | |
{ | |
return musicSvc.getDuration(); | |
} | |
public int get_elapsed() | |
{ | |
return musicSvc.getElapsed(); | |
} | |
public boolean is_playing() | |
{ | |
return musicSvc.isPlaying(); | |
} | |
protected void onResume() | |
{ | |
super.onResume(); | |
if (songCompletedListener == null) | |
songCompletedListener = new SongCompletedListener(); | |
intentFilter = new IntentFilter("Refresh the Song Info"); | |
registerReceiver(songCompletedListener, intentFilter); | |
updateSongInfo(); | |
} | |
protected void onPause() | |
{ | |
if (songCompletedListener != null) unregisterReceiver(songCompletedListener); | |
super.onPause(); | |
} | |
class SongCompletedListener extends BroadcastReceiver | |
{ | |
public void onReceive(Context context, Intent intent) | |
{ | |
if (intent.getAction().equals("Refresh the Song Info")) | |
{ | |
updateSongInfo(); | |
} | |
} | |
} | |
public void onBackPressed() | |
{ | |
if (manager.getBackStackEntryCount() > 0) | |
manager.popBackStack(); | |
else | |
moveTaskToBack(true); | |
} | |
} |
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
package laffhelm.nm; | |
import android.content.ContentUris; | |
import android.content.Context; | |
import android.media.MediaMetadataRetriever; | |
import android.net.Uri; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.provider.MediaStore; | |
import java.io.Serializable; | |
public class Song implements Serializable, Comparable<Song> | |
{ | |
public long id; | |
public String file_name; | |
public String title; | |
public String artist; | |
public String album; | |
public String duration; | |
public MediaMetadataRetriever mediaMetadataRetriever; | |
//имя файла, обычно если нет имени альбома илм названия | |
public String getFile_Name() | |
{ | |
return file_name; | |
} | |
public Song(long id, String title, String file_name, String artist, String album, String duration) | |
{ | |
this.id = id; | |
this.title = title; | |
this.file_name = file_name; | |
this.artist = artist; | |
this.album = album; | |
this.duration = duration; | |
} | |
//длительность | |
public String getDuration() | |
{ | |
String minutes = String.valueOf(Integer.valueOf(duration) / 60000); | |
String seconds = String.valueOf((Integer.valueOf(duration) / 1000) % 60); | |
if (minutes.length() < 2) | |
{ | |
minutes = "0" + minutes; | |
} | |
if (seconds.length() < 2) | |
{ | |
seconds = "0" + seconds; | |
} | |
return minutes + ":" + seconds; | |
} | |
//присвоение айди | |
public long getId() | |
{ | |
return id; | |
} | |
//название | |
public String getTitle() | |
{ | |
if (title.equals("") || title == null) | |
{ | |
return file_name.substring(0, file_name.length() - 4); | |
} | |
return title; | |
} | |
//исполнитель | |
public String getArtist() | |
{ | |
if (artist.equals("<unknown>")) | |
{ | |
return "<unknown>"; | |
} | |
return artist; | |
} | |
//название альбома | |
public String getAlbum() | |
{ | |
return album; | |
} | |
//обложка | |
public byte[] getAlbum_Art(Context context) | |
{ | |
mediaMetadataRetriever = new MediaMetadataRetriever(); | |
mediaMetadataRetriever.setDataSource(context, getUri()); | |
return mediaMetadataRetriever.getEmbeddedPicture(); | |
} | |
//путь к файлу | |
public Uri getUri() | |
{ | |
return ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); | |
} | |
public int compareTo(Song song) | |
{ | |
return this.title.toUpperCase().compareTo(song.getTitle().toUpperCase()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment