如何在android中停止并启动在线流

问题描述 投票:0回答:1

我正在构建一个Android应用程序,应该从URL流式传输在线广播电台。

我目前正在进行演示,我从在线教程中获得了很多帮助,但我遇到了问题。

当我第二次点击我的播放按钮时,我希望它停止,当我再次按下播放按钮时,从当前电台开始,但是它暂停并从我停止的地方继续,而不是电台当前正在播放的内容。

虽然我知道我正在使用mediaPlayer.pause()但是当我使用mediaPlayer.stop()时它似乎没有重新开始。

主要信息

package com.radline.test.radlinetest;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {


    Button b_play;
    boolean prepared = false;
    boolean started = false;
    MediaPlayer mediaPlayer;

   String stream = "http://stream.radioreklama.bg:80/radio1rock128";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b_play = (Button)findViewById(R.id.b_play);
        b_play.setEnabled(false);
        b_play.setText("LOADING.");

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        new PlayerTask().execute(stream);

        b_play.setOnClickListener(new View.OnClickListener(){
            @Override
                    public void onClick(View view) {
                if (started) {
                    started = false;
                    mediaPlayer.pause();
                    b_play.setText("PLAY");
                }
                else{
                    started = true;
                    mediaPlayer.start();
                    b_play.setText("PAUSE");
                }


            }
        });

    }

    class PlayerTask extends AsyncTask<String, Void, Boolean>{
        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
            } catch (IOException e) {
                e.printStackTrace();
            }

            return prepared;

        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            b_play.setEnabled(true);
            b_play.setText("PLAY");
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (started){
            mediaPlayer.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (started){
            mediaPlayer.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(prepared){
            mediaPlayer.release();
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.radline.test.radlinetest.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="233dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="0.468"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="36dp"
        android:layout_marginEnd="36dp"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="360dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="50dp"
        app:layout_constraintVertical_bias="0.505">

        <Button
            android:id="@+id/b_previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="PREVIOUS" />

        <Button
            android:id="@+id/b_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="PLAY" />

        <Button
            android:id="@+id/b_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="STOP" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VOICE OF UCHE"
        android:textColor="#000000"
        android:textSize="30sp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="32dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

有人可以帮忙吗?

android streaming android-mediaplayer online-radio
1个回答
0
投票

documentation的图表中可以看出,在prepare()之后调用prepareAsync()之前,你必须使用start()stop()。因此,在调用stop()之前,请使用start()并再次准备播放器。然后它应该按照需要工作。

© www.soinside.com 2019 - 2024. All rights reserved.