原始文件夹中的音频文件出现问题

发布于 2024-12-03 07:37:01 字数 2160 浏览 0 评论 0原文

我想播放 /src/res/raw 文件夹中的音频文件,如下所示:

 public class SayItActivity extends Activity {
 private ImageButton SayLetterImgBtn;
 private MediaPlayer mp;     
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    SayLetterImgBtn=(ImageButton) findViewById(R.id.sayletter);
    SayLetterImgBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {

            mp = MediaPlayer.create(SayItActivity.this, R.raw.a);   
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mp.release();
                }

            });
        }

    });

}
}

Logcat

09-08 15:13:16.692: ERROR/AndroidRuntime(477): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mds.sayit/com.android.mds.sayit.SayItActivity}: java.lang.NullPointerException
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.os.Looper.loop(Looper.java:123)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 15:13:16.692: ERROR/AndroidRuntime(477): Caused by: java.lang.NullPointerException
09-08 15:13:16.713: WARN/ActivityManager(59):   Force finishing activity  com.android.mds.sayit/.SayItActivity

我该如何处理?

谢谢

I want to play an audio file which is there in my /src/res/raw folder as below:

 public class SayItActivity extends Activity {
 private ImageButton SayLetterImgBtn;
 private MediaPlayer mp;     
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    SayLetterImgBtn=(ImageButton) findViewById(R.id.sayletter);
    SayLetterImgBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {

            mp = MediaPlayer.create(SayItActivity.this, R.raw.a);   
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mp.release();
                }

            });
        }

    });

}
}

Logcat

09-08 15:13:16.692: ERROR/AndroidRuntime(477): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mds.sayit/com.android.mds.sayit.SayItActivity}: java.lang.NullPointerException
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.os.Looper.loop(Looper.java:123)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-08 15:13:16.692: ERROR/AndroidRuntime(477):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 15:13:16.692: ERROR/AndroidRuntime(477): Caused by: java.lang.NullPointerException
09-08 15:13:16.713: WARN/ActivityManager(59):   Force finishing activity  com.android.mds.sayit/.SayItActivity

How do I go about this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-12-10 07:37:01

替换为 ::

private MediaPlayer mp;  

MediaPlayer mp_xmPlayer2 = new MediaPlayer(),

更新 ::

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Replace with ::

private MediaPlayer mp;  

to

MediaPlayer mp_xmPlayer2 = new MediaPlayer(),

Update ::

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
懒猫 2024-12-10 07:37:01

您尚未在媒体播放器上调用准备方法。

基本上,要使用媒体播放器,您需要调用prepare()。然后媒体播放器准备媒体管道并在准备完成时通知应用程序。只有这样你才能在媒体播放器上调用start()。

让我知道这是否有效。

沙什

You have not called prepare method on media player.

Basically, to use media player, you need to invoke prepare (). media player then prepares media pipeline and notifies the application when it completes preparation. Only then you can call start() on media player.

let me know if this works.

Shash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文