Skip to content

Commit

Permalink
Move audio playback to service.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdryomov committed Jun 20, 2014
1 parent c1ba936 commit 1bf8432
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 84 deletions.
7 changes: 7 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<uses-permission
android:name="android.permission.USE_CREDENTIALS"/>

<uses-permission
android:name="android.permission.WAKE_LOCK"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/application_name"
Expand Down Expand Up @@ -93,6 +96,10 @@

</service>

<service
android:name=".service.AudioService">
</service>

<meta-data
android:name="com.crashlytics.ApiKey"
android:value="d7b65346d3cf0028328f006bff447501d70f8996"/>
Expand Down
181 changes: 97 additions & 84 deletions src/main/java/org/amahi/anywhere/activity/ServerFileAudioActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
package org.amahi.anywhere.activity;

import android.app.Activity;
import android.media.AudioManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
Expand All @@ -43,18 +47,20 @@
import org.amahi.anywhere.server.client.ServerClient;
import org.amahi.anywhere.server.model.ServerFile;
import org.amahi.anywhere.server.model.ServerShare;
import org.amahi.anywhere.service.AudioService;
import org.amahi.anywhere.task.AudioMetadataRetrievingTask;
import org.amahi.anywhere.util.AudioAlbumArtDownloader;
import org.amahi.anywhere.util.Intents;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;

public class ServerFileAudioActivity extends Activity implements MediaController.MediaPlayerControl, MediaPlayer.OnPreparedListener
public class ServerFileAudioActivity extends Activity implements ServiceConnection,
MediaPlayer.OnPreparedListener,
MediaController.MediaPlayerControl
{
public static final Set<String> SUPPORTED_FORMATS;

Expand All @@ -67,45 +73,24 @@ public class ServerFileAudioActivity extends Activity implements MediaController
));
}

private static final class SavedState
{
private SavedState() {
}

public static final String AUDIO_TIME = "audio_time";
}

@Inject
ServerClient serverClient;

private MediaPlayer audioPlayer;

private AudioService audioService;
private MediaController audioControls;

private int audioTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_file_audio);

setUpSavedState(savedInstanceState);

setUpInjections();

setUpHomeNavigation();

setUpAudio();
}

private void setUpSavedState(Bundle savedState) {
if (savedState == null) {
return;
}

audioTime = savedState.getInt(SavedState.AUDIO_TIME);
}

private void setUpInjections() {
AmahiApplication.from(this).inject(this);
}
Expand Down Expand Up @@ -174,20 +159,35 @@ private void setUpAudioAlbumArt() {
protected void onStart() {
super.onStart();

setUpAudioPlayer();
setUpAudioService();
setUpAudioServiceBind();
}

private void setUpAudioService() {
Intent intent = new Intent(this, AudioService.class);
startService(intent);
}

private void setUpAudioServiceBind() {
Intent intent = new Intent(this, AudioService.class);
bindService(intent, this, Context.BIND_AUTO_CREATE);
}

@Override
public void onServiceDisconnected(ComponentName serviceName) {
}

@Override
public void onServiceConnected(ComponentName serviceName, IBinder serviceBinder) {
setUpAudioServiceBind(serviceBinder);

setUpAudioControls();
setUpAudioPlayback();
}

private void setUpAudioPlayer() {
try {
audioPlayer = new MediaPlayer();
audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioPlayer.setDataSource(this, getAudioUri());
audioPlayer.setOnPreparedListener(this);
audioPlayer.prepareAsync();
} catch (IOException e) {
throw new RuntimeException(e);
}
private void setUpAudioServiceBind(IBinder serviceBinder) {
AudioService.AudioServiceBinder audioServiceBinder = (AudioService.AudioServiceBinder) serviceBinder;
audioService = audioServiceBinder.getAudioService();
}

private void setUpAudioControls() {
Expand All @@ -197,22 +197,27 @@ private void setUpAudioControls() {
audioControls.setAnchorView(findViewById(R.id.animator));
}

private void setUpAudioPlayback() {
if (audioService.isStarted()) {
showAudio();
} else {
audioService.startAudio(getAudioUri(), this);
}
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
setUpAudioTime();
public void onPrepared(MediaPlayer audioPlayer) {
start();

showAudio();
showAudioControls();
}

private void setUpAudioTime() {
if (audioPlayer.getCurrentPosition() == 0) {
audioPlayer.seekTo(audioTime);
}
private void showAudio() {
showAudioMetadata();
showAudioControls();
}

private void showAudio() {
private void showAudioMetadata() {
ViewAnimator animator = (ViewAnimator) findViewById(R.id.animator);

View content = findViewById(R.id.layout_content);
Expand All @@ -223,15 +228,27 @@ private void showAudio() {
}

private void showAudioControls() {
Animation showAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_up_view);
audioControls.startAnimation(showAnimation);
if (areAudioControlsAvailable() && !audioControls.isShowing()) {
Animation showAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_up_view);
audioControls.startAnimation(showAnimation);

audioControls.show(0);
}
}

private boolean areAudioControlsAvailable() {
return audioControls != null;
}

audioControls.show(0);
private void hideAudioControls() {
if (areAudioControlsAvailable()) {
audioControls.hide();
}
}

@Override
public void start() {
audioPlayer.start();
audioService.getAudioPlayer().start();
}

@Override
Expand All @@ -241,7 +258,7 @@ public boolean canPause() {

@Override
public void pause() {
audioPlayer.pause();
audioService.getAudioPlayer().pause();
}

@Override
Expand All @@ -256,22 +273,22 @@ public boolean canSeekForward() {

@Override
public void seekTo(int time) {
audioPlayer.seekTo(time);
audioService.getAudioPlayer().seekTo(time);
}

@Override
public int getDuration() {
return audioPlayer.getDuration();
return audioService.getAudioPlayer().getDuration();
}

@Override
public int getCurrentPosition() {
return audioPlayer.getCurrentPosition();
return audioService.getAudioPlayer().getCurrentPosition();
}

@Override
public boolean isPlaying() {
return audioPlayer.isPlaying();
return audioService.getAudioPlayer().isPlaying();
}

@Override
Expand All @@ -281,65 +298,61 @@ public int getBufferPercentage() {

@Override
public int getAudioSessionId() {
return audioPlayer.getAudioSessionId();
return audioService.getAudioPlayer().getAudioSessionId();
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
finish();
return true;

default:
return super.onOptionsItemSelected(menuItem);
}
}

@Override
protected void onResume() {
super.onResume();

BusProvider.getBus().register(this);
showAudioControls();

audioPlayer.start();
BusProvider.getBus().register(this);
}

@Override
protected void onPause() {
super.onPause();

BusProvider.getBus().unregister(this);

hideAudioControls();
}

private void hideAudioControls() {
audioControls.hide();
BusProvider.getBus().unregister(this);
}

@Override
protected void onStop() {
super.onStop();

audioTime = getCurrentPosition();

tearDownAudioPlayer();
tearDownAudioServiceBind();
}

private void tearDownAudioPlayer() {
audioPlayer.stop();
audioPlayer.release();
private void tearDownAudioServiceBind() {
unbindService(this);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
protected void onDestroy() {
super.onDestroy();

tearDownSavedState(outState);
}

private void tearDownSavedState(Bundle savedState) {
savedState.putInt(SavedState.AUDIO_TIME, getCurrentPosition());
if (isFinishing()) {
tearDownAudioService();
}
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
finish();
return true;

default:
return super.onOptionsItemSelected(menuItem);
}
private void tearDownAudioService() {
Intent intent = new Intent(this, AudioService.class);
stopService(intent);
}
}
Loading

0 comments on commit 1bf8432

Please sign in to comment.