我正在创建一个简单的项目,我正在创建一个允许我播放多个音频文件的应用程序。
我想播放多个文件,有人可以帮帮我吗?我创建了两个按钮,其中一个播放一首歌曲,另一个按钮播放另一个。但是,一个按钮只能工作并播放我添加的最新音频。有人知道该怎么做吗? 以下是我的代码示例:
package com.example.soundboard;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
MediaPlayer myMediaPlayer = null;
MediaPlayer myMediaPlayer1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.herbimage);
myMediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.icecream);
myMediaPlayer1 = MediaPlayer.create(MainActivity.this, R.raw.boxandstring);
Button soundclip1 = (Button) findViewById(R.id.button1);
Button soundclip2 = (Button) findViewById(R.id.button2);
soundclip1.setOnClickListener (new OnClickListener()
{
@Override
public void onClick(View view)
{
myMediaPlayer.start();
myMediaPlayer.pause();
}
});
//second sound clip
soundclip2.setOnClickListener (new OnClickListener()
{
@Override
public void onClick(View view)
{
myMediaPlayer1.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
尝试使用SoundPool类 它有多种声音