Search code examples
javaandroidandroid-mediaplayer

Android: Long press of button to save sound (Soundboard)


I have put together a basic soundboard using Android Studio. Please be warned; I am new to Android app development.

I have a series of buttons that I have managed to get to play some .mp3 sounds that I have placed in the Raw folder and mentioned in my MainActivity.java file.

Albeit, I would like the ability to be able to long press on the buttons to save the associated sound to the storage of the phone.

Does anybody have any idea how I could go about this please?

Many thanks, Billy.

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);

    }

}```



Solution

  • i don't see where is your button but let's suppose button_one will save mySound1 after long click

    button_one.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
    
         copyRAWtoPhone(R.raw.nickingbentcoppers,storagePath);
    
         return true;
     }
    });
    

    and here is copyRawToPhone function ,where first Variable id will be your raw file id and String path will be your phone storage path.

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