Skip to content

Commit

Permalink
Somewhat better handling of media player creation. SoundPool lacks so…
Browse files Browse the repository at this point in the history
…me features like report current position, if not that would probably be preferable...
  • Loading branch information
torkildr committed Oct 25, 2010
1 parent 5673a80 commit 8a026e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/gen
/bin
/assets
.DS_Store
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<string name="app_name">Trommelyd</string>

<string name="sound_error">Ba-dom-tschh (sorry, didn\'t work)</string>
<string name="sound_error">Ba-dom-tschh! Sorry, error during play :(</string>
<string name="first_run">Now what?</string>
<string name="url">http://app.trommelyd.no/</string>

Expand Down
12 changes: 9 additions & 3 deletions src/no/trommelyd/android/TrommelydPlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ private synchronized boolean createMediaPlayer() {
if (mPlayer != null) {
mPlayer.release();
}

// Create player and bind completion listener
mPlayer = MediaPlayer.create(this, resource);

// Sometimes, MediaPlayer.create will fail, this is by no means a bullet-proof
// solution, but it at least gives the player a chance to fix itself.
int retries = 5;

do {
// Create player and bind completion listener
mPlayer = MediaPlayer.create(this, resource);
} while (mPlayer == null && --retries > 0);

// Player not created (should this maybe be handled better?)
if (mPlayer != null) {
mPlayer.setOnCompletionListener(this);
Expand Down
25 changes: 21 additions & 4 deletions src/no/trommelyd/android/TrommelydPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceActivity;
Expand Down Expand Up @@ -34,18 +35,27 @@
*
* @author torkildr
*/
public class TrommelydPreferences extends PreferenceActivity {
public class TrommelydPreferences extends PreferenceActivity
implements OnSharedPreferenceChangeListener {

public static final String PREF_MUTED = "muted";
public static final String PREF_COUNT = "count";
public static final String PREF_REPEAT = "repeat";
public static final String PREF_STARTUP = "startup";
public static final String PREF_FIRST = "first_run";

// Keep track of changes
private boolean mIsChanged = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Grab preferences and register the onChange listener
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.registerOnSharedPreferenceChangeListener(this);

// Preferences to change
addPreferencesFromResource(R.xml.preferences);

// New category
Expand Down Expand Up @@ -73,12 +83,19 @@ protected void onCreate(Bundle savedInstanceState) {
intentPref.setSummary("Sound played " + count + " time" +
((count > 1) ? "s" : ((count == 0) ? "s" : "")));
}


@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
mIsChanged = true;
}

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

Toast.makeText(this, R.string.preference_saved, Toast.LENGTH_SHORT).show();

if (mIsChanged) {
Toast.makeText(this, R.string.preference_saved, Toast.LENGTH_SHORT).show();
}
}

}

0 comments on commit 8a026e5

Please sign in to comment.