原始文件夹中的音频文件出现问题
我想播放 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换为 ::
以
更新 ::
Replace with ::
to
Update ::
您尚未在媒体播放器上调用准备方法。
基本上,要使用媒体播放器,您需要调用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