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 } catch (Exception ex) { Log.d(TAG, "create failed:", ex); // fall through } return null; }
public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) { try { MediaPlayer mp = new MediaPlayer(); mp.setDataSource(context, uri); if (holder != null) { mp.setDisplay(holder); } mp.prepare(); return mp; } catch (IOException ex) { Log.e(TAG, "create failed:", ex); // fall through } catch (IllegalArgumentException ex) { Log.e(TAG, "create failed:", ex); // fall through } catch (SecurityException ex) { Log.e(TAG, "create failed:", ex); // fall through } return null; }