gpt4 book ai didi

android - 无法创建媒体播放器,简短的 Android 应用程序

转载 作者:太空狗 更新时间:2023-10-29 15:18:17 24 4
gpt4 key购买 nike

非常短的应用程序,您按下一个按钮并播放一段声音。声音为 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

试图用谷歌搜索这个坏小子,但到目前为止一无所获,有人有什么想法吗?让我感到不安的是它可以在 VM 上运行,但不能在我的手机上运行。

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

最佳答案

根据这个问题:R.raw.anything cannot be resolved

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

或者

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

    MediaPlayer mp_nudge = MediaPlayer.create(getBaseContext(), com.exmaple.goran.R.raw.nudge_nudge);

关于android - 无法创建媒体播放器,简短的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10521884/

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