I have build an app that use mp3-files. In the first version the files was included in the apk-file. I now are working on a version that have the mp3-files in an URL-resource. I have adjust the code and almost everything works fine. The problem is that the buttons for stop, pause and play now not is working. This is the code that start the streaming (works fine):
private void playAudio(String media) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(media);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
}
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
finish();
}
});
}
And this is the functions I use for stopping etc. Worked fine with local file (raw-resources) But now it is not working:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent(SpelaSaga.this, VisaIngressSaga.class);
Bundle bundle = new Bundle();
bundle.putString("Titel", titel);
bundle.putInt("Position", position);
intent.putExtras(bundle);
SpelaSaga.this.startActivity(intent);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
private OnClickListener button_1Listener = new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
Intent intent = new Intent(SpelaSaga.this, VisaIngressSaga.class);
Bundle bundle = new Bundle();
bundle.putString("Titel", titel);
bundle.putInt("Position", position);
intent.putExtras(bundle);
SpelaSaga.this.startActivity(intent);
finish();
}
};
private OnClickListener button_2Listener = new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
};
I have looked around and have only find old versions of streaming tutorials that needs much more coding. Hope someone out there have a good idea of how to control the streaming.
I created very similar app, my code looks like this:
when I press back button:
i hope, that this answer help You ;)