简体   繁体   English

Android 广播流媒体 - 活动恢复 - 再次播放

[英]Android radio streaming - on activity resume - plays again

I'm building a Android app that streams simple radio station.我正在构建一个流简单广播电台的 Android 应用程序。

EDIT: I added the OnCreate event and the init() function.编辑:我添加了 OnCreate 事件和 init() 函数。

on create:创建时:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        play_or_pause();

    }

the init() function: init() 函数:

    public void init() {
            mediaPlayer = new MediaPlayer();
            stream_url = "http://the_radio_station.m3u8";
            playing_now = false;
            fb_error = "";
        }

on created I'm calling the AsyncTask method:在创建时,我正在调用 AsyncTask 方法:

play_or_pause();

the AsyncTask method: AsyncTask 方法:

public class play_or_pause_AsyncTask extends
            AsyncTask<Boolean, Integer, String> {

        @Override
        protected String doInBackground(Boolean... params) {
            // TODO Auto-generated method stub

            if (!params[0]) {
                fb_error = "";
                try {
                    if (mediaPlayer.isLooping() || mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                    }
                    mediaPlayer.reset();
                    fb_error = String.valueOf(mediaPlayer.isPlaying());
                    mediaPlayer.setDataSource(stream_url);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    playing_now = true;
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    fb_error += e.toString();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    fb_error += e.toString();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    fb_error += e.getMessage();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    fb_error += e.toString();
                }
            } else {
                mediaPlayer.stop();
                playing_now = false;
            }
            return fb_error;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
//           Toast.makeText(MainActivity.this, fb_error, Toast.LENGTH_LONG).show();
        }
    }

when I'm opening another Activity and coming back to the MainActivity(which making the stream) its starting to run the streaming again without stooping the old one.当我打开另一个 Activity 并返回 MainActivity(它制作流)时,它开始再次运行流而不弯曲旧的。

I reed many posts here, the only suggestion was to use:我在这里发了很多帖子,唯一的建议是使用:

if ( mediaPlayer.isPlaying()) mediaPlayer.stop();

but it ain't helping (someone wrote that the mediaPlayer object is strange, you cant tell what is the objecct real status - Like if the .isPlaying() is true / false).但这无济于事(有人写道 mediaPlayer 对象很奇怪,您无法分辨出对象的真实状态-例如 .isPlaying() 是否为真/假)。

so what I can do in this situation?那么在这种情况下我能做些什么呢?

try this way i use everytime试试这种我每次都用的方法

@Override
public void onStop() {
    super.onStop();

    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM