play the pressed song

This commit is contained in:
tibbi 2016-01-10 22:31:49 +01:00
parent 4d3c066697
commit a53dc5a462
3 changed files with 166 additions and 3 deletions

View file

@ -3,6 +3,9 @@
package="musicplayer.simplemobiletools.com"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -18,5 +21,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="musicplayer.simplemobiletools.com.MusicService"/>
</application>
</manifest>

View file

@ -1,9 +1,14 @@
package musicplayer.simplemobiletools.com;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
@ -21,8 +26,11 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
private static final int MIN_DURATION_SECS = 20;
private static final int MIN_DURATION_MS = 20000;
private ArrayList<Song> songs;
private MusicService musicService;
private boolean isMusicBound;
@Bind(R.id.playPauseBtn) ImageView playPauseBtn;
@Bind(R.id.songs) ListView songsList;
@Bind(R.id.songTitle) TextView titleTV;
@ -45,9 +53,14 @@ public class MainActivity extends AppCompatActivity {
songPicked(position);
}
});
final Intent intent = new Intent(this, MusicService.class);
bindService(intent, musicConnection, Context.BIND_AUTO_CREATE);
startService(intent);
}
private void songPicked(int pos) {
musicService.setSong(pos, true);
updateSongInfo(songs.get(pos));
}
@ -76,7 +89,7 @@ public class MainActivity extends AppCompatActivity {
final int artistIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
final int durationIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
do {
if (musicCursor.getInt(durationIndex) > MIN_DURATION_SECS) {
if (musicCursor.getInt(durationIndex) > MIN_DURATION_MS) {
final long id = musicCursor.getLong(idIndex);
final String title = musicCursor.getString(titleIndex);
final String artist = musicCursor.getString(artistIndex);
@ -87,6 +100,31 @@ public class MainActivity extends AppCompatActivity {
}
}
private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
MusicService.MyBinder binder = (MusicService.MyBinder) iBinder;
musicService = binder.getService();
musicService.setSongs(songs);
isMusicBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
isMusicBound = false;
}
};
@Override
protected void onDestroy() {
if (isMusicBound) {
isMusicBound = false;
unbindService(musicConnection);
}
super.onDestroy();
}
@OnClick(R.id.previousBtn)
public void previousClicked() {
if (isPlaylistEmpty())

View file

@ -1,13 +1,89 @@
package musicplayer.simplemobiletools.com;
import android.app.Service;
import android.content.ContentUris;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
public class MusicService extends Service {
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class MusicService extends Service
implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
private static final String TAG = MusicService.class.getSimpleName();
private ArrayList<Song> songs;
private final IBinder musicBind = new MyBinder();
private MediaPlayer player;
private ArrayList<Integer> playedSongIDs;
private Song currSong;
@Override
public void onCreate() {
super.onCreate();
songs = new ArrayList<>();
playedSongIDs = new ArrayList<>();
initMediaPlayer();
}
public void initMediaPlayer() {
player = new MediaPlayer();
player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
public void playNext() {
setSong(getNewSongId(), true);
}
private int getNewSongId() {
final int cnt = songs.size();
if (cnt == 0) {
return -1;
} else if (cnt == 1) {
return 0;
} else {
final Random random = new Random();
int newSongIndex = playedSongIDs.size() - 1;
// make sure we do not repeat the same song
while (newSongIndex == playedSongIDs.size() - 1) {
newSongIndex = random.nextInt(cnt - 1);
}
return newSongIndex;
}
}
public void setSong(int songId, boolean addNewSong) {
if (player == null)
initMediaPlayer();
player.reset();
if (addNewSong)
playedSongIDs.add(songId);
currSong = songs.get(songId);
try {
final Uri trackUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, currSong.getId());
player.setDataSource(getApplicationContext(), trackUri);
} catch (IOException e) {
Log.e(TAG, "setSong IOException " + e.getMessage());
}
player.prepareAsync();
}
@Nullable
@Override
@ -15,6 +91,49 @@ public class MusicService extends Service {
return musicBind;
}
@Override
public boolean onUnbind(Intent intent) {
if (player != null && !player.isPlaying()) {
destroyPlayer();
}
return false;
}
public void setSongs(ArrayList<Song> songs) {
this.songs = songs;
}
@Override
public void onCompletion(MediaPlayer mp) {
if (player.getCurrentPosition() > 0) {
player.reset();
playNext();
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
player.reset();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
destroyPlayer();
}
private void destroyPlayer() {
player.stop();
player.release();
player = null;
}
public class MyBinder extends Binder {
MusicService getService() {
return MusicService.this;