如何在android中的该列表视图项上单击上下文菜单项时播放列表视图项

时间:2014-08-22 11:49:14

标签: android listview

我正在开发一个应用程序,用于记录传入和传出呼叫,并在一个列表视图中显示。当我点击列表视图项时,它将显示一个上下文菜单项(播放,删除) 选项。当我单击上下文菜单列表视图中的播放项时,将播放媒体播放器。我会尝试,但它不会播放媒体播放器。我可以这样做,请帮助我。新的Android开发。我发布了我的代码。

*public class CallLog
extends Activity
{
private final String TAG = "CallRecorder";
private ListView fileList = null;
public static ArrayAdapter<String> fAdapter = null;
private ArrayList<String> recordingNames = null;
private SeekBar seekbar;
private MediaPlayer mediaPlayer=new MediaPlayer();
private ImageButton pauseButton;
public TextView startTimeField,endTimeField;
AudioManager audioManager;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();
int oneTimeOnly = 0;

private void loadRecordingsFromProvider() 
{
    //fAdapter.clear();
    fAdapter.clear();
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(RecordingProvider.CONTENT_URI, null, null, null, null);
    String[] names = new String[c.getCount()];
    int i = 0;
    if (c.moveToFirst()) {
        do {
            // Extract the recording names
            fAdapter.add(c.getString(RecordingProvider.DETAILS_COLUMN));
            i++;
        } while (c.moveToNext());
    }
    fAdapter.notifyDataSetChanged();
   }
  @SuppressLint("ShowToast")
  private void loadRecordingsFromDir() 
  {
    fAdapter.clear();
    File dir = new File(RecordService.DEFAULT_STORAGE_LOCATION);
    String[] dlist = dir.list();
    Log.v("fille", "recording file" +dlist);
    for (int i=0; i<dlist.length; i++) {
        fAdapter.add(dlist[i]);
    }
    fAdapter.notifyDataSetChanged();
   }
    private class CallItemClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        view.showContextMenu();
        CharSequence s = (CharSequence)parent.getItemAtPosition(position);
        Log.w(TAG, "CallLog just got an item clicked: " + s);
        File f = new File(RecordService.DEFAULT_STORAGE_LOCATION + "/" + s.toString());
        /*boolean useMediaController = true;

        if (useMediaController) {
            Intent playIntent = new Intent(getApplicationContext(), CallPlayer.class);      //Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(f);
            playIntent.setData(uri);
            startActivity(playIntent);
        } else {
            playFile(s.toString());
        }*/
        }
        }
      @Override
     public void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    setContentView(R.layout.call_log);

    //recordingNames = new String[0];
    fileList = (ListView)findViewById(R.id.play_file_list);
    Context context = getApplicationContext();
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    fAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
    fileList.setAdapter(fAdapter);
    fileList.setOnItemClickListener(new CallItemClickListener());
    registerForContextMenu(fileList);
   }


   public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater m = getMenuInflater();  
    m.inflate(R.menu.nn, menu);  
   }



    @SuppressLint("ShowToast")
   @Override
    public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();  
    int index=info.position;
    switch(item.getItemId()){ 

    case R.id.play:


        final Dialog seekDialog = new Dialog(this);  
        seekDialog.setTitle("paly");  
        seekDialog.setContentView(R.layout.dialog); 
        //final Button butt=(Button)levelDialog.findViewById(R.id.but);
        startTimeField=(TextView)seekDialog.findViewById(R.id.textView1);
        endTimeField=(TextView)seekDialog.findViewById(R.id.textView2);
        pauseButton = (ImageButton)seekDialog.findViewById(R.id.imageButton2);
        seekbar = (SeekBar)findViewById(R.id.seek);
        seekDialog.show();



        fileList.getItemAtPosition(index);
        Log.v("playyyyyyyy", "id" +fileList.getItemAtPosition(index));


        String path=fAdapter.getItem(index);
        try {

            mediaPlayer.setDataSource(path);

        } catch (IllegalArgumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        mediaPlayer.start();


        //Toast.makeText(getApplicationContext(), "play", 1000).show();
        return true;

       case R.id.delete:

        Toast.makeText(getApplicationContext(), "delete", 1000).show();
        return true;
       }

       return super.onContextItemSelected(item);
        }

        public void onStart()
            {
             super.onStart();
                Log.i(TAG, "CallLog onStart");
              }
                 public void onRestart()
              {
                super.onRestart();
                   Log.i(TAG, "CallLog onRestart");
               }
               public void onResume()
               {
                 super.onResume();
                   //Log.i(TAG, "CallLog onResume about to load recording list again, does this work?");
              loadRecordingsFromDir();
              }

                /*private void playFile(String fName) {
            Log.i(TAG, "playFile: " + fName);
            Context context = getApplicationContext();
              Intent playIntent = new Intent(context, PlayService.class);
             playIntent.putExtra(PlayService.EXTRA_FILENAME,  RecordService.DEFAULT_STORAGE_LOCATION + "/" + fName);
             ComponentName name = context.startService(playIntent);
           if (null == name) {
             Log.w(TAG, "CallLog unable to start PlayService with intent: " + playIntent.toString());
           } else {
            Log.i(TAG, "CallLog started service: " + name);
            }
           }*/
          protected void onDestroy() {
             /*if(null!=mediaPlayer){
               mediaPlayer.release();
             }*/
            super.onDestroy();
            if(mediaPlayer!=null){
           mediaPlayer.stop();
          mediaPlayer.release();
             mediaPlayer = null;
            }
             }
           /*public void onDestroy() {
             Context context = getApplicationContext();
             Intent playIntent = new Intent(context, PlayService.class);
          context.stopService(playIntent);
          super.onDestroy();
            }*/
              }*

0 个答案:

没有答案