媒体播放器的java.lang.IllegalStateException

时间:2022-12-05 20:32:26

Hi i am building online radio streaming app. i am getting java.lang.IllegalStateException error when i am pressing play button. Main activity looks like following .

嗨,我正在建立在线广播流应用程序。我按下播放按钮时收到java.lang.IllegalStateException错误。主要活动如下。

package com.radioawaz.simerpreetjassal.radioawaz;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import java.io.IOException;

public class MainActivity extends Activity implements OnClickListener {

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeUIElements();

        initializeMediaPlayer();
    }

    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);

    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}

Erorr Log cat

Erorr Log cat

09-29 20:51:51.278 7852-7852/com.radioawaz.simerpreetjassal.radioawaz E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.radioawaz.simerpreetjassal.radioawaz, PID: 7852
                                                                                        java.lang.IllegalStateException
                                                                                            at android.media.MediaPlayer.prepareAsync(Native Method)
                                                                                            at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.startPlaying(MainActivity.java:67)
                                                                                            at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.onClick(MainActivity.java:55)
                                                                                            at android.view.View.performClick(View.java:5198)
                                                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-29 20:51:52.978 7852-7852/com.radioawaz.simerpreetjassal.radioawaz I/Process: Sending signal. PID: 7852 SIG: 9

Hi i am building online radio streaming app. i am getting java.lang.IllegalStateException error when i am pressing play button. Main activity looks like following .

嗨,我正在建立在线广播流应用程序。我按下播放按钮时收到java.lang.IllegalStateException错误。主要活动如下。

3 个解决方案

#1


0  

Please add <uses-permission android:name="android.permission.INTERNET"/> in your AndroidManifest.xml file.

请在AndroidManifest.xml文件中添加

#2


0  

Looking at the documentation there are two possible problems:

查看文档有两个可能的问题:

  1. The MediaPlayer requires the INTERNET permission if used with a networked stream (https://developer.android.com/reference/android/media/MediaPlayer.html#Permissions), but the behaviour should that not be the case is not documented.
  2. 如果与网络流(https://developer.android.com/reference/android/media/MediaPlayer.html#Permissions)一起使用,MediaPlayer需要INTERNET权限,但不应记录的情况应该是不存在的情况。

  3. The code in theory should work, however an error would put the MediaPlayer in the error state, which would make it throw an IllegalStateException. Add an ErrorListener through https://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener) before doing any actions on the MediaPlayer object.
  4. 理论上的代码应该可以工作,但是错误会使MediaPlayer处于错误状态,这会导致抛出IllegalStateException。在对MediaPlayer对象执行任何操作之前,通过https://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)添加ErrorListener。

Important section from the documentation:

文档中的重要部分:

In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances. Sometimes, due to programming errors, invoking a playback control operation in an invalid state may also occur. Under all these error conditions, the internal player engine invokes a user supplied OnErrorListener.onError() method if an OnErrorListener has been registered beforehand via setOnErrorListener(android.media.MediaPlayer.OnErrorListener).

通常,一些回放控制操作可能由于各种原因而失败,例如不支持的音频/视频格式,交错的音频/视频,分辨率太高,流超时等。因此,在这些情况下,错误报告和恢复是一个重要的问题。有时,由于编程错误,也可能发生在无效状态下调用重放控制操作。在所有这些错误条件下,如果事先通过setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注册了OnErrorListener,则内部播放器引擎会调用用户提供的OnErrorListener.onError()方法。

#3


0  

delete your player.prepareAsync(), and then add player.prepare() in player.prepareAsync(),like this: hope can help you

删除你的player.prepareAsync(),然后在player.prepareAsync()中添加player.prepare(),这样:希望可以帮到你

 private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.prepare();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

#1


0  

Please add <uses-permission android:name="android.permission.INTERNET"/> in your AndroidManifest.xml file.

请在AndroidManifest.xml文件中添加

#2


0  

Looking at the documentation there are two possible problems:

查看文档有两个可能的问题:

  1. The MediaPlayer requires the INTERNET permission if used with a networked stream (https://developer.android.com/reference/android/media/MediaPlayer.html#Permissions), but the behaviour should that not be the case is not documented.
  2. 如果与网络流(https://developer.android.com/reference/android/media/MediaPlayer.html#Permissions)一起使用,MediaPlayer需要INTERNET权限,但不应记录的情况应该是不存在的情况。

  3. The code in theory should work, however an error would put the MediaPlayer in the error state, which would make it throw an IllegalStateException. Add an ErrorListener through https://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener) before doing any actions on the MediaPlayer object.
  4. 理论上的代码应该可以工作,但是错误会使MediaPlayer处于错误状态,这会导致抛出IllegalStateException。在对MediaPlayer对象执行任何操作之前,通过https://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)添加ErrorListener。

Important section from the documentation:

文档中的重要部分:

In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances. Sometimes, due to programming errors, invoking a playback control operation in an invalid state may also occur. Under all these error conditions, the internal player engine invokes a user supplied OnErrorListener.onError() method if an OnErrorListener has been registered beforehand via setOnErrorListener(android.media.MediaPlayer.OnErrorListener).

通常,一些回放控制操作可能由于各种原因而失败,例如不支持的音频/视频格式,交错的音频/视频,分辨率太高,流超时等。因此,在这些情况下,错误报告和恢复是一个重要的问题。有时,由于编程错误,也可能发生在无效状态下调用重放控制操作。在所有这些错误条件下,如果事先通过setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注册了OnErrorListener,则内部播放器引擎会调用用户提供的OnErrorListener.onError()方法。

#3


0  

delete your player.prepareAsync(), and then add player.prepare() in player.prepareAsync(),like this: hope can help you

删除你的player.prepareAsync(),然后在player.prepareAsync()中添加player.prepare(),这样:希望可以帮到你

 private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.prepare();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}