MediaPlayer.OnCompletionListener and View.OnClickListener?

16,661

Solution 1

extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener 

Then you need to register your activity.

mediaPlayer.setOnCompletionListener(this);
someView.setOnClickListener(this);

Where 'this' is the activity you just created

Solution 2

Stick this code right up at the start as part of the onCreate():

MediaPlayer mp = MediaPlayer.create(this, pathToTheFile, web, whereverTheSoundIs);
mp.setOnCompletionListener(this);
mp.start();

If that doesn't work, then you have a problem locating the sound file, or it is in the wrong format.

From experience, cut things down to simple parts and try and get each part working first before moving on.

Another thing I do is put comments after each } for example: '} // End of Case'

Oh, almost forgot, in the onCompletion you might like to close off the media player with

mp.release();

Cheers

Share:
16,661
vamp6x6x6x
Author by

vamp6x6x6x

Updated on June 04, 2022

Comments

  • vamp6x6x6x
    vamp6x6x6x about 2 years

    I am currently trying:

    extends Activity implements MediaPlayer.OnCompletionListener 
    extends Activity implements View.OnClickListener 
    

    at the same time and its not working or rather im not sure how to implement it...how would I go about doing this?

    edit: maybe it will help if I show you guys what I have now and its not working:

    package com.vamp6x6x6x.rusty;
    
    import java.io.IOException;
    
    import com.vamp6x6x6x.rusty.R;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class rustyactivity extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener {
        /** Called when the activity is first created. */
    
        ImageView display;
        int toPhone;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            setContentView(R.layout.main);
    
            display = (ImageView) findViewById(R.id.IVDisplay);
            ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
            ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
            ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
            ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
            ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
             Button setWall = (Button) findViewById(R.id.bSetWallpaper);
    
            toPhone = R.drawable.guy1;
    
            image1.setOnClickListener(this);
            image2.setOnClickListener(this);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
            image5.setOnClickListener(this);
            setWall.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            switch (v.getId()) {
    
            case R.id.IVimage1:
            display.setImageResource(R.drawable.guy1);
            toPhone = R.drawable.guy1;
            break;
            case R.id.IVimage2:
                display.setImageResource(R.drawable.guy2);
                toPhone = R.drawable.guy2;
                break;
            case R.id.IVimage3:
                display.setImageResource(R.drawable.guy3);
                toPhone = R.drawable.guy3;
                break;
            case R.id.IVimage4:
                display.setImageResource(R.drawable.guy4);
                toPhone = R.drawable.guy4;
                break;
            case R.id.IVimage5:
                display.setImageResource(R.drawable.guy5);
                toPhone = R.drawable.guy5;
                break;
    
            case R.id.bSetWallpaper:
    
                Bitmap whatever = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));
                   try{
                       getApplicationContext().setWallpaper(whatever);
                   }catch(IOException e){
                       e.printStackTrace();
                   }
            }
    
            Button ending = (Button) findViewById(R.id.theme);
            ending.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playSound(R.raw.theme);
                }       
     });
        }
            private void playSound(int resId) {
                MediaPlayer mp = MediaPlayer.create(this, resId);
                mp.setOnCompletionListener(this);
                mp.start();
            }
    
        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
    
        }
        }