如何播放主屏幕和第二屏幕上添加的音乐?

时间:2014-07-19 16:39:23

标签: java android layout screen

我是android世界的新手。我正在开发一个应用程序,主屏幕上有6个按钮,第二个屏幕上有5个按钮。当用户点击这些按钮时,主屏幕上的5个按钮应该能够播放音乐。并且每个按钮应该能够播放与原始文件夹中的音乐相对应的不同音乐。主屏幕上的第6个按钮应允许用户转到下一个屏幕(第二个屏幕)。同样,第二个屏幕上的5个按钮应播放与原始文件夹中的音乐对应的音乐。请查看我的代码..我不知道我的代码有什么问题?

我的主要Java代码如下所示......

public class MainActivity extends Activity implements OnClickListener{  

    public class onClickListener implements OnClickListener {       

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent("com.example.playaudio.SecondActivity");
            startActivity(i);   

        }               
    }

    public class onClick {

    }

    private MediaPlayer mp;
    Button button;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.buttonSwitch);
        button.setOnClickListener(new onClickListener());
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        Button button1=(Button)findViewById(R.id.button_1);
        Button button2=(Button)findViewById(R.id.button_2);
        Button button3=(Button)findViewById(R.id.button_3);
        Button button4=(Button)findViewById(R.id.button_4);
        Button button5=(Button)findViewById(R.id.button_5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);       
    }

    private void setOnClickListener(
            com.example.playaudio.MainActivity.onClick onClick) {
        // TODO Auto-generated method stub      

    }

    public static void onClick(View v) {
        int resId;
        switch (v.getId()) {
        case R.id.button_1:
            resId = R.raw.a;
            break;
        case R.id.button_2:
            resId = R.raw.b;
            break;
        case R.id.button_3: 
            resId = R.raw.c;
            break;
        case R.id.button_4:
            resId = R.raw.d;
            break;
        case R.id.button_5:
            resId = R.raw.e;
            break;      
        default:
            resId = R.raw.a;
            break;
        }

        // Release any resources from previous MediaPlayer
        if (mp != null) {
            mp.release();

        }
        // Create a new MediaPlayer to play this sound
        mp = MediaPlayer.create(this, resId);
        mp.start();
    }

    {
             startActivity(new Intent(MainActivity.this,SecondActivity.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }


    @Override

    protected void onDestroy() {
        if(null!=mp){
            mp.release();
        }
        super.onDestroy();
    }
}

Second Screen Java class

import com.example.playaudio.MainActivity.onClick;

import android.app.Activity;

import android.content.Intent;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class SecondActivity extends Activity

{

@Override

protected void onCreate(Bundle savedInstanceState)

{

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);




setContentView(R.layout.activity_second);




b= (Button) findViewById (R.id.buttonSwitch);




b.setOnClickListener(new onClick());




setVolumeControlStream(AudioManager.STREAM_MUSIC);




Button button7=(Button)findViewById(R.id.button7);




Button button8=(Button)findViewById(R.id.button8);




Button button9=(Button)findViewById(R.id.button9);




Button button10=(Button)findViewById(R.id.button10);




Button button11=(Button)findViewById(R.id.button11);




button7.setOnClickListener((OnClickListener) this);




button8.setOnClickListener((OnClickListener) this);




button9.setOnClickListener((OnClickListener) this);




button10.setOnClickListener((OnClickListener) this);




button11.setOnClickListener((OnClickListener) this);

}

public void onClick(View v) {

int resId;

switch (v.getId()) {




case R.id.button7:




    resId = R.raw.f;




    break;




case R.id.button8:




    resId = R.raw.g;




    break;




case R.id.button9:




    resId = R.raw.h;




    break;




case R.id.button10:




    resId = R.raw.i;




    break;




case R.id.button11:




    resId = R.raw.j;




    break;




default:




    resId = R.raw.a;




    break;




}




 {




     startActivity(new Intent(MainActivity.this,SecondActivity.class));




 }

1 个答案:

答案 0 :(得分:0)

我认为你没有准备好你的媒体播放器。我留给你一个有效的代码。我总是准备同一首歌。请记住通过音乐更改它并创建一个“资产”文件夹,您可以在其中放置“.mp3”文件。

public class MediaPlayerActivity extends ActionBarActivity implements View.OnClickListener {

private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;
private Button mButton6;
private MediaPlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    mButton1 = (Button) findViewById(R.id.button1);
    mButton2 = (Button) findViewById(R.id.button2);
    mButton3 = (Button) findViewById(R.id.button3);
    mButton4 = (Button) findViewById(R.id.button4);
    mButton5 = (Button) findViewById(R.id.button5);
    mButton6 = (Button) findViewById(R.id.button6);

    mButton1.setOnClickListener(this);
    mButton2.setOnClickListener(this);
    mButton3.setOnClickListener(this);
    mButton4.setOnClickListener(this);
    mButton5.setOnClickListener(this);
    mButton6.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button1:
            playMusic("lori.mp3");
            break;
        case R.id.button2:
            playMusic("lori.mp3");
            break;
        case R.id.button3:
            playMusic("lori.mp3");
            break;
        case R.id.button4:
            playMusic("lori.mp3");
            break;
        case R.id.button5:
            playMusic("lori.mp3");
            break;
        case R.id.button6:
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
            break;
    }
}

private void playMusic(String songName) {
    if (mPlayer != null && mPlayer.isPlaying()) {
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
    }

    try {
        mPlayer = new MediaPlayer();
        mPlayer.reset();
        AssetFileDescriptor descriptor = getAssets().openFd(songName);
        mPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        mPlayer.prepare();
        mPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

我希望它适合你。如果此代码适合您,请记住将答案标记为正确。谢谢!