简体   繁体   English

Android Studio分享mp3和播放/停止歌曲

[英]Android Studio Share mp3 and play/stop songs

I have this code: 我有以下代码:

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
MediaPlayer mp;

//Buttons 
ImageButton peroperoperopero;
ImageButton personajitosdos;
peroperoperopero = (ImageButton) findViewById(R.id.peroperoperopero);
personajitosdos = (ImageButton) findViewById(R.id.personajitosdos);

//code
peroperoperopero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mp = MediaPlayer.create(MainActivity.this,R.raw.peroperopero);
            mp.start();
        }
    });

peroperoperopero.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Intent compartirAudio = new Intent(android.content.Intent.ACTION_SEND);
            compartirAudio.setType("audio/*");
            compartirAudio.putExtra(Intent.EXTRA_STREAM,
                    Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/" + R.raw.peroperopero));
            startActivity(Intent.createChooser(compartirAudio, "Compartir vía"));
            return false;
        }
    });

personajitosdos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mp = MediaPlayer.create(MainActivity.this,R.raw.unospersonajitos);
            mp.start();
        }
    });

}

} }

I would need to know what to modify to: 我需要知道修改为:

  • Pressing the button peroperoperopero share it in WhatsApp (currently when I share it, a document is sent but not the audio) 按下按钮peroperoperopero在WhatsApp中共享它(当前,当我共享它时,发送文档但不发送音频)

  • I want only one sound to play at the same time, now if I precede the two buttons at the same time the sounds are superimposed. 我现在只希望同时播放一种声音,现在,如果我同时在两个按钮之前放在一起,声音就会叠加在一起。

  • I also want that while the sound is playing, if I press the button again, it stops. 我还希望声音正在播放,如果再次按下按钮,它会停止。

please tell me what do I have to change in the code? 请告诉我我必须更改代码什么? Thank you very much. 非常感谢你。

For audio play and stop use this following: 对于音频播放和停止,请使用以下命令:

  MediaPlayer mp;
  mp = MediaPlayer.create(context, R.raw.sound_one);
  mp.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
    // TODO Auto-generated method stub
    mp.reset();
    mp.release();
    mp=null;
   }
  });
  mp.start();

To share audio with what's app you can go to the following link Sharing audio file 要与应用程序共享音频,您可以转到以下链接共享音频文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM