我會保持簡單。我正在製作基本上被視爲音板的東西。除了一個例外,我都以我想要的方式工作。當我選擇一個ImageView時,分配給它的聲音播放。當我選擇另一個ImageView時,分配給它的聲音播放,但第一個聲音仍在播放。他們只是重疊。我希望以前的聲音在新的聲音開始時停止。一次播放一個MediaPlayer實例
我確實看到類似的問題已經被問到這個問題,但沒有人幫助我。有沒有簡單的我可以改變它使其工作?
這裏是我的代碼(減去進口,以節省空間):
公共類鈴聲擴展活動實現OnClickListener {
MediaPlayer songPlaying;
MediaPlayer ring1;
MediaPlayer ring2;
MediaPlayer ring3;
String songFile;
String songTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.ringview);
//Rintones Sound
ring1 = MediaPlayer.create(ringtones.this, R.raw.cc_sgl_rm);
ring2 = MediaPlayer.create(ringtones.this, R.raw.cc_sgl_kft);
ring3 = MediaPlayer.create(ringtones.this, R.raw.cc_sgl_e);
ImageView iv = (ImageView)findViewById(R.id.ccsgl1);
iv.setOnClickListener(this);
registerForContextMenu(iv);
ImageView iv2 = (ImageView)findViewById(R.id.ccsgl2);
iv2.setOnClickListener(this);
registerForContextMenu(iv2);
ImageView iv3 = (ImageView)findViewById(R.id.ccsgl3);
iv3.setOnClickListener(this);
registerForContextMenu(iv3);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.ccsgl1:
ring1.start();
songFile = "cc_sgl_rm";
songTitle = "Regenerate Me";
songPlaying = ring1;
break;
case R.id.ccsgl2:
ring2.start();
songFile = "cc_sgl_kft";
songTitle = "Knock Four Times";
songPlaying = ring2;
break;
case R.id.ccsgl3:
ring3.start();
songFile = "cc_sgl_e";
songTitle = "Eleven";
songPlaying = ring3;
break;
}
}
public boolean stopsong(MediaPlayer songPlaying){
if(songPlaying!=null){
songPlaying.stop();
songPlaying.release();
songPlaying.start();
}
return false;
}
//CONTEXT MENU
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
if
(savering(R.raw.cc_sgl_rm)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
if
(savering(R.raw.cc_sgl_kft)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
if
(savering(R.raw.cc_sgl_e)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
}
//Save into Ring tone Folder
public boolean savering(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false; }
String path="/sdcard/media/audio/ringtones/";
String filename=songFile+".mp3";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, songTitle);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "six3six");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
return true;
}
你想要的聲音previos停止?還是等待?從音板中我會期望聲音重疊。 –
我希望它完全停止以前的聲音。 –