[英]How to play more than one audio file in android?
I am creating a simple project where I am making an app that allows me to play more than one audio file. 我正在创建一个简单的项目,正在制作一个允许我播放多个音频文件的应用程序。
I want to play more than one file, can someone help me? 我想播放多个文件,有人可以帮我吗? I created two buttons in which one plays one song and the other button plays another. 我创建了两个按钮,其中一个按钮播放一首歌曲,另一个按钮播放另一首。 However, one button only works and plays the recent audio that i added in. Does anybody know what to do? 但是,一个按钮只能工作并播放我添加的最新音频。有人知道该怎么做吗? Here is an example of my code: 这是我的代码示例:
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);
}
}
尝试使用SoundPool类获取一种以上的声音
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.