如何停止流MP3文件 [英] How to stop a streaming mp3-file

查看:114
本文介绍了如何停止流MP3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须构建使用的mp3文件的应用程序。在第一个版本的文件被列入APK文件。我现在正在努力在具有在URL的资源的mp3文件的版本。
我有调整code,几乎一切工作正常。
问题是,对于停止的按钮,暂停和播放,现在在工作。
这是code,它开始流(正常工作):

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.

推荐答案

我创造了非常类似的应用程序,我的code是这样的:

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

当我preSS后退按钮:

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

这篇关于如何停止流MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆