3

非常短的应用程序,您按下一个按钮并播放一段声音。声音采用 mp3/wav 格式,该应用程序在 eclipse 中的 VM 上运行,但在手机(Galaxy S2)上尝试时不起作用。

public class SoundBites extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.soundbite);     
    }

    public void SheldonButtonListener(View v){
        MediaPlayer mp_bazinga = MediaPlayer.create(getBaseContext(), R.raw.bazinga);
        mp_bazinga.start();
    }

    public void SpanishButtonListener(View v){
        MediaPlayer mp_spanish = MediaPlayer.create(getBaseContext(), R.raw.spanish_inquisition);
        mp_spanish.start();
    }

    public void NudgeButtonListener(View v){
        MediaPlayer mp_nudge = MediaPlayer.create(getBaseContext(), R.raw.nudge_nudge);
        mp_nudge.start();
    }  
}

使用 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1.0">

    <View
        android:id="@+id/buttons_helper1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <View
        android:id="@+id/buttons_helper2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/bazinga_button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@+id/buttons_helper1"
        android:layout_above="@+id/buttons_helper2"
        android:background="@drawable/sheldon_cooper"
        android:onClick="SheldonButtonListener" />

    <Button
        android:id="@+id/spanish_button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@+id/buttons_helper1"
        android:layout_above="@+id/buttons_helper2"
        android:background="@drawable/spanish_inquisition"
        android:onClick="SpanishButtonListener" />

    <Button
        android:id="@+id/nudge_button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/buttons_helper2"
        android:background="@drawable/nudge_nudge"
        android:onClick="NudgeButtonListener" />

</RelativeLayout>

我收到以下错误:

05-09 20:12:15.395: E/MediaPlayer(15110): Unable to to create media player
05-09 20:12:15.405: D/MediaPlayer(15110): create failed:
05-09 20:12:15.405: D/MediaPlayer(15110): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.media.MediaPlayer.setDataSource(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.media.MediaPlayer.create(MediaPlayer.java:741)
05-09 20:12:15.405: D/MediaPlayer(15110):   at com.example.goran.SoundBites.NudgeButtonListener(SoundBites.java:30)
05-09 20:12:15.405: D/MediaPlayer(15110):   at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110):   at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.view.View$1.onClick(View.java:3064)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.view.View.performClick(View.java:3591)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.view.View$PerformClick.run(View.java:14263)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.os.Handler.handleCallback(Handler.java:605)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.os.Looper.loop(Looper.java:137)
05-09 20:12:15.405: D/MediaPlayer(15110):   at android.app.ActivityThread.main(ActivityThread.java:4507)
05-09 20:12:15.405: D/MediaPlayer(15110):   at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110):   at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.405: D/MediaPlayer(15110):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-09 20:12:15.405: D/MediaPlayer(15110):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-09 20:12:15.405: D/MediaPlayer(15110):   at dalvik.system.NativeStart.main(Native Method)
05-09 20:12:15.405: D/AndroidRuntime(15110): Shutting down VM
05-09 20:12:15.405: W/dalvikvm(15110): threadid=1: thread exiting with uncaught exception (group=0x40c3e1f8)
05-09 20:12:15.420: E/AndroidRuntime(15110): FATAL EXCEPTION: main
05-09 20:12:15.420: E/AndroidRuntime(15110): java.lang.IllegalStateException: Could not execute method of the activity
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.view.View$1.onClick(View.java:3069)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.view.View.performClick(View.java:3591)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.view.View$PerformClick.run(View.java:14263)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.os.Handler.handleCallback(Handler.java:605)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.os.Looper.loop(Looper.java:137)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.app.ActivityThread.main(ActivityThread.java:4507)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at dalvik.system.NativeStart.main(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110): Caused by: java.lang.reflect.InvocationTargetException
05-09 20:12:15.420: E/AndroidRuntime(15110):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.420: E/AndroidRuntime(15110):    at android.view.View$1.onClick(View.java:3064)
05-09 20:12:15.420: E/AndroidRuntime(15110):    ... 11 more
05-09 20:12:15.420: E/AndroidRuntime(15110): Caused by: java.lang.NullPointerException
05-09 20:12:15.420: E/AndroidRuntime(15110):    at com.example.goran.SoundBites.NudgeButtonListener(SoundBites.java:31)
05-09 20:12:15.420: E/AndroidRuntime(15110):    ... 14 more

试图用谷歌搜索这个坏男孩,但到目前为止空手而归,有人有什么想法吗?令我不安的是它可以在虚拟机上运行,​​但不能在我的手机上运行。

编辑:发现两个问题,一个是我需要控制音频音量,另一个似乎是我的 WAV 文件已损坏,至少现在可以输出 mp3!

4

2 回答 2

0

根据这个问题:R.raw.anything 无法解决

Eclipse 决定导入 android.R 而不是你的 package_name.R 文件...

或者

您需要使用完整的包名来引用原始资源。

    MediaPlayer mp_nudge = MediaPlayer.create(getBaseContext(), com.exmaple.goran.R.raw.nudge_nudge);
于 2012-05-09T18:41:45.647 回答
0

尝试修复项目属性。然后清理你的项目!将再次生成 R。

于 2012-05-09T19:19:45.323 回答