Sai Sai - 9 days ago 7
Android Question

Android Media Player:setDataSource Failed

I try to create Media Player using Surface View. When I try to set the DataSource for the Media Player, I got the setDataSource Failed Exception. How to resolve it.

code:



@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource//"+getPackageName()+ "/raw/"+R.raw.androidvideo));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
System.err.println(e.getMessage());
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.release();
}

Answer

You forgot to add colon after resource.

mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource://"+getPackageName()+ "/raw/"+R.raw.androidvideo));