一次播放一个MediaPlayer实例

时间:2011-09-25 01:20:56

标签: android media-player

我会保持简单。我正在制作一个基本上可以被视为音板的东西。我有一切按照我想要的方式工作,但有一个例外。当我选择一个ImageView时,会播放分配给它的声音。当我选择另一个ImageView时,分配给它的声音会播放,但第一个声音仍在播放。他们只是重叠。我希望以前的声音在新的声音开始时停止。

我确实看到了有关此问题的类似问题,但这些问题都没有对我有所帮助。是否有一些简单的东西我可以改变以使其有效?

这是我的代码(减去导入以节省空间):

public class ringtones extends Activity实现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; 
}

1 个答案:

答案 0 :(得分:0)

如果您的声音片段小于1 MB,请尝试使用SoundPool而不是MediaPlayer。

SoundPool pool = new SoundPool(maxStreams, streamType, srcQuality);

maxStreams设置为您希望一次播放的声音数量(在您的情况下为1)。 streamType可能会AudioManager.STREAM_MUSICsrcQuality设置为0(尚未实施)。

然后你会打电话给

pool.load(this, R.raw.sound_file, 1);

加载声音文件。

SoundPool的文档相当不错: http://developer.android.com/reference/android/media/SoundPool.html