9

我正在制作一个简单的应用程序来播放在线广播流。我知道如何设置流。到目前为止,这是我的代码。它设置流并播放它。但如果我停止,我无法让它重新开始。我错过了一些明显的东西吗?

final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // might take long! (for buffering, etc)

   streamButton = (ToggleButton) findViewById(R.id.playstopstream);

   streamButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
        // TODO Auto-generated method stub
         new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(isChecked)
                {


                    mediaPlayer.start();

                }
                else
                {
                    mediaPlayer.stop();
                    mediaPlayer.reset();



                }
            }
4

7 回答 7

18

老问题,但我发现它正在寻找这个。只需使用 pause 停止,然后在重新启动之前使用 seekTo(0):

mediaPlayer.seekTo(0);
mediaPlayer.start();

无需每次都重新准备录音机即可工作。

于 2015-11-14T09:11:32.633 回答
9

当你打电话mediaPlayer.reset() mediaPlayer时进入空闲状态。对于播放,您应该在开始之前进行初始化准备。

您可以在此处检查媒体播放器状态:http: //developer.android.com/reference/android/media/MediaPlayer.html

于 2012-09-19T01:51:09.937 回答
7
public void playButton (View view){

        if (isActive == false){
            mediaPlayer.start();
            isActive = true;
        }else{
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }
    }

适合我。

于 2017-07-14T16:42:44.020 回答
1

尝试这个

@Override
public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
    // TODO Auto-generated method stub
     new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(isChecked) {
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
            else {
                mediaPlayer.stop();
                mediaPlayer.reset();
            }
        }
于 2014-02-10T02:19:23.253 回答
0

完成歌曲/播放歌曲后,您必须调用 onDestroy() 方法。您可以从我的实时代码中获得帮助。

public class RingtonePlayingService extends Service {
    public static MediaPlayer mediaPlayer;
    public static Context context;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            mediaPlayer = new MediaPlayer();
            // this is important.
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

            try {
                // Uri ringerUri = RingtoneManager.getActualDefaultRingtoneUri(
                // getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
                mediaPlayer.setDataSource(context,
                        Settings.System.DEFAULT_RINGTONE_URI);
                // mediaPlayer.setDataSource(context, ringerUri);

                mediaPlayer.setLooping(true);
                mediaPlayer.prepare();
                if (SettingsManager.getInstance(context).getFinishCall()) {

                } else {
                    mediaPlayer.start();
                }

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        return START_NOT_STICKY;
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        TTcall.onDestroyr();
        if (mediaPlayer != null) {

            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.stop();
                mediaPlayer = null;
            }

        }
    }

}

我希望你会从上面的代码中得到帮助。请尝试像这样使用。谢谢

于 2014-12-13T07:35:32.227 回答
-2

此外,您可能不需要stop()pause()可能就足够了,这意味着您可以play()在想继续玩时调用。

于 2012-11-08T14:15:46.153 回答
-5
mediaPlay.seek(0);
mediaPlay.pause();
于 2016-04-14T01:46:31.580 回答