在Media Player中出错:SeekTo处于错误状态:&开始在状态0中调用

时间:2013-01-19 07:31:55

标签: android media-player

当我用作服务时,我在媒体播放器中面临一些问题。

1。如果我将Activity中的Media Player对象初始化为静态并在服务类中进一步使用,那么它会显示error (-38, 0)Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0start called in state 0

2. 如果我初始化服务类中的Media Player对象和活动中的用户更多 行动它的节目是我Null Pointer

public static MediaPlayer mPlayer = null;

播放点击活动

    Audio_Player_Play.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub

            if (mPlayer == null)
            {

                Intent Play_Intent = new Intent(RDS_Singel_Audio.this,
                        Audio_Player.class);

                startService(Play_Intent);
                Audio_Player_Play.setVisibility(View.GONE);
                Audio_Player_Pause.setVisibility(View.VISIBLE);

            } else
            {
                // Play_Streaming();
                mPlayer.seekTo(mPlayer.getCurrentPosition());
                mPlayer.start();
                Audio_Player_Play.setVisibility(View.GONE);
                Audio_Player_Pause.setVisibility(View.VISIBLE);
            }
        }
    });

暂停点击活动

Audio_Player_Pause.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                // Play_Streaming();
                // checking is medai player running

                Audio_Player_Play.setVisibility(View.VISIBLE);
                Audio_Player_Pause.setVisibility(View.GONE);

                // checking is audio playing in background
                if (mPlayer != null && mPlayer.isPlaying())
                {
                    mPlayer.pause();
                    /*
                     * Intent Pause_Intent = new Intent(RDS_Singel_Audio.this,
                     * Audio_Player.class); stopService(Pause_Intent);
                     */
                    Audio_Player_Play.setVisibility(View.VISIBLE);
                    Audio_Player_Pause.setVisibility(View.GONE);
                }
            }
        });

OnResume() - OnPause() - OnDistroy()

@Override
    protected void onResume()
    {
        super.onResume();
    }
@Override
    protected void onPause()
    {
        // cleanUp();
        if (mPlayer != null && mPlayer.isPlaying())
        {
            mPlayer.release();
            mPlayer = null;

        }

        super.onPause();
    }
@Override
    protected void onDestroy()
    {
        cleanUp();
        super.onDestroy();
    }

private void cleanUp()
    {
        if (mPlayer != null)
        {
            mVisualizerView.release();
            mPlayer.release();
            mPlayer = null;
        }
    }

Service.Java

public class Audio_Player extends Service
{
    private static final String TAG = "MyService";

    // public static MediaPlayer mPlayer;
    Function f;
    RDS_Singel_Audio sa;
    MediaPlayer mPlayer;

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

    @Override
    public void onCreate()
    {
        try
        {
            sa = new RDS_Singel_Audio();
            Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG)
                    .show();

            sa.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            sa.mPlayer.setDataSource(sa.Audio_URL);
            sa.mPlayer.prepare();// might take long! (for buffering, etc)
            sa.mVisualizerView.link(sa.mPlayer);
            sa.mPlayer.start();
            sa.addLineRenderer();
            sa.mPlayer.setLooping(false); // Set looping

            // sa.mPlayer.start();

        } catch (Exception e)
        {
            // TODO: handle exception
            Log.d(TAG, "" + e);
        }

    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        if (sa.mPlayer != null)
        {
            sa.mPlayer.stop();
            sa.mPlayer.release();

        }
        sa.mPlayer = null;

    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");

        // mPlayer.setDataSource("http://clients.ncrypted.net/riddimapp/images/audio/4/df9919ccb2880933c795f43d735cf9bc.mp3");

    }

}

我发现了一些关于同一问题的link,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

好吧,我看到了你的代码的这一部分,但目前还不清楚你想要实现的目标。您正试图让玩家进入当前位置,但它已经处于该位置?你可以删除那部分。

// Play_Streaming();
mPlayer.seekTo(mPlayer.getCurrentPosition());

另外,在您的暂停期间,您将释放播放器,但不会在您的onresume上再次生成播放器。您可以使用您提供的链接中的代码重新创建它:

@Override
    protected void onResume()
    {
        super.onResume();
    }
@Override
    protected void onPause()
    {
        // cleanUp();
        if (mPlayer != null && mPlayer.isPlaying())
        {
            mPlayer.release();
            mPlayer = null;

        }

        super.onPause();
    }

此外,由于您似乎将媒体播放器的源设置为网址,因此您应该在以下部分中更改prepareAsync的准备工作,以便缓冲:

sa.mPlayer.setDataSource(sa.Audio_URL);
sa.mPlayer.prepare();// might take long! (for buffering, etc)

您提供的链接已经有很多建议可以克服上述问题。 请尝试该链接上的代码,尤其是那些建议使用prepareAsync和onprepared监听器的代码。如果不起作用,你可以回到这里。