How to stop a streaming mp3-file

2019-08-30 05:54发布

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.

1条回答
Animai°情兽
2楼-- · 2019-08-30 06:29

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

public void startSound(final String URL) {
    bt.setText("STOP");
    new Thread() {
        public void run() {
            try {
                Uri uri = Uri.parse(URL);

                mp = MediaPlayer.create(PlaySound.this, uri);
                mp.start();
                mp.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(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 ;)

查看更多
登录 后发表回答