从原始资源通过MediaPlayer播放媒体时出现问题

时间:2011-02-11 18:43:11

标签: android media-player

我创建了音板和代码编译,它只是不会创建任何声音或播放mp3,我在哪里编码错误,因为它不会从任何地方开始。

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SplashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
    public class Bluarg extends Activity implements OnClickListener{
        MediaPlayer mp1;
        MediaPlayer mp2;
        MediaPlayer mp3;
        MediaPlayer mp4;
        MediaPlayer mp5;
        MediaPlayer mp6;
        MediaPlayer mp7;
        MediaPlayer mp8;
        MediaPlayer mp9;
        MediaPlayer mp10;
        MediaPlayer mp11;
        MediaPlayer mp12;
        MediaPlayer mp13;
        MediaPlayer mp14;
        MediaPlayer mp15;
        MediaPlayer mp16;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mp1 = MediaPlayer.create(this, R.raw.deb1);
            mp2 = MediaPlayer.create(this, R.raw.deb2);
            mp3 = MediaPlayer.create(this, R.raw.deb3);
            mp4 = MediaPlayer.create(this, R.raw.deb4);
            mp5 = MediaPlayer.create(this, R.raw.deb5);
            mp6 = MediaPlayer.create(this, R.raw.deb6);
            mp7 = MediaPlayer.create(this, R.raw.deb7);
            mp8 = MediaPlayer.create(this, R.raw.deb8);
            mp9 = MediaPlayer.create(this, R.raw.deb9);
            mp10 = MediaPlayer.create(this, R.raw.deb10);
            mp11 = MediaPlayer.create(this, R.raw.deb11);
            mp12 = MediaPlayer.create(this, R.raw.deb12);
            mp13 = MediaPlayer.create(this, R.raw.deb13);
            mp14 = MediaPlayer.create(this, R.raw.deb14);
            mp15 = MediaPlayer.create(this, R.raw.deb15);
            mp16 = MediaPlayer.create(this, R.raw.deb16);



            final Button button2 = (Button) findViewById(R.id.Button02);
            button2.setOnClickListener(this);
            final Button button3 = (Button) findViewById(R.id.Button03);
            button3.setOnClickListener(this);
            final Button button4 = (Button) findViewById(R.id.Button04);
            button4.setOnClickListener (this);

            final Button button5 = (Button) findViewById(R.id.Button05);
            button5.setOnClickListener (this);

            final Button button6 = (Button) findViewById(R.id.Button06);
            button6.setOnClickListener (this);

            final Button button7 = (Button) findViewById(R.id.Button07);
            button7.setOnClickListener (this);

            final Button button8 = (Button) findViewById(R.id.Button08);
            button8.setOnClickListener (this);

            final Button button9 = (Button) findViewById(R.id.Button09);
            button9.setOnClickListener (this);

            final Button button10 = (Button) findViewById(R.id.Button10);
            button10.setOnClickListener (this);

            final Button button11 = (Button) findViewById(R.id.Button11);
            button11.setOnClickListener (this);

            final Button button12 = (Button) findViewById(R.id.Button12);
            button12.setOnClickListener (this);

            final Button button13 = (Button) findViewById(R.id.Button13);
            button13.setOnClickListener (this);

            final Button button14 = (Button) findViewById(R.id.Button14);
            button14.setOnClickListener (this);

            final Button button15 = (Button) findViewById(R.id.Button15);
            button15.setOnClickListener (this);

            final Button button16 = (Button) findViewById(R.id.Button16);
            button16.setOnClickListener (this);

            final Button button17 = (Button) findViewById(R.id.Button17);
            button17.setOnClickListener (this);
        }

        @Override
        public void onClick(View v) {
            switch(v.getId()) {
            case R.id.Button02:
                mp1.start();
                break;
            case R.id.Button03:
                mp2.start();
                break;
            case R.id.Button04:
                mp3.start();
                break;
            case R.id.Button05:
                mp4.start();
                break;  
            case R.id.Button06:
                mp5.start();
                break;
            case R.id.Button07:
                mp6.start();
                break;     
            case R.id.Button08:
                mp7.start();
                break;
            case R.id.Button09:
                mp8.start();
                break;     
            case R.id.Button10:
                mp9.start();
                break;
            case R.id.Button11:
                mp10.start();
                break; 
            case R.id.Button12:
                mp11.start();
                break;
            case R.id.Button13:
                mp12.start();
                break;    
            case R.id.Button14:
                mp13.start();
                break;
            case R.id.Button15:
                mp14.start();
                break;    
            case R.id.Button16:
                mp15.start();
                break; 
            case R.id.Button17:
                mp16.start();
                break;    
            }
        }
        @Override
        protected void onDestroy() {
            mp1.release();
            mp2.release();
            mp3.release();
            mp4.release();
            mp5.release();
            mp6.release();
            mp7.release();
            mp8.release();
            mp9.release();
            mp10.release();
            mp11.release();
            mp12.release();
            mp13.release();
            mp14.release();
            mp15.release();
            mp16.release();
            super.onDestroy();
        }
    }

}

1 个答案:

答案 0 :(得分:3)

当您创建太多MediaPlayer对象时,我发现这是一个问题。这里有一个很多的冗余代码。除了使用一些循环来删除90%的代码之外,请尝试使用类似这样的内容来代替您当前拥有的MediaPlayers:

//outside onCreate(), where you have your MPs now
MediaPlayer mp;
Resources resources;

//inside onCreate()
mp = new MediaPlayer();
resources = getResources();

int[] rawIds = {
    R.raw.deb1, R.raw.deb2,
    R.raw.deb3, R.raw.deb4,
    R.raw.deb5, R.raw.deb6,
    R.raw.deb7, R.raw.deb8,
    R.raw.deb9, R.raw.deb10,
    R.raw.deb11, R.raw.deb12,
    R.raw.deb13, R.raw.deb14,
    R.raw.deb15, R.raw.deb16 
};

int[] buttonIds = {
    R.id.Button02, R.id.Button03,
    R.id.Button04, R.id.Button05,
    R.id.Button06, R.id.Button07,
    R.id.Button08, R.id.Button09,
    R.id.Button10, R.id.Button11,
    R.id.Button12, R.id.Button13,
    R.id.Button14, R.id.Button15,
    R.id.Button16, R.id.Button17 
};

//this can replace your whole Button assignment portion
for (int i : buttonIds) {
    Button b = (Button)findViewById(i);
    b.setOnClickListener(this);
}

//in your onClick(). Might be a better way to do this than looping through
//all of the ids, but this should still be quick, and much less code.
for (int i = 0, i < buttonIds.length; i++) {

    //if the button being clicked is e.g. Button02...
    if(v.getId() == buttonIds[i]) {

        //get the file descriptor for your Raw Resource associated with Button02...
        AssetFileDescriptor afd = resources.openRawResourceFd(rawIds[i]);

        //in case it's not in the ready state...
        mp.reset();

        //and set the mediaPlayer's data source, prepare it, and begin
        mp.setDataSource(afd.getDescriptor(), afd.getStartOffset(), afd.getLength());
        mp.prepare();
        mp.start();
        break;
    }
}