Good day! Application - flipping pictures (ViewPager) with voice acting (MediaPlayer) for each picture.
The problem is when the main activation calls the child with the first picture, there is no music, when flipping back and forth all the pictures are voiced, including the first one. Only at the first call the first picture is not voiced.
The intention is to “voice the first page at last”.
I shoveled the Internet, I did not find anything. Help me please! SOS!
Code:

package com.mycompany.myapplication; import android.media.MediaPlayer; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class FirstActivity extends AppCompatActivity { public static MediaPlayer mp=null; ViewPager viewPager; FirstCustomSwipeAdapter adapter; private int[] mAudio=new int[]{R.raw.sound_11,R.raw.sound_12,R.raw.sound_13}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); viewPager = (ViewPager) findViewById(R.id.view_pager); adapter = new FirstCustomSwipeAdapter((this)); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(mOnPageListener); } public OnPageChangeListener mOnPageListener=new OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { if (mp !=null){ mp.stop(); } mp= MediaPlayer.create(FirstActivity.this,mAudio[position]); mp.start(); } @Override public void onPageScrollStateChanged(int state) {} }; } 
  • mOnPageListener.onPageSelected(0); in onCreate tried? - Yura Ivanov
  • I tried mOnPageListener.onPageSelected(0); in onCreate-worked! With OnLoad I did not try, it can also work. Thanks to all! - Svetlana

1 answer 1

Add an OnLoad handler to activate, and drop the code that will sound the first picture. That is, the idea is to call the voice acting at the moment when the activation is created, registered and drawn to the user, there definitely is a corresponding event for this.

  • Or OnDraw, as it is called in androids. - QuaternioNoir