Make soundclip in a listView selectable as (notifi

2019-08-28 14:00发布

问题:

I've got a list of tones which I've made, I've managed to get them into a list but now I'm faced with the challenge of allowing the user to "touch and hold" to assign as a notification tone. Here's the code from MainActivity.java:

       import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Axo");
s.setSoundResourceId(R.raw.axo);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }
}

I also have two other java files, one is a "sound class" and the other a "sound adapter" although I don't think they are relative to my question.

Just so you guys know, it's taken me weeks to get this far as I'm still a freshman in javaland. Any help or examples here would be hot! Hell if you wanna do the work for me then that's even sweeter!

回答1:

The solution I post is just making it possible for you to detect the position of the listitem you clicked and set as ringtone (which is not working), inside the setRingtone function the magic happens. all code to make the sound as ringtone must be placed in there

Inside the switch change the case id 'main_menu' to the item id from your menu -> context_menu.xml like below

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/main_menu"
    android:title="@string/set_ringtone"
    android:showAsAction="ifRoom"
    />
</menu>

Add beneath onCreateContextMenu the code below.

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.context_menu, menu);
   }
 }

 /*-----------------------------  add code below ------------------------------*/

 @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info;
        try {
            info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        } catch (ClassCastException e) {
            Log.e("", "bad menuInfo", e);
            return false;
        }

        switch (item.getItemId()){
            case R.id.main_menu:
                setRingtone(item.getItemId(), info.position);
                //Toast.makeText(this, "id = " + item.getItemId() + " pos =     " + info.position, Toast.LENGTH_SHORT).show();
                break;
            default:
                return false;
        }

        return true;
    }

    public  void setRingtone(int id, int pos){
        //RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);

        Toast.makeText(this, "id = " + id + " pos = " + pos, Toast.LENGTH_SHORT).show();
    }