0

I am trying to restore audio to the position it was and the file it was when the user left the fragment. To do this I save the location of the audio file, and the seek position using personal prefs, along with a boolean for whether or not the audio was playing when the user left. I save this info first thing in onPause().

When I resume, I initialize the views etc. and the very last thing I do in onResume is read from personal prefs and play the audio stored there is appropriate.

However when I try to play from onResume, the media completion listener gets called immediately and the file gets skipped.

I have been running tests and I know that the media player is handed the right data, is prepared correctly and set to play.

The way I am trying to play the audio is the same way I do it if a user clicks manually to play audio, and that works flawlessly.

Only when trying to 'restore' the audio to where it was when a user left does the completion listener get called immediately.

Has anyone seen this before?

    public void setAudioURLAndPLay(Context context, String url)
    {
        Log.d(TAG, "setAudioURLAndPLay");
        CacheQueue.getInstance().addImmediateTaskToQueue(CacheQueue.AUDIO_TASK, context, url, 0, handler);

    }

    private void playCahcedFile(String location)
    {
        Log.d(TAG, "playCahcedFile");
        try
        {
            this.reset();
            this.setAudioStreamType(AudioManager.STREAM_MUSIC);
            this.setDataSource(location);
            this.setOnPreparedListener(new OnPreparedListener()
            {

                @Override
                public void onPrepared(MediaPlayer mp)
                {
                    setPlay();
                }
            });
            this.prepareAsync();
        }
        catch (Exception e)
        {
            Log.d(TAG, "Exception", e);
        }
    }

    public void setPlay()
    {
        Log.d(TAG, "setPlay");
        this.start();
        this.setProgressHandler(this.listener);
    }

and where the calls are being made

    public void initializeFromResume()
    {
        PersonalPrefs prefs = new PersonalPrefs(getActivity());
        if (!prefs.isPLaying())
        {
            return;
        }
        else
        {
            playNewAudio(prefs.getURL());
            // ((ActivityMain) getActivity()).getMediaManager().setSeek(prefs.getSeek());
        }
    }

private void playNewAudio(String url)
{
        getMediaManager().setAudioURLAndPLay(getActivity(), url)
    mediaState = MediaState.playing;
    initializeSeekBar();
    getMediaManager().setOnCompletionListener(this);
    mediaController.togglePlayButton(mediaState);

}
4

1 回答 1

0

我想通了,并将答案发布给将来遇到类似问题的任何人。只需要延迟运行一个帖子。并不完全令人惊讶,但它确实有效。

        h.postDelayed(new Runnable()
        {

            @Override
            public void run()
            {
                PersonalPrefs prefs = new PersonalPrefs(getActivity());
                playNewAudio(prefs.getURL());
            }
        }, 1000);
于 2013-06-05T17:59:46.777 回答