Skip to content Skip to sidebar Skip to footer

How To Stop A Streaming Mp3-file

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 ha

Solution 1:

I created very similar app, my code looks like this:

publicvoidstartSound(final String URL) {
    bt.setText("STOP");
    newThread() {
        publicvoidrun() {
            try {
                Uri uri = Uri.parse(URL);

                mp = MediaPlayer.create(PlaySound.this, uri);
                mp.start();
                mp.setOnCompletionListener(newOnCompletionListener() {
                    @OverridepublicvoidonCompletion(MediaPlayer arg0) {
                        bt.setEnabled(true);
                        bt.setText("SŁUCHAJ");
                    }
                });
            } catch (Exception e) {

            }
            myProgressDialog.dismiss();
        }
    }.start();
}

when I press back button:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        try {
            mp.stop();
        } catch (Exception e) {

        }
    }
    return super.onKeyDown(keyCode, event);
}

i hope, that this answer help You ;)

Post a Comment for "How To Stop A Streaming Mp3-file"