1. Are you ready for the Galaxy S20? Here is everything we know so far!

ANDROID LISTENER!!!

Discussion in 'Android Development' started by S12, Mar 13, 2016.

  1. S12

    S12 Newbie
    Thread Starter

    I need help creating a listener to show a ad after a button click that plays a sound.

    My listener to play the sound http://prntscr.com/a90ojo
     


  2. Deleted User

    Deleted User Guest

    Hi. What do you need help with?
     
  3. S12

    S12 Newbie
    Thread Starter

    I need to create a listener to show an ad after a sound is finish playing. I have how I am currently playing the sound in the screenshot above.
     
  4. Deleted User

    Deleted User Guest

    You can set a listener on the media player like this

    Code (Text):
    1.  
    2. m_context.setOnCompletionListener(new OnCompletionListener(){
    3.  
    4.                 @Override
    5.                 public void onCompletion(MediaPlayer mp){
    6.                     mp.reset();
    7.                     mp.release();
    8.                     mp=null;
    9.  
    10.                    // Show your ad here
    11.  
    12.                 }
    13.             });
    14.  
     
  5. S12

    S12 Newbie
    Thread Starter

    ty will try.
     
  6. S12

    S12 Newbie
    Thread Starter

    I'm getting this error http://prntscr.com/afduke. Also my class that i'm trying to define it in.

    Code (Text):
    1. import android.content.Context;
    2. import android.util.Log;
    3. import android.view.LayoutInflater;
    4. import android.view.View;
    5. import android.view.View.OnClickListener;
    6. import android.view.ViewGroup;
    7. import android.widget.BaseAdapter;
    8. import android.widget.ImageButton;
    9. import android.widget.TextView;
    10.  
    11. import com.startapp.android.publish.StartAppAd;
    12.  
    13.  
    14. public class ClipAdapter extends BaseAdapter {
    15.     private StartAppAd startAppAd;
    16.     private GridActivity m_context;
    17.     private Soundboard m_soundboard;
    18.     private static LayoutInflater inflater=null;
    19.  
    20.  
    21.     public ClipAdapter(GridActivity c, Soundboard s) {
    22.         m_context = c;
    23.         startAppAd = new StartAppAd(m_context);
    24.         m_soundboard = s;
    25.         inflater = ( LayoutInflater )m_context.
    26.                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    27.  
    28.  
    29.     }
    30.  
    31.     @Override
    32.     public int getCount() {
    33.         // TODO Auto-generated method stub
    34.         return m_soundboard.getSize();
    35.     }
    36.  
    37.     @Override
    38.     public Object getItem(int position) {
    39.         // TODO Auto-generated method stub
    40.         return m_soundboard.get(position);
    41.     }
    42.  
    43.     @Override
    44.     public long getItemId(int position) {
    45.         // TODO Auto-generated method stub
    46.         return 0;
    47.     }
    48.  
    49.  
    50.     @Override
    51.     public View getView(int position, View convertView, ViewGroup parent) {
    52.         // TODO Auto-generated method stub
    53.         try {
    54.             final Clip c = m_soundboard.get(position);
    55.             View rowView;
    56.          
    57.             if (c != null) {
    58.                 rowView = inflater.inflate(R.layout.grid_item_layout, null);
    59.                 TextView tv =(TextView) rowView.findViewById(R.id.textView);
    60.                 ImageButton button=(ImageButton) rowView.findViewById(R.id.imageButton);
    61.                 //Button button = new Button(m_context);
    62.                 //button.setHeight(24);
    63.                 tv.setText(c.getTitle());
    64.                 m_context.registerForContextMenu(button);
    65.                 button.setTag(c);
    66.                 //button.getBackground().setColorFilter(new LightingColorFilter(0xffFFFFFF, 0xffFFFFFF));
    67.                 //button.setPadding(0,0,0,0);
    68.                 button.setOnClickListener(new OnClickListener() {
    69.                     @Override
    70.                     public void onClick(View v) {
    71.                         // TODO Auto-generated method stub
    72.                         m_context.play(c.getResourceId());
    73.                         startAppAd.showAd(); // show the ad
    74.                         startAppAd.loadAd(); // load the next ad
    75.                     }
    76.                 });
    77.                 //return button;
    78.                 return rowView;
    79.             }
    80.         }
    81.         catch (IndexOutOfBoundsException e) {
    82.             Log.e(getClass().getCanonicalName(), "No audio at position "
    83.                     + position);
    84.         }
    85.         return null;
    86.     }
    87. }
     
  7. Deleted User

    Deleted User Guest

    setOnCompletionListener is a method of class MediaPlayer. The variable m_context is not a MediaPlayer.
    You need to pass your MediaPlayer object into the ClickAdapter class as a constructor parameter.
     
  8. S12

    S12 Newbie
    Thread Starter

Loading...
Similar Threads - ANDROID LISTENER
  1. RLScott
    Replies:
    0
    Views:
    0
  2. AmgedAlam
    Replies:
    0
    Views:
    22
  3. Daniel1996
    Replies:
    0
    Views:
    100
  4. georgewah
    Replies:
    3
    Views:
    179
  5. Ghost_Dev
    Replies:
    1
    Views:
    163
  6. hk1337
    Replies:
    0
    Views:
    167
  7. Rascalsailor
    Replies:
    0
    Views:
    158
  8. seeknp
    Replies:
    3
    Views:
    226
  9. YanPatel
    Replies:
    2
    Views:
    465

Share This Page

Loading...