gpt4 book ai didi

audio - 在Android上修改Sound Player

转载 作者:行者123 更新时间:2023-12-03 02:11:26 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的钢琴应用程序。我找到了本教程; http://www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/

我在此链接上下载了文件并导入了它。我运行该应用程序,上面有4个按钮,可播放 Assets 文件中加载的4种不同的音符。

然后,我尝试修改此项目以制作钢琴应用。

起初,我从xml中删除了这些红色按钮。

我在背景上添加了钢琴图像。

然后我在每个键上创建了按钮。

xml文件为;

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_vertical"
android:background="@drawable/images"
android:orientation="vertical" >

<Button
android:id="@+id/buttonC4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonD4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonE4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonF4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />


<Button
android:id="@+id/buttonG4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonA4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonB4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonC5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

<Button
android:id="@+id/buttonD5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />


<Button
android:id="@+id/buttonE5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />

</LinearLayout>

然后我在网上找到了笔记并下载了。

我将这些便笺文件上传到 Assets 文件夹。

它们每个都是wav格式。

然后我将这些代码添加到PianoTest Activity中;
  buttons = new ArrayList<View>();
buttons.add(findViewById(R.id.buttonC4));
buttons.add(findViewById(R.id.buttonD4));
buttons.add(findViewById(R.id.buttonF4));
buttons.add(findViewById(R.id.buttonG4));
buttons.add(findViewById(R.id.buttonA4));
buttons.add(findViewById(R.id.buttonB4));
buttons.add(findViewById(R.id.buttonC5));
buttons.add(findViewById(R.id.buttonD5));
buttons.add(findViewById(R.id.buttonE5));

然后我修改了这个功能;

private void toggleButtonSound(查看按钮,向下按下 bool(boolean) 值)
{
字符串注释= null;
switch (button.getId())
{
case R.id.buttonC4:
note = "C4";
break;

case R.id.buttonD4:
note = "D4";
break;
case R.id.buttonE4:
note = "E4";
break;

case R.id.buttonF4:
note = "F4";
break;

case R.id.buttonG4:
note = "G4";
break;
case R.id.buttonA4:
note = "A4";
break;

case R.id.buttonB4:
note = "B4";
break;

case R.id.buttonC5:
note = "C5";
break;

case R.id.buttonD5:
note = "D5";
break;
case R.id.buttonE5:
note = "E5";
break;
}

然后我改变了AudioTrack类。我将格式更改为wav文件;
public void run()
{
try
{
String path = note + ".wav";

其他所有内容都与原始项目中的相同。

但是该应用无法正常运行。

当我运行该应用程序并单击某个按键上的透明按钮时,它会播放声音,但与实际声音不同。

听起来有些不同。我可以说是音符的电子版本。

当我单击其他一些键时,应用程序崩溃。它表示应用程序意外停止。

怎么了 ?我应该怎么做才能使其正常工作?原因可能是我dpwnloaded的文件。我需要找到哪种笔记?在此项目的位或持续时间方面是否有任何特殊格式?

这对我来说非常重要,如果您可以从链接下载文件并运行,则可以看到其工作原理。您可以建议我修改它的方法。我希望你能帮助我。
这是logcat:
05-13 19:49:55.580: D/AndroidRuntime(485): Shutting down VM
05-13 19:49:55.580: W/dalvikvm(485): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 19:49:55.600: E/AndroidRuntime(485): FATAL EXCEPTION: main
05-13 19:49:55.600: E/AndroidRuntime(485): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.martinhoeller/net.martinhoeller.PianoTestActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class ButtonView
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.os.Looper.loop(Looper.java:123)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 19:49:55.600: E/AndroidRuntime(485): at dalvik.system.NativeStart.main(Native Method)
05-13 19:49:55.600: E/AndroidRuntime(485): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class ButtonView
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.Activity.setContentView(Activity.java:1647)
05-13 19:49:55.600: E/AndroidRuntime(485): at net.martinhoeller.PianoTestActivity.onCreate(PianoTestActivity.java:23)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-13 19:49:55.600: E/AndroidRuntime(485): ... 11 more
05-13 19:49:55.600: E/AndroidRuntime(485): Caused by: java.lang.ClassNotFoundException: android.view.ButtonView in loader dalvik.system.PathClassLoader[/data/app/net.martinhoeller-1.apk]
05-13 19:49:55.600: E/AndroidRuntime(485): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-13 19:49:55.600: E/AndroidRuntime(485): ... 20 more
05-13 19:52:22.293: D/AndroidRuntime(513): Shutting down VM
05-13 19:52:22.293: W/dalvikvm(513): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 19:52:22.309: E/AndroidRuntime(513): FATAL EXCEPTION: main
05-13 19:52:22.309: E/AndroidRuntime(513): java.lang.ArrayIndexOutOfBoundsException
05-13 19:52:22.309: E/AndroidRuntime(513): at net.martinhoeller.PianoTestActivity.onTouchEvent(PianoTestActivity.java:66)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.app.Activity.dispatchTouchEvent(Activity.java:2089)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.os.Looper.loop(Looper.java:123)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 19:52:22.309: E/AndroidRuntime(513): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:52:22.309: E/AndroidRuntime(513): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 19:52:22.309: E/AndroidRuntime(513): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您似乎有很多问题。让我们先尝试修复布局膨胀崩溃问题。

List<Button> buttons;
...
buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.buttonC4));
buttons.add((Button)findViewById(R.id.buttonD4));
...
for (Button button : buttons) {
button.setOnClickListener(this);
}

这将正确转换布局的“按钮 View ”,并为每个 View 设置一个OnClickListener。
您的 Activity 将需要 implement View.OnClickListener才能使用 setOnClickListener(this)。如果您将点击处理分开,请替换为适当的类,在这种情况下,该类将为 implement OnClickListener而不是Activity。

关于audio - 在Android上修改Sound Player,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23639666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com