Java 安卓:长按按钮保存声音(音板)

Java 安卓:长按按钮保存声音(音板),java,android,android-mediaplayer,Java,Android,Android Mediaplayer,我用Android Studio制作了一个基本的音板。请注意;我是Android应用程序开发新手 我有一系列按钮,可以播放一些.mp3声音,这些声音我放在Raw文件夹中,并在MainActivity.java文件中提到 尽管如此,我还是希望能够长按按钮将相关声音保存到手机存储器中 有人知道我该怎么做吗 非常感谢,比利 import android.app.AlertDialog; import android.content.DialogInterface; import android.co

我用Android Studio制作了一个基本的音板。请注意;我是Android应用程序开发新手

我有一系列按钮,可以播放一些.mp3声音,这些声音我放在Raw文件夹中,并在MainActivity.java文件中提到

尽管如此,我还是希望能够长按按钮将相关声音保存到手机存储器中

有人知道我该怎么做吗

非常感谢,比利

import android.app.AlertDialog; 
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mysound1;
    MediaPlayer mysound2;
    MediaPlayer mysound3;
    MediaPlayer mysound4;
    MediaPlayer mysound5;
    MediaPlayer mysound6;
    MediaPlayer mysound7;
    MediaPlayer mysound8;
    MediaPlayer mysound9;
    MediaPlayer mysound10;
    MediaPlayer mysound11;
    MediaPlayer mysound12;
    MediaPlayer mysound13;
    MediaPlayer mysound14;
    MediaPlayer mysound15;
    MediaPlayer mysound16;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mysound1 = MediaPlayer.create(this, R.raw.nickingbentcoppers);
        mysound2 = MediaPlayer.create(this, R.raw.bentcoppers);
        mysound3 = MediaPlayer.create(this, R.raw.bentcoppers2);
        mysound4 = MediaPlayer.create(this, R.raw.bentcoppers3);
        mysound5 = MediaPlayer.create(this, R.raw.twentyfirstcentury);
        mysound6 = MediaPlayer.create(this, R.raw.imdoingmine);
        mysound7 = MediaPlayer.create(this, R.raw.illgoafterhim);
        mysound8 = MediaPlayer.create(this, R.raw.iamcalm);
        mysound9 = MediaPlayer.create(this, R.raw.likethebattle);
        mysound10 = MediaPlayer.create(this, R.raw.thatsright);
        mysound11 = MediaPlayer.create(this, R.raw.seenenough);
        mysound12 = MediaPlayer.create(this, R.raw.jesusmary);
        mysound13 = MediaPlayer.create(this, R.raw.changepage);
        mysound14 = MediaPlayer.create(this, R.raw.opendialog);
        mysound15 = MediaPlayer.create(this, R.raw.yes);
        mysound16 = MediaPlayer.create(this, R.raw.no);

    }

    public void sound1(View view) {
        mysound1.start();
    }

    public void sound2(View view) {
        mysound2.start();
    }

    public void sound3(View view) {
        mysound3.start();
    }

    public void sound4(View view) {
        mysound4.start();
    }

    public void sound5(View view) {
        mysound5.start();
    }

    public void sound6(View view) {
        mysound6.start();
    }

    public void sound7(View view) {
        mysound7.start();
    }

    public void sound8(View view) {
        mysound8.start();
    }

    public void sound9(View view) {
        mysound9.start();
    }

    public void sound10(View view) {
        mysound10.start();
    }

    public void sound11(View view) {
        mysound11.start();
    }

    public void sound12(View view) {
        mysound12.start();
    }

    public void sound13(View view) {
        mysound13.start();
    }

    public void sound14(View view) {
        mysound14.start();
    }

    public void sound15(View view) {
        mysound15.start();
    }

    public void sound16(View view) {
        mysound16.start();
    }

    @Override
    public void onBackPressed() {

        mysound14.start();

        new AlertDialog.Builder(this)

                .setMessage("Are you sure you want to exit, feller?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mysound15.start();

                        ActivityCompat.finishAffinity(MainActivity.this);

                        System.exit(0);
                    }
                })

                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mysound16.start();
                    }
                })

                .show();

    }

    public void gotopage1(View v) {

        mysound13.start();
        finish();

        Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
        startActivity(myIntent);

    }

    public void gotopage2(View v) {

        mysound13.start();

        Intent myIntent = new Intent(getBaseContext(), SecondActivity.class);
        startActivity(myIntent);

    }

}```



我看不出你的按钮在哪里,但让我们假设按钮在长时间点击后将保存
mySound1

button_one.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {

     copyRAWtoPhone(R.raw.nickingbentcoppers,storagePath);

     return true;
 }
});
这里是
copyRawToPhone
函数,其中第一个变量id将是您的原始文件id,字符串路径将是您的手机存储路径

private void CopyRAWtoPhone(int id, String path) throws IOException {
InputStream in = getResources().openRawResource(id);
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try { 
    while ((read = in.read(buff)) > 0) {
        out.write(buff, 0, read);
    } 
} finally { 
    in.close();
    out.close();
 } 
}    

我试着应用你发布的内容,但我可能在某个地方出了问题

我一直在尝试将声音文件保存到内部存储器(因为我没有外部存储器)。当我长按按钮时,我输入的消息会弹出,说文件已经保存(到应用程序的目录为止),但我认为它根本没有保存在那里

请你看一下我的代码,看看我哪里出错了。谢谢你帮我摆脱困境,我真的很感谢大家的帮助

''''@覆盖


我的MainActivity.Java文件代码粘贴在上面,以防需要。您好,非常感谢您的回答。我一直在考虑将其实现到我的代码中。按钮本身仅在activity_mail.xml文件中引用,例如,在该文件中,它们由android:id=“@+id/button6”等表示。我不知道这是否有区别?我没有完全理解您的意思,请您解释一下您的问题。对不起,我的意思是Main Activity.java文件中根本没有提到任何按钮。所以我不知道我是否需要在Main.Activity中添加一些额外的代码?我希望这有意义?谢谢。是的,你必须添加一个小部件(按钮,图像,…),一旦你点击它,它就会触发copyRAWtophone功能。我的手机在内存中有一个下载文件夹,因为它没有外部SD卡。将保存文件夹设置为下载文件夹的最佳方式是什么?谢谢
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);

    Button a;

    a = findViewById(R.id.button11);

    a.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View arg0) {

            String path = getFilesDir().getAbsolutePath();

            try {
                copyRAWtoPhone(R.raw.nickingbentcoppers, path);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Toast.makeText(SecondActivity.this, "Saved to " + getFilesDir(), Toast.LENGTH_SHORT).show();

            return true;
                               }

        private void copyRAWtoPhone(int nickingbentcoppers, String path) throws IOException {

            InputStream in = getResources().openRawResource(nickingbentcoppers);
            FileOutputStream out = new FileOutputStream(path);
            byte[] buff = new byte[1024];
            int read = 0;
            try {
                while ((read = in.read(buff)) > 0) {
                    out.write(buff, 0, read);
                }
            } finally {
                in.close();
                out.close();
            }

        }
    }); ''''