Reputation: 117
package com.example.instrumentapp;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
private Button E;
private MediaPlayer play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
E=(Button)findViewById(R.id.E);
E.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
play=MediaPlayer.create(MainActivity.this, R.raw.e);
play.start();
play.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
// TODO Auto-generated method stub
play.release();
}
});
}
});
}
}
I am trying to play an e note.
There is an e.wav file in raw folder.
There is a button id/E in the xml folder.
This is the only class in the project.
When i run the app and click the button it says unexpectedly closed.
What is wrong ?
Here is LOGCAT ;
05-13 23:04:29.374: E/MediaPlayer(469): Unable to to create media player
05-13 23:04:29.384: D/MediaPlayer(469): create failed:
05-13 23:04:29.384: D/MediaPlayer(469): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-13 23:04:29.384: D/MediaPlayer(469): at android.media.MediaPlayer.setDataSource(Native Method)
05-13 23:04:29.384: D/MediaPlayer(469): at android.media.MediaPlayer.create(MediaPlayer.java:645)
05-13 23:04:29.384: D/MediaPlayer(469): at com.example.instrumentapp.MainActivity$1.onClick(MainActivity.java:34)
05-13 23:04:29.384: D/MediaPlayer(469): at android.view.View.performClick(View.java:2408)
05-13 23:04:29.384: D/MediaPlayer(469): at android.view.View$PerformClick.run(View.java:8816)
05-13 23:04:29.384: D/MediaPlayer(469): at android.os.Handler.handleCallback(Handler.java:587)
05-13 23:04:29.384: D/MediaPlayer(469): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 23:04:29.384: D/MediaPlayer(469): at android.os.Looper.loop(Looper.java:123)
05-13 23:04:29.384: D/MediaPlayer(469): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 23:04:29.384: D/MediaPlayer(469): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:04:29.384: D/MediaPlayer(469): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 23:04:29.384: D/MediaPlayer(469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 23:04:29.384: D/MediaPlayer(469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 23:04:29.384: D/MediaPlayer(469): at dalvik.system.NativeStart.main(Native Method)
05-13 23:04:29.384: D/AndroidRuntime(469): Shutting down VM
05-13 23:04:29.384: W/dalvikvm(469): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 23:04:29.394: E/AndroidRuntime(469): FATAL EXCEPTION: main
05-13 23:04:29.394: E/AndroidRuntime(469): java.lang.NullPointerException
05-13 23:04:29.394: E/AndroidRuntime(469): at com.example.instrumentapp.MainActivity$1.onClick(MainActivity.java:35)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.view.View.performClick(View.java:2408)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.view.View$PerformClick.run(View.java:8816)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.os.Handler.handleCallback(Handler.java:587)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.os.Looper.loop(Looper.java:123)
05-13 23:04:29.394: E/AndroidRuntime(469): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 23:04:29.394: E/AndroidRuntime(469): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:04:29.394: E/AndroidRuntime(469): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 23:04:29.394: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 23:04:29.394: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 23:04:29.394: E/AndroidRuntime(469): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 8206
Reputation: 333
It's obviously that the MediaPlayer object is not created successfully. Here is the implementation of the API MediaPlayer.create():
public static MediaPlayer create(Context context, int resid) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null) return null;
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
}
return null;
}
Apparently, mp.setDataSource
throws an IOException and thus returns a null MediaPlayer object. Please double check your e.wav in the raw directory:
Upvotes: 1