1、MediaPlayer有时候用静态的create的方法,有时候又使用new MediaPlayer();
这两中用法有什么区别?
查看源码,其实create也调用了new MediaPlayer();
// Note no convenience method to create a MediaPlayer with SurfaceTexture sink.
/**
* Convenience method to create a MediaPlayer for a given resource id.
* On success, {@link #prepare()} will already have been called and must not be called again.
* <p>When done with the MediaPlayer, you should call {@link #release()},
* to free the resources. If not released, too many MediaPlayer instances will
* result in an exception.</p>
*
* @param context the Context to use
* @param resid the raw resource id (<var>R.raw.<something></var>) for
* the resource to use as the datasource
* @return a MediaPlayer object, or null if creation failed
*/
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;
}
从代码上来分析,使用create就是为了简单,使用create了就不需要再调用setDataSource、不需要重复判断参数是否正确等工作。
本质上应该是没有任何区别的。