2

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

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

2.如果我在服务类中初始化媒体播放器对象,并且用户在活动中进行更多操作,它的节目就是我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;
        }
    }

服务.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");

    }

}

我找到了一些关于同一问题的链接,但我不知道如何解决它。

4

1 回答 1

0

好吧,我看到了你的这部分代码,但不清楚你想要实现什么。您正试图让玩家转到其当前位置,但它已经在该位置?你可以删除那个部分。

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

此外,在您的 onpause 中,您正在释放播放器,但不会在您的 onresume 上再次生成它。您可以使用您提供的链接中的代码在此处重新创建它:

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

        }

        super.onPause();
    }

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

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

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

于 2014-05-08T11:09:39.953 回答