长按列表视图项目以共享音频
Long click on listview item to share audio
我的应用程序有一个音频项目列表,当您单击一个项目时,它会根据数组列表中的项目播放声音。
我想要做的是,当我点击该项目时,音频会根据我点击的项目共享 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;
}
}
例子
谢谢
我认为问题是你没有在列表本身上设置长按监听器。
而不是:
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;
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
sendWhatsAppAudio();
return true;
}
});
我的应用程序有一个音频项目列表,当您单击一个项目时,它会根据数组列表中的项目播放声音。 我想要做的是,当我点击该项目时,音频会根据我点击的项目共享 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;
}
}
例子
谢谢
我认为问题是你没有在列表本身上设置长按监听器。
而不是:
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;
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
sendWhatsAppAudio();
return true;
}
});