As far as I can tell, my app runs fine but I notice this error from the SoundPool being reporting often in the logcat.
E/SoundPool(28182): 2 mChannels.erase can not get the lock
Any ideas as to what would cause this? I tried removing my thread synchronization code but I am still receiving the error.
This was observed using a Galaxy S2 (SC-02C) running Android 2.3.6.
Another solution that I propose for you :
Create an instance of SoundPool by sound with maxStreams = 1
Set volume 0 to only one streamID returned by play() method
This solution is not very clean I agree...
public class Sound {
public final static int STREAM = AudioManager.STREAM_SYSTEM;
private SoundPool pool;
private int soundID;
private int streamID;
public Sound(Context context, int resID) {
pool = new SoundPool(1, STREAM, 0);
soundID = pool.load(context, resID, 1);
}
public final void play() {
streamID = pool.play(soundID, 1, 1, 1, 0, 1);
}
public final void stop() {
pool.setVolume(streamID, 0, 0);
}
}
public class MyActivity extends Activity {
public static Sound alarm1;
public static Sound alarm2;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
setVolumeControlStream(Sound.STREAM);
if(alarm1 == null) alarm1 = new Sound(this, R.raw.alarm1);
if(alarm2 == null) alarm2 = new Sound(this, R.raw.alarm2);
findViewById(R.id.play_1).setOnClickListener( new OnClickListener() {
#Override public void onClick(View view) { alarm1.play(); }
});
findViewById(R.id.stop_1).setOnClickListener( new OnClickListener() {
#Override public void onClick(View view) { alarm1.stop(); }
});
findViewById(R.id.play_2).setOnClickListener( new OnClickListener() {
#Override public void onClick(View view) { alarm2.play(); }
});
findViewById(R.id.stop_2).setOnClickListener( new OnClickListener() {
#Override public void onClick(View view) { alarm2.stop(); }
});
}
}
And to manage all your sounds, we can imagine a library like this :
public class Soundlib {
private Context context;
private HashMap<Integer, Sound> library;
public Soundlib(Context context) {
this.context = context;
library = new HashMap<Integer, Sound>();
}
public final void add(int... rawID) {
for(int id : rawID) library.put(id, new Sound(context, id));
}
public final void play(int... rawID) {
for(int id : rawID) library.get(id).play();
}
public final void stop(int... rawID) {
for(int id : rawID) library.get(id).stop();
}
public final void stopAll() {
for(int id : library.keySet()) stop(id);
}
}
// to initilize sound library
if(soundlib == null) soundlib = new Soundlib(this){{
// to add one or more sounds
add(R.raw.alarm1, R.raw.alarm2);
}};
// to play one or more sounds
soundlib.play(R.raw.alarm1);
// to stop one or more sounds
soundlib.stop(R.raw.alarm1);
// to stop all sounds
soundlib.stopAll();
I think this error log is caused by the play() / stop() method of your SoundPool instance.
Do you try to play / stop an audio stream?
Related
I'm building app which make tic sound on clicking the button. But the sound stops after clicking 14 time. The code is here:
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view){
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tic_sound);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();
}
});
No need to initialise it every time you click the button.
private MediaPlayer _mediaPlayer;
#Override
protected void onResume()
{
super.onResume();
_mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tic_sound);
_mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState)
{
//...
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view) {
_mediaPlayer.start();
}
});
}
For short sounds i would recommend using SoundPool
Class SoundPlayer(){
private final static float leftVol = 1.0f;
private final static float rightVol = 1.0f;
private final static float rate = 1.0f;
private final static int loop = 0;
private int priority = 1;
private int audioId = = -1;
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
SoundPool soundPool= new SoundPool.Builder()
.setMaxStreams(2)
.setAudioAttributes(attributes)
.build();
public void loadSounds(Context context){
audioId = soundPool.load(context, R.raw.tic_sound, priority);
}
public void playAudio() {
soundPool.play(audioId, leftVol, rightVol, priority, loop, rate) ;
}
}
And use it like this
private SoundPlayer soundPlayer;
....
....
onCreate ...{
soundPlayer = SoundPlayer();
soundPLayer.loadSounds(this);
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view){
soundPLayer.playAudio();
}
I've this class (my media player class) and I need to create 2 methods on it, one to change the volume and other to get the current volume. (I'll create a seekbar to change the player volume), ok.
public class LoopMediaPlayer {
private Context ctx = null;
private int rawId = 0;
private MediaPlayer currentPlayer = null;
private MediaPlayer nextPlayer = null;
public static LoopMediaPlayer create(Context ctx, int rawId) {
return new LoopMediaPlayer(ctx, rawId);
}
private LoopMediaPlayer(Context ctx, int rawId) {
this.ctx = ctx;
this.rawId = rawId;
currentPlayer = MediaPlayer.create(this.ctx, this.rawId);
currentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
currentPlayer.start();
}
});
createNextMediaPlayer();
}
private void createNextMediaPlayer() {
nextPlayer = MediaPlayer.create(ctx, rawId);
currentPlayer.setNextMediaPlayer(nextPlayer);
currentPlayer.setOnCompletionListener(onCompletionListener);
}
private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
currentPlayer = nextPlayer;
createNextMediaPlayer();
}
};
public void stopPlayer() {
if (currentPlayer != null && currentPlayer.isPlaying()) {
currentPlayer.stop();
currentPlayer.release();
}
if (nextPlayer != null && nextPlayer.isPlaying()) {
nextPlayer.stop();
nextPlayer.release();
}
}
public void setVolume(float vol) {
//todo
currentPlayer.setVolume(vol, vol);//don't work
}
public int getVolume(){
//todo
return 0;
}
}
and my seekBar progress and listener
seekBar.setProgress(player.getVolume());//use the method that retrieve the current volume
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, final int i, boolean b) {
runOnUiThread(new Runnable() {
#Override
public void run() {
player.setVolume(i);//use the method that change the volume
}
});
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I tried mp.setVolume() but didn't change any thing. And I can't get the current volume to set like the seekbar progress before show it.
Details - All Audios are in raw file (not stream). There will be more than one player playing at same time, them will be stored so I can retrieve any of them any time, and I want to change and retrieve just the volume of a specific player not of all them at same time.
Anyone know how I should implement both this methods, 'cause I have no idea.
Thanks
how are you supposed to let the mediaplayer know what it is supposed to play?
wholeTextPlayer = MediaPlayer.create(Lesson1Reading.this, engAu);
It works fine if I declare the file in the top:
MediaPlayer wholeTextPlayer;
private int engAu = R.raw.l1r_en_l10;
Button btn_default_acc_whole;
It doesn't work from within a button click if / else statement wherever I try to put it with the following combination:
MediaPlayer wholeTextPlayer;
private int engAu;
Button btn_default_acc_whole;
The button click:
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (wholeTextPlayer.isPlaying()) {
wholeTextPlayer.pause();
} else {
wholeTextPlayer.start();
startPlayProgressUpdater();
}
setEngAu(R.raw.l1r_en_l10); //this line doesn't want to fit anywhere
}
});
The setter:
public void setEngAu(int engAu) {
this.engAu = engAu;
}
Of course they are separately placed in the activity, I just copied and pasted the relevant bits from it.
Thanks guys.
Here is the whole code:
'public class Lesson1Grammar extends Activity {
private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
private final Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson1grammar);
WholeDefAccPlayer();
final RelativeLayout playerScreen = (RelativeLayout)findViewById(R.id.playerScreen);
final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
btn_player_screen.setOnClickListener(new View.OnClickListener() {
//this hides/unhides the part of the layout in which the player is
#Override
public void onClick(View arg0) {
if (playerScreen.isShown()) {
playerScreen.setVisibility(View.GONE);
} else {
playerScreen.setVisibility(View.VISIBLE);
}
}
});
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (wholeTextPlayer.isPlaying()) {
wholeTextPlayer.pause();
} else {
setEngAu(R.raw.default_acc_audio);
wholeTextPlayer.start();
startPlayProgressUpdater();
}
}
});
}
public void setEngAu(int engAu) {
this.engAu = engAu;
}
private void WholeDefAccPlayer() {
wholeTextPlayer = MediaPlayer.create(Lesson1Grammar.this, engAu);
((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(wholeTextPlayer.getDuration());
seekBar.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
public void startPlayProgressUpdater() {
seekBar.setProgress(wholeTextPlayer.getCurrentPosition());
if (wholeTextPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
else wholeTextPlayer.pause();
}
// This is event handler thumb moving event
private void seekChange(View v){
if(wholeTextPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
wholeTextPlayer.seekTo(sb.getProgress());
}
}
}
'
if your plan is to make the method setEngAu() is the controller as you mentioned in the comment. then you you need to use that method like this
public void setEngAu(int enAu)
{
this.EngAu = enAu;
wholeTextPlayer.setDataSource(engAu);
wholeTextPlayer.prepare();
}
you need to implement onPrepared listener from the media player
I do not know your classes but I assume where you say something like;
wholeTextPlayer = new MediaPlayer();
wholeTextPlayer.setOnPreparedListener(this);
here you start the player after it became prepared
public void onPrepared(MediaPlayer player)
{
player.start();
}
Remember to you will need to call wholeTextPlayer.release() if you do not want the player to hold on that resource anymore( think of it as memory issues - recommended if you check the documentation)
EDIT :
I adjusted your code a little, please take a look and let me know.
private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
final Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson1grammar);
WholeDefAccPlayer();
final RelativeLayout playerScreen = (RelativeLayout) findViewById(R.id.playerScreen);
final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
btn_player_screen.setOnClickListener(new View.OnClickListener()
{
//this hides/unhides the part of the layout in which the player is
#Override
public void onClick(View arg0)
{
if(playerScreen.isShown())
{
playerScreen.setVisibility(View.GONE);
}
else
{
playerScreen.setVisibility(View.VISIBLE);
}
}
});
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{//problem here is
if(playerState == PlayerState_Playing)
{
wholeTextPlayer.pause();
setPlayerState(PlayerState_Paused);
}
else if(playerState == PlayerState_Paused)
{
wholeTextPlayer.start();
setPlayerState(PlayerState_Playing);
}
else
{
setEngAu(R.raw.default_acc_audio);
wholeTextPlayer.start();
startPlayProgressUpdater();
setPlayerState(PlayerState_Playing);
}
}
});
}
public void setEngAu(int engAu)
{
this.engAu = engAu;
if(wholeTextPlayer !=null)
{//just in case you call this method and player was not initialized yet
wholeTextPlayer.release();
}
setPlayerState(PlayerState_Preparing);
wholeTextPlayer = MediaPlayer.create(this, engAu);
}
private void WholeDefAccPlayer()
{
//this line probably will fail because engAu is not really initialized yet, unless you have default value for it
wholeTextPlayer = MediaPlayer.create(this, engAu);
((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//you can not call getDuration unless the player is prepared, so this might crash you
// seekBar.setMax(wholeTextPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
{
if(playerState != PlayerState_Preparing)
{//if player state is preparing it means we can not seek yet, player.start() must be called first
wholeTextPlayer.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
}
public void startPlayProgressUpdater()
{
if(seekBar.getMax() != wholeTextPlayer.getDuration())
{//in case you change track later, this will check if the seek bar max value with track duration.
// if they are not the same, then it will adjust it for you
seekBar.setMax(wholeTextPlayer.getDuration());
}
seekBar.setProgress(wholeTextPlayer.getCurrentPosition());
if(wholeTextPlayer.isPlaying())
{
Runnable notification = new Runnable()
{
public void run()
{
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
else wholeTextPlayer.pause();
}
// This is event handler thumb moving event
private void seekChange(View v)
{
if(wholeTextPlayer.isPlaying())
{
SeekBar sb = (SeekBar) v;
wholeTextPlayer.seekTo(sb.getProgress());
}
}
private final int PlayerState_Preparing = 0;
private final int PlayerState_Playing = 1;
private final int PlayerState_Paused = 2;
private int playerState;
private void setPlayerState(int state)
{//this is used to track the player state, because wholeTextPlayer.isPlaying() can return false in many conditions ( too general )
//you can check in the media player documentation to know more details about this
playerState = state;
}
I've done a little with android in the past, but this is the first time I've touched it in over a year and I've been stuck on this problem since yesterday.
I'm working on a project with someone and I need to play a sound a certain number of times at set intervals. (For example, play once after one minute, twice after two minutes, three times after three minutes, and on and on). I can get the sound to play at whatever interval, that's not an issue, but I can't figure out how to get it to play the correct number of times at each interval. It either ends up looping infinitely, playing once each time the interval is up or playing once and stopping.
Tried TimerTask, switched to Handler/Runnable, tried using a for loop and using an if statement with counter. After two evenings of multiple attempts, hours of research and my limited experience, this is the one problem I've run into that I haven't been able to figure out.
Here's the code I've currently got in for this particular feature. I'm having issues with the Runnable tenMinChime and OnCompletionListener chimeCompletion. Any guidance at all is very appreciated.
public class MainActivity extends AppCompatActivity {
TextView chimeOn, chimeOff;
Handler chimeHandler = new Handler();
MediaPlayer.OnCompletionListener chimeCompletion;
MediaPlayer cp;
int chimeCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (screen == 1) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
setContentView(R.layout.activity_main);
chimeOn = (TextView) findViewById(R.id.chimeOn);
chimeOff = (TextView) findViewById(R.id.chimeOff);
chimeOn.setTextColor(0xFFbebebe);
chimeOff.setTextColor(0xFF000000);
cp = MediaPlayer.create(this, R.raw.placeholder_chime);
chimeOn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (chime == 0) {
chimeOn.setTextColor(0xFF000000);
chimeOff.setTextColor(0xFFbebebe);
handler2.postDelayed(tenMinChime, 5000);
chime = 1;
}
}
});
chimeOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (chime == 1) {
chimeOff.setTextColor(0xFF000000);
chimeOn.setTextColor(0xFFbebebe);
handler2.removeCallbacks(tenMinChime);
chime = 0;
}
}
});
chimeCompletion = new MediaPlayer.OnCompletionListener() {
int count = 0;
int maxCount = chimeCount;
#Override
public void onCompletion(MediaPlayer mp) {
if(count < maxCount) {
count++;
cp.seekTo(0);
cp.start();
cp.setOnCompletionListener(chimeCompletion);
}
}
};
public Runnable tenMinChime = new Runnable() {
public void run() {
chimeCount+=1;
cp.start();
cp.setOnCompletionListener(chimeCompletion);
}
};
Maybe this solves your issue ?
public class MainActivity extends AppCompatActivity {
TextView chimeOn, chimeOff;
Handler handler2 = new Handler();
MediaPlayer.OnCompletionListener chimeCompletion;
MediaPlayer cp;
int chimeCount = 0;
int mPlayCount=0;
int mMaxPlayCount=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (screen == 1) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
setContentView(R.layout.activity_main);
chimeOn = (TextView) findViewById(R.id.chimeOn);
chimeOff = (TextView) findViewById(R.id.chimeOff);
chimeOn.setTextColor(0xFFbebebe);
chimeOff.setTextColor(0xFF000000);
cp = MediaPlayer.create(this, R.raw.placeholder_chime);
chimeOn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (chime == 0) {
chimeOn.setTextColor(0xFF000000);
chimeOff.setTextColor(0xFFbebebe);
handler2.postDelayed(tenMinChime, 5000);
chime = 1;
}
}
});
chimeOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (chime == 1) {
chimeOff.setTextColor(0xFF000000);
chimeOn.setTextColor(0xFFbebebe);
handler2.removeCallbacks(tenMinChime);
cp.setOnCompletionListener(null);
chime = 0;
mMaxPlayCount=0;
mPlayCount=0;
}
}
});
chimeCompletion = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(mPlayCount < mMaxPlayCount) {
mPlayCount++;
cp.seekTo(0);
cp.start();
}
}
};
public Runnable tenMinChime = new Runnable() {
public void run() {
mPlayCount=0;
mMaxPlayCount= 3;
cp.setOnCompletionListener(chimeCompletion);
cp.start();
}
};
I coded a simple mp3 player a couple of days ago. The code is at Playing music in sleep/standby mode in Android 2.3.3
Everything works fine when the mp3 player (Samsung 3.6, Android 2.2) plays music on its own. I bought a bluetooth speaker (A2DP 3.0) and connected it to the mp3 player. The music still plays fine, but when the mp3 player screen goes dark into sleep mode, the music starts skipping on the bluetooth speaker.
It does not happen if the mp3 plays music on its own in sleep mode
Other music player apps in the mp3 player seem to play fine even in sleep mode over the bluetooth speaker
If I disable bluetooth, and play the music using the speaker connected by a cable, then the music plays fine even in standby mode.
This leads me to believe that something may be wrong with my code.
Here's my complete code:
public class MainActivity extends Activity {
private static String audioPath;
private static ArrayList<String> devotionalList;
private static ArrayList<String> christmasList;
private static ArrayList<String> cinemaList;
private static ImageButton playPauseButton;
private static TextView appMsg;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar) findViewById(R.id.volumeBar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
arg1, 0);
}
});
File baseDir = Environment.getExternalStorageDirectory();
audioPath = baseDir.getAbsolutePath() + "/external_sd/music/";
devotionalList = loadSongs(audioPath + "devotional/");
christmasList = loadSongs(audioPath + "christmas/");
cinemaList = loadSongs(audioPath + "cinema/");
}
private ArrayList<String> loadSongs(String songDirectory) {
File directory = new File(songDirectory);
ArrayList<String> al1 = new ArrayList<String>();
for (String filename : directory.list()) {
al1.add(songDirectory + filename);
}
return al1;
}
public void playPauseSong(View v) {
if (MP3PlayerService.isPlaying) {
playPauseButton.setImageResource(android.R.drawable.ic_media_play);
// Intent i1 = new Intent(this, MP3PlayerService.class);
stopService(new Intent(this, MP3PlayerService.class));
} else {
if (MP3PlayerService.playerStarted) {
playPauseButton
.setImageResource(android.R.drawable.ic_media_pause);
startService(new Intent(this, MP3PlayerService.class));
}
}
}
public void playNextSong(View v) {
if (MP3PlayerService.playerStarted) {
startService(new Intent(this, MP3PlayerService.class));
}
}
public void playPreviousSong(View v) {
if (MP3PlayerService.playerStarted) {
MP3PlayerService.previousSong = true;
startService(new Intent(this, MP3PlayerService.class));
}
}
#Override
protected void onPause() {
super.onPause();
playPauseButton = null;
appMsg = null;
}
#Override
protected void onResume() {
super.onResume();
appMsg = (TextView) this.findViewById(R.id.txtAppMsg);
appMsg.setText("");
playPauseButton = (ImageButton) this.findViewById(R.id.btnPlayPause);
}
public void playDevotional(View v) {
playPlayList(devotionalList);
}
public void playChristmas(View v) {
playPlayList(christmasList);
}
public void playCinema(View v) {
playPlayList(cinemaList);
}
private void playPlayList(ArrayList<String> playList) {
if (MP3PlayerService.isPlaying) {
MP3PlayerService.mediaPlayer.stop();
MP3PlayerService.mediaPlayer.reset();
MP3PlayerService.mediaPlayer.release();
}
MP3PlayerService.songPosition = 0;
playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
Intent i1 = new Intent(this, MP3PlayerService.class);
i1.putExtra("playListChoice", playList);
startService(i1);
}
}
And here's my code for the mediaplayer service:
public class MP3PlayerService extends Service implements OnCompletionListener {
private static ArrayList<String> al1;
private static FileInputStream fis;
private static FileDescriptor fd;
static boolean previousSong;
static boolean playerStarted = false;
static int songPosition;
static boolean isPlaying = false;
static String currentSong;
static ArrayList<String> songHistory;
static MediaPlayer mediaPlayer;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
songHistory = new ArrayList<String>();
}
#Override
public void onDestroy() {
songPosition = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
isPlaying = false;
}
#Override
public void onStart(Intent intent, int startid) {
if (songPosition == 0) {
Bundle extras = intent.getExtras();
if (extras == null) {
if (previousSong) {
playSong();
} else {
// Next Song Button clicked
playNextSong();
}
} else {
// A Playlist button is clicked
al1 = (ArrayList<String>) extras.get("playListChoice");
playSong();
}
} else {
songPosition = 0;
mediaPlayer.start();
}
isPlaying = true;
}
private void playNextSong() {
if (isPlaying) {
mediaPlayer.release();
playSong();
}
}
private void playSong() {
if (previousSong) {
mediaPlayer.release();
if (songHistory.size() > 1) {
currentSong = songHistory.get(songHistory.size() - 2);
songHistory.remove(songHistory.size() - 1);
} else {
currentSong = songHistory.get(0);
}
previousSong = false;
} else {
currentSong = al1.get(new Random().nextInt(al1.size()));
songHistory.add(currentSong);
}
try {
fis = new FileInputStream(currentSong);
fd = fis.getFD();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(songPosition);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
playerStarted = true;
} catch (FileNotFoundException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
public void onCompletion(MediaPlayer arg0) {
playNextSong();
}
}
I think I fixed it. The slider volume code was the source of the problem. Without that piece, the app was still working during standby mode using the bluetooth speakers. I wanted the slider bar, so I tried acquring a partial wake lock after instantiating the media player for each song and that kept the app going correctly duing standby mode (with the slider volume control as part of the app as well). I release the lock later after each song.