gpt4 book ai didi

java - 如何播放主屏幕和副屏幕上添加的音乐?

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:01 25 4
gpt4 key购买 nike

我是 Android 世界的新手。我正在开发一个应用程序,主屏幕上有 6 个按钮,第二屏幕上有 5 个按钮。主屏幕上的 5 个按钮应该能够在用户单击这些按钮时播放音乐。每个按钮应该能够播放与原始文件夹中的音乐相对应的不同音乐。主屏幕上的第六个按钮应允许用户转到下一个屏幕(第二个屏幕)。同样,第二个屏幕上的 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();
}
}

第二个屏幕 Java 类

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));




}

最佳答案

我认为您没有准备好媒体播放器。我给你留下了一个有效的代码。我总是准备同一首歌。请记住根据您的音乐进行更改,并创建一个“assets”文件夹,您可以在其中放置“.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();
}

}

}

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

关于java - 如何播放主屏幕和副屏幕上添加的音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com