delayed audio in android - android

i'm trying to play audio when a button is clicked but there's some delay between when i press the button and when the audio is actually played. I get this error: AudioFlinger(17396): write blocked for 162 msecs, 3 delayed writes, thread 0x15440 This is what i got so far:
ImageButton i = (ImageButton)findViewById(R.id.button);
i.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
s = new AudioPlayer(getApplicationContext());
s.playSound(R.raw.conga1);
}
return true;
}
});
AudioPlayer class
public class AudioPlayer {
private MediaPlayer mediaPlayer;
private final OnCompletionListener mediaPlayerListener = new MediaPlayerListener();
private Context context = null;
public AudioPlayer(Context context)
{
this.context = context;
init();
}
private void init() {
if (mediaPlayer == null) {
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int streamVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(mediaPlayerListener);
mediaPlayer.setVolume(streamVolume, streamVolume);
}
}
private void setSound(int id) {
if (mediaPlayer!=null) {
mediaPlayer.reset();
AssetFileDescriptor file = context.getResources().openRawResourceFd(id);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
}
}
public void playSound(int id) {
if (mediaPlayer!=null) {
setSound(id);
mediaPlayer.start();
}
}
private static class MediaPlayerListener implements OnCompletionListener {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.seekTo(0);
}
}
}
Any clues?
Thanks in advance.
EDITS:
So i added a singleton and it helped but there's still delay. This is how it looks like now:
AudioPlayer:
public static synchronized AudioPlayer getSingletonObject(Context context, Uri pathToFile) {
if (audioPlayer == null) {
audioPlayer = new AudioPlayer(context, pathToFile);
}
return audioPlayer;
}
public void setSound(String pathToFile) {
if (mediaPlayer!=null) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(pathToFile);
mediaPlayer.prepareAsync();
} //catch....
}
}
public void playSound(String path) {
if (mediaPlayer!=null) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
mediaPlayer.start();
} //catch...
}
}
Main:
final String path = "sdcard/myappsounds/snaredrum2.wav";
final AudioPlayer s = AudioPlayer.getSingletonObject(getApplicationContext(), Uri.parse(path));
s.setSound(path);
ImageButton i = (ImageButton)findViewById(R.id.button);
i.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
s.playSound(path);
}
return true;
}
});
Any ideas? What i'm trying to do is like a drumset, that's why the buttons must respond correctly.

I/O on Android is slow. The audio subsystem is being forced to wait for it's data, and is kind enough to warn about it.
Rather than giving MediaPlayer a raw FileDescriptor (which will have no buffering applied) try using a file:// URI. With any luck the system content provider for file: will buffer it up for you the moment it's opened.

I don't really know Android in depth, but from looking at this code what you could try is to use your AudioPlayer as a singleton and reuse your MediaPlayer instance?

Related

Problem with MediaPlayer onPrepared() not being called after stop() and prepareAsync()

I start by loading a Media player into a composition class:
public class MediaPlayerWURI {
private final MediaPlayer mediaPlayer;
final Uri uri;
final ActivityMain activityMain;
boolean isPrepared = true;
MediaPlayerWURI(ActivityMain activityMain, MediaPlayer mediaPlayer, Uri uri){
this.activityMain = activityMain;
this.mediaPlayer = mediaPlayer;
this.uri= uri;
mediaPlayer.setOnPreparedListener(null);
mediaPlayer.setOnErrorListener(null);
mediaPlayer.setOnPreparedListener(new MOnPreparedListener(this));
mediaPlayer.setOnErrorListener(new MOnErrorListener());
}
public void prepareAsync(){
isPrepared = false;
mediaPlayer.prepareAsync();
}
public void start(){
mediaPlayer.start();
}
public void stop(){
isPrepared = false;
mediaPlayer.stop();
}
class MOnPreparedListener implements MediaPlayer.OnPreparedListener{
final MediaPlayerWURI mediaPlayerWURI;
public MOnPreparedListener(MediaPlayerWURI mediaPlayerWURI){
this.mediaPlayerWURI = mediaPlayerWURI;
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayerWURI.isPrepared = true;
}
}
class MOnErrorListener implements MediaPlayer.OnErrorListener {
public MOnErrorListener(){
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
activityMain.releaseMediaPlayers();
return false;
}
}
}
The media player passed in is created with MediaPlayer.create(getApplicationContext()) and is started successfully.
The following code do not trigger onPrepared() and gets stuck in a loop.
mediaPlayerWURI.stop();
mediaPlayerWURI.prepareAsync();
while (!mediaPlayerWURI.isPrepared) { }
mediaPlayerWURI.start();
I have tried prepareAsync() on another thread:
executorService.submit(new Runnable() {
#Override
public void run() {
mediaPlayerWURI.prepareAsync();
}
});
My guess is it is a threading issue, but I am not sure how to handle this, or if it even is a threading issue. My understanding is that the MediaPlayer is preparing in another thread and that the loop shouldn't prevent it from calling on prepared. I am not sure what thread onPrepare() is ran on, but from the above, I think it means the main thread is supposed to run onPrepare() and is waiting for the loop to end.
Also, I am getting weird behavior where onPrepared() is being called after the construction of the MediaPlayer. Is that normal? My assumption is that onPrepared() is called when setOnPrepared() is called on a prepared MediaPlayer. This means the listener is attached.
The problem was that while waiting for the MediaPlayer to be prepared while (!mediaPlayerWURI.isPrepared) { }, I was hogging the UI thread, which is the same thread that onPrepared() uses.
To fix this, I had to stop hogging the UI thread. I added a boolean to my MediaPlayerWURI wrapper class that indicates to play the MediaPlayer on prepared.
private final MediaPlayer mediaPlayer;
volatile boolean isPrepared;
volatile boolean shouldPlay;
synchronized public void shouldStart(boolean shouldPlay){
if(shouldPlay && isPrepared){
mediaPlayer.start();
} else {
this.shouldPlay = shouldPlay;
}
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
synchronized (mediaPlayerWURI) {
mediaPlayerWURI.isPrepared = true;
if (shouldPlay) {
mediaPlayer.start();
shouldPlay = false;
}
}
}

How to show/hide play/pause Button in video view in android?

I am doing an Android project based on Video view. I want to show play button before the user clicks play, when user decides to pause video - pause button shows up. Clicking on pause button should trigger playing the video again from the same place where it was paused (like YouTube video).
im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.setVideoURI(Uri.parse("android.resource://com.example.cm.filmfestival/" + R.raw.mission));
im1.setVisibility(View.INVISIBLE);
im2.setVisibility(View.INVISIBLE);
vv1.start();
}
});
im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.stopPlayback();
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
im1.setVisibility(View.VISIBLE);
vv1.start();
im2.setVisibility(View.VISIBLE);
vv1.stopPlayback();
return true;
}
Use the code below
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
public VideoView myVideoView;
private int position = 0;
private MediaController mediaControls;
// set the media controller buttons
if (mediaControls == null)
{
mediaControls = new MediaController(MainActivity.this);
}
// initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
try
{
// set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
// set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("your UrI"));
} catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
// we also set an setOnPreparedListener in order to know when the video
// file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mediaPlayer)
{
// if we have a position on savedInstanceState, the video
// playback should start from here
myVideoView.seekTo(position);
System.out.println("vidio is ready for playing");
if (position == 0)
{
myVideoView.start();
} else
{
// if we come from a resumed activity, video playback will
// be paused
myVideoView.pause();
}
}
});
Set an OntouchListener on your VideoView and then in Ontouch callback check if the video is playing or paused before pausing or playing it.
private boolean stopped = false;
private ImageView postVideoPlaypauseIcon;
//set it to any play/pause icon
postVideoPlaypauseIcon = mView.findViewById(R.id.playpause_icon);
private int stopPosition;
Add above code.
then, add CustomVideoView class, make VideoView extend it and create an object video and use findviewbyid
public class CustomVideoView extends VideoView {
private PlayPauseListener mListener;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
#Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}
#Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}
public static interface PlayPauseListener {
void onPlay();
void onPause();
}
}
replace xml for VideoView with below code
`<package-name.CustomVideoView
android:id="#+id/custom_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>`
Finally,
add setOnTouchListener for listening to touch.
video.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(stopped == false){
stopPosition = postVideo.getCurrentPosition();
video.pause();
stopped = true;
} else if(stopped == true){
video.seekTo(stopPosition);
video.start();
stopped = false;
}
Log.e("TAP","from video ");
return false;
}
});
add setPlayPauseListener on your videoView object
video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
#Override
public void onPlay() {
System.out.println("Play!");
videoPlaypauseIcon.setVisibility(View.VISIBLE);
}
#Override
public void onPause() {
System.out.println("Pause!");
videoPlaypauseIcon.setVisibility(View.INVISIBLE);
}
});
used this for reference

Android MediaPlayer Streaming Error: 100: MEDIA_ERROR_SERVER_DIED

I've developed an app which takes an advantage of the native Android's MediaPlayer. The source code of my class making use of Media Player is below.
The problem is that only on some devices after some miliseconds of playback (I hear only voice, the screen remains black) I keep getting error(100,0) which according to the documentation says
public static final int MEDIA_ERROR_SERVER_DIED
Media server died. In this case, the application must release the MediaPlayer object and instantiate a new one.
On forums I've found out that I need to reset the player every time I get it... but I get it after just a short moment and then it dies forever. I cannot reset the player every second since playback is useless. I cannot get why some devices have this problem and others not. The one that I know has Android OS > 4.0.
Of course, first init() and then showVideo() are getting called. The last onError with code 100 is then called. What's a potential solution to make the streams run continuously and not break?
public class NativePlayer extends Player implements OnBufferingUpdateListener,
OnCompletionListener, OnErrorListener, OnInfoListener {
private VideoView videoview;
private PlayerListener listener;
private MainActivity context;
private final Logger logger = LoggerFactory.getLogger(NativePlayer.class);
#Override
public void init(MainActivity activity) {
this.videoview = (VideoView) activity.findViewById(R.id.video);
context = activity;
}
#Override
public void showVideo(final String url, final PlayerListener _listener) {
listener = _listener;
videoview.setVisibility(View.VISIBLE);
try {
Uri video = Uri.parse(url);
videoview.setVideoURI(video);
} catch (Exception e) {
logger.error("Error playing video", e);
listener.onVideoError();
return;
}
videoview.setOnCompletionListener(this);
videoview.setOnErrorListener(this);
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoview.start();
if (listener != null) {
listener.onVideoStarted();
}
}
});
}
#Override
public void onStop() {
stop();
}
private void stop() {
if (videoview == null) {
return;
}
if (videoview.isPlaying()) {
videoview.stopPlayback();
}
}
#Override
public void onDestroy() {
}
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if (listener != null) {
listener.onVideoError();
}
return false;
}
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (listener != null) {
listener.onInfo(what, extra);
}
return false;
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (listener != null) {
listener.onBufferingUpdate(percent);
}
}
}
I had same problem (error 100, mediaplayer died, etc.).
I resolve it by using .stopPlayback(), and starting stream again.
Below is my part of code:
private void startWatchVideo(final string video_link) {
videoViewVA.setMediaController(new MediaController(this));
videoViewVA.setVideoURI(Uri.parse(video_link));
videoViewVA.requestFocus();
videoViewVA.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer media) {
media.start();
}
});
videoViewVA.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer media, int what, int extra) {
if (what == 100)
{
videoViewVA.stopPlayback();
startWatchVideo(video_link);
}
return true;
}
});
}
On practice it looks like video is just slows down

Live audio stuttering in mediaplayer()?

I am new to android. I am making a live audio streaming app(streams from a URL). As soon as I hit play, the audio plays but there is a stuttering about every half second. I checked the live audio URL on my computer and it plays perfectly. And I checked it in another audio streaming app I am making, using the same emulator, and it plays perfectly there too. The streaming file is .mp3. I have added the code.
public class StreamAudioFromUrlSampleActivity extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private Button btn_play,
btn_pause,
btn_stop;
//private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengt
hOfAudio;
private final String URL = "**some url**";
//private final Handler handler = new Handler();
/*private final Runnable r = new Runnable() {
#Override
public void run() {
//updateSeekProgress();
}
};*/
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();mediaPlayer = new MediaPlayer();
}
private void init() {
btn_play = (Button)findViewById(R.id.btn_play);
btn_play.setOnClickListener(this);
btn_pause = (Button)findViewById(R.id.btn_pause);
btn_pause.setOnClickListener(this);
btn_pause.setEnabled(false);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(false);
//seekBar = (SeekBar)findViewById(R.id.seekBar);
//seekBar.setOnTouchListener(this);
//mediaPlayer.setOnBufferingUpdateListener(this);
//mediaPlayer.setOnCompletionListener(this);
}
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
//seekBar.setSecondaryProgress(percent);
}
#Override
public void onCompletion(MediaPlayer mp) {
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
//SeekBar tmpSeekBar = (SeekBar)v;
//mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
}
return false;
}
#Override
public void onClick(View view) {
try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
//lengthOfAudio = mediaPlayer.getDuration();
} catch (Exception e) {
Log.e("Error", "error"+e);
}
switch (view.getId()) {
case R.id.btn_play:
playAudio();
break;
case R.id.btn_pause:
pauseAudio();
break;
case R.id.btn_stop:
stopAudio();
break;
default:
break;
}
//updateSeekProgress();
}
private void updateSeekProgress() {
if (mediaPlayer.isPlaying()) {
//seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
// handler.postDelayed(r, 1000);
}
}
private void stopAudio() {
mediaPlayer.stop();
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
mediaPlayer.release();
//seekBar.setProgress(0);
}
private void pauseAudio() {
mediaPlayer.pause();
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
}
private void playAudio() {try{
Thread.sleep(10000);}catch(InterruptedException x){}
mediaPlayer.start();
btn_play.setEnabled(false);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}
}

Android music skips in sleep mode when using bluetooth

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.

Categories

Resources