长按listview项目以共享音频

时间:2017-12-23 20:18:32

标签: android listview audio share

当您点击根据arraylist中的项目播放声音的项目时,我的应用程序有一个音频项目列表。 我想要做的是,当我点击该项目时,根据我点击的项目,音频被共享whatsapp,facebook等。我怎样才能做到这一点? 我的长按事件中存在一些问题,但它不起作用。

MainActivity.class

public class MainActivity extends AppCompatActivity {
    ListView lv;
    MediaPlayer mp;
    ArrayList<memes> item;
    ArrayAdapter<memes> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memes_main);

        lv = findViewById(R.id.lv);
        mp = new MediaPlayer();

        item = new ArrayList<>();
        item.add(new memes("Gemidão", R.raw.gemidaoremix));
        item.add(new memes("Nunca nem vi", R.raw.nuncanemvi));
        item.add(new memes("Caga", R.raw.caga));
        item.add(new memes("Cagado de fome", R.raw.cagado));
        item.add(new memes("Cala Boca", R.raw.calaboca));
        item.add(new memes("Canal", R.raw.canal));
        item.add(new memes("Capeta", R.raw.capeta));

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(arrayAdapter);

        //play audio
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                playSong(position);
            }
        });
        //share
        View view = lv;
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                sendWhatsAppAudio();

                return true;
            }
        });

    }

    private void sendWhatsAppAudio(){
        InputStream inputStream;
        FileOutputStream fileOutputStream;
        try {
            inputStream = getResources().openRawResource(R.raw.gemidaoremix);
            fileOutputStream = new FileOutputStream(
                    new File(Environment.getExternalStorageDirectory(), "sound.mp3"));

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }

            inputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
        intent.setType("audio/*");
        startActivity(Intent.createChooser(intent, "Share sound"));
    }
public void playSong(int songIndex) {

        mp.reset();
        mp = MediaPlayer.create(this, item.get(songIndex).getResId());

        mp.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.release();
    }

    }

memes.class

public class memes{

    private String nome;
    private int resID;

    memes(String nome, int resID){

        this.nome = nome;
        this.resID = resID;
    }

    public String getNome(){
        return nome;
    }

    public int getResId(){
        return resID;
    }

    @Override
    public String toString(){
        return nome;
    }

}

Exemples

how should I stay raw

由于

2 个答案:

答案 0 :(得分:1)

我认为问题是你没有在listiew本身设置长按一下监听器。

而不是:

View view = lv;
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            sendWhatsAppAudio();

            return true;
        }
    });

尝试:

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Do your thing here
            // I suggest using the int position parameter to determine what item was clicked
            // Then use that information to determine what file to share
            return true;
            }
        });

答案 1 :(得分:0)

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

  @Override

   public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

   sendWhatsAppAudio();
   return true;
   }
 });