开发者

MediaPlayer.OnCompletionListener and View.OnClickListener?

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.setOnClickList开发者_Go百科ener(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

    }
    }


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


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


try this! it works to me, i hope it helps you:
main:

package com.hairdryer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.hairdryer.ServiceReproductor;

public class Reproductor extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton btnInicio = (ImageButton) findViewById(R.id.on);
        ImageButton btnFin = (ImageButton) findViewById(R.id.off);
        btnInicio.setOnClickListener(this);
        btnFin.setOnClickListener(this);
    }

    public void onClick(View src) {
        ImageButton btnInicio = (ImageButton) findViewById(R.id.on);

        switch (src.getId()) {
        case R.id.on:
            btnInicio.setBackgroundResource(R.drawable.on2);
            startService(new Intent(this, ServiceReproductor.class));
            break;
        case R.id.off:
            stopService(new Intent(this, ServiceReproductor.class));
            btnInicio.setBackgroundResource(R.drawable.on);
            break;
        }
    }
}

ServiceReproductor.java:

package com.hairdryer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.widget.Toast;


public class ServiceReproductor extends Service implements OnCompletionListener{
    private MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        //Toast.makeText(this, "Servicio Creado", Toast.LENGTH_LONG).show();    
        player = MediaPlayer.create(this, R.raw.inicio);
        player.setOnCompletionListener(this);

    }

    @Override
    public void onDestroy() {
        //Toast.makeText(this, "Servicio Detenido", Toast.LENGTH_LONG).show();
        player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        //Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_LONG).show();
        player.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        player = MediaPlayer.create(this, R.raw.secador);
        player.start();
        player.setLooping(true);
        // TODO Auto-generated method stub

    }   

}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/secador"
        />

    <ImageButton    android:id="@+id/off"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/off"

        />
    <ImageButton    android:id="@+id/on"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/off"
        android:layout_marginRight="10dp"
        android:background="@drawable/on"

        />

</RelativeLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜