我的项目有2个音频文件,' dog.mp3'和' cat.mp3'所以下面的代码使我能够根据特定的列表视图项目点击播放任何声音但是如何修改代码以便我想实现以下目的?:
所有帮助将不胜感激。
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
mp = MediaPlayer.create(Test.this, R.raw.cat);
mp.start();
}
if (position == 1) {
mp = MediaPlayer.create(Test.this, R.raw.dog);
mp.start();
}
}
});
错误
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
ListView mainList;
String[] listContent = {
"chimes", "chord", "ding",
"notify", "recycle", "ringin",
"ringout", "tada"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainList = (ListView)findViewById(R.id.main_listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
mainList.setAdapter(adapter);
int[] resID = { R.raw.chimes, R.raw.chord, R.raw.ding,
R.raw.notify, R.raw.recycle, R.raw.ringin, R.raw.ringout, R.raw.tada };
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
playSong(position);
}
});
public void playSong(int songIndex) {
// Play song
mp.reset();
mp.setDataSource(resID[songIndex]);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
}
}
答案 0 :(得分:1)
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.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// variable declaration
private ListView mainList;
private MediaPlayer mp;
private final String[] listContent = { "chimes", "chord", "ding", "notify",
"recycle", "ringin", "ringout", "tada" };
private final int[] resID = { R.raw.chimes, R.raw.chord, R.raw.ding,
R.raw.notify, R.raw.recycle, R.raw.ringin, R.raw.ringout,
R.raw.tada };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing variables
mp = new MediaPlayer();
mainList = (ListView) findViewById(R.id.main_listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
mainList.setAdapter(adapter);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
playSong(position);
}
});
}
public void playSong(int songIndex) {
// Play song
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getApplicationContext(), resID[songIndex]);// create's
// new
// mediaplayer
// with
// song.
// if you don't know what is getApplicationContext() here better use
// MainActivity.this or yourclassname.this
mp.start(); // starting mediaplayer
// setting up what to do if current song completes.
/*
* mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
*
* @Override public void onCompletion(MediaPlayer mp) { // TODO
* Auto-generated method stub
*
* } });
*/
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
答案 1 :(得分:0)
您可能应该使用SoundPool
:
http://developer.android.com/reference/android/media/SoundPool.html
您可以加载onCreate中的所有音频,并将maxStreams设置为1,这样一次只能播放一个声音。