我使用Android Studio整理了基本的音板。请注意;我是Android应用程序开发的新手。
我有一系列按钮,可以播放一些放置在Raw文件夹中并在MainActivity.java文件中提到的.mp3声音。
尽管如此,我希望能够长按按钮以将关联的声音保存到手机的存储器中。
有人知道我该怎么做吗?
非常感谢,比利。
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);
}
}```
答案 0 :(得分:0)
我看不到您的按钮在哪里,但让我们假设在长时间单击后button_one将保存mySound1
button_one.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
copyRAWtoPhone(R.raw.nickingbentcoppers,storagePath);
return true;
}
});
这是copyRawToPhone
函数,其中第一个Variable ID将是您的原始文件ID,String路径将是您的电话存储路径。
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();
}
}
答案 1 :(得分:0)
我尝试应用您发布的内容,但是我可能在某个地方出错了。
我一直试图将声音文件保存到内部存储中(因为我没有外部存储器)。长按该按钮时,会弹出一条消息,提示我文件已保存(到目前为止已保存到应用程序的目录中),但我认为它实际上根本不会保存在那里。>
请您看看我的代码,看看我哪里出错了。感谢您帮助这个菜鸟,我真的很感谢人们的帮助!
''''@@覆盖
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();
}
}
}); ''''