Frage

I'm trying to stream 2-3 second .ogg files in a PageViewer, I need the ability to buffer 3-4 .ogg files so that it will be instant when they click preview.

Example: Gridview of thumbnails is loaded User clicks on any thumbnail Activity loads into a ViewPager ViewPager displays an Image plus a play button Clicking Play the MediaPlayer will load the audio from a URL When I click play it has a 1-4 second delay until playback starts I swipe to the next page and tap the play button with another 1-4 second delay

Is it possible to pre-load/buffer the next 2-3 ViewPager tabs so I can have instant playback?

These files are small, 25-100kb.

public void playmusic() {

    try {

        this.mediaPlayer = new MediaPlayer();
        if (mediaPlayer.isPlaying() || mediaPlayer != null) {
            this.stopAudio();
            this.mediaPlayer.stop();
        }
        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        this.mediaPlayer.setDataSource(arrayURLAudio[getPos()]);
        this.mediaPlayer.prepare();
        this.mediaPlayer.start();
    } catch (IOException ioe) {

    }catch (IllegalThreadStateException itse){

    }catch (Exception e){

    }

Also

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int i) {
            if (mediaPlayer != null) {
                stopAudio();
            }
            textView.setText(arrayURLAnimalName[i]);
            setPos(i);

        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

//UPDATE:

I've tried to create a MediaPlayer[] Array but this is coming up as null.

Instance Variable: MediaPlayer[] arrayMediaPlayer;

onCreate:

    arrayMediaPlayer = new MediaPlayer[arrayURLAudio.length];

    for (int i = 0; i < arrayURLAudio.length; i++) {
        try {
              arrayMediaPlayer[i].setDataSource(arrayURLAudio[i]);
        } catch (IOException ioe) {
        } catch (NullPointerException npe) {
        };

If I try to print out my arrayMediaPlayer[i], its coming up as null.

War es hilfreich?

Lösung

You are only creating one MediaPlayer in your first code snippet. Then you create an empty array of type MediaPlayer and try to access it before adding players. So for it to work you need to set arrayMediaPlayer[i] with a new MediaPlayer before setting the datasource.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top