android - 长按 ListView 项即可共享音频

标签 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();
    }

    }
模因类
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;
    }

}
范例
how should I stay raw
谢谢

最佳答案

我认为问题在于您没有在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;
            }
        });

关于android - 长按 ListView 项即可共享音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47955710/

相关文章:

android - 显示 SD 卡上的所有音乐

android - 在 Android Facebook SDK 中使用共享对话框。如何知道用户是否实际分享或取消分享 Activity ?

java - Android Studio(增加最终变量)

asp.net - Response.End 之后关闭窗口

javascript - 使用JavaScript在HTML5中多次加载音频

raspbian - PyAudio在Raspi上用Python录制/捕获并停止/终止

android - 具有未决 Intent 的通知操作不适用于 JellyBean+

java - 从选定的列表项中选择一个 TextView

java - 禁用 ListView 中的项目 android java

audio - 将WAV文件转换为PCAP