共享音频文件 [英] Sharing an audio file

查看:118
本文介绍了共享音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过按钮共享音频文件,但是当我单击它时,应用程序显示一条消息不支持文件格式".我该如何解决?这是我的代码

I am trying to share an audio file with a button, but when I click on it, the application shows a message "The file format isn't supported". How can I solve this? Here is my code

Button buonaseeera=(Button) findViewById(R.id.pulsantebuonaseeera);



    buonaseeera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            audiobuonaseeera=MediaPlayer.create(getApplicationContext(), R.raw.buonaseeeraaudio);
            audiobuonaseeera.start();





    Button sharebutton=(Button) findViewById(R.id.sharebutton);
            sharebutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String sharePath = 
Environment.getExternalStorageDirectory().getPath()
                            + "raw2sd";
                    Uri uri = Uri.parse(sharePath);
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("audio/*");
                    share.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(share, "Share Audio 
File"));

推荐答案

尝试以下示例:

public class MusicPlayerActivity extends AppCompatActivity {
Activity mactivity;
private Button btn_shareaudio;

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

        mactivity = this;

        btn_shareaudio = (Button) findViewById(R.id.btn_activity_music_player_shareaudio);

        btn_shareaudio.setOnClickListener(
            new View.OnClickListener() {
            @Override
            public void onClick(View v) {     
            File f = new File(/*Path of the song*/);
            Uri uri = Uri.parse("file://" + f.getAbsolutePath());
            Intent share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.setType("audio/*");
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mactivity.startActivity(Intent.createChooser(share, "Share audio File"));

            Toast.makeText(getApplicationContext(), "Song Shared Successfully", Toast.LENGTH_SHORT).show();     
            }
       });
}

这篇关于共享音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆