How to stop a streaming mp3-file

a 夏天 提交于 2019-12-25 04:19:38

问题


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:


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 ;)



来源:https://stackoverflow.com/questions/8354218/how-to-stop-a-streaming-mp3-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!