本文整理汇总了Java中android.content.res.AssetFileDescriptor.getStartOffset方法的典型用法代码示例。如果您正苦于以下问题:Java AssetFileDescriptor.getStartOffset方法的具体用法?Java AssetFileDescriptor.getStartOffset怎么用?Java AssetFileDescriptor.getStartOffset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.res.AssetFileDescriptor
的用法示例。
在下文中一共展示了AssetFileDescriptor.getStartOffset方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: open
import android.content.res.AssetFileDescriptor; //导入方法依赖的package包/类
@CalledByNative
public static long[] open(Context context, String fileName) {
AssetFileDescriptor afd = null;
try {
AssetManager manager = context.getAssets();
afd = manager.openNonAssetFd(fileName);
return new long[] { afd.getParcelFileDescriptor().detachFd(),
afd.getStartOffset(),
afd.getLength() };
} catch (IOException e) {
// As a general rule there's no point logging here because the caller should handle
// receiving an fd of -1 sensibly, and the log message is either mirrored later, or
// unwanted (in the case where a missing file is expected), or wanted but will be
// ignored, as most non-fatal logs are.
// It makes sense to log here when the file exists, but is unable to be opened as an fd
// because (for example) it is unexpectedly compressed in an apk. In that case, the log
// message might save someone some time working out what has gone wrong.
// For that reason, we only suppress the message when the exception message doesn't look
// informative (Android framework passes the filename as the message on actual file not
// found, and the empty string also wouldn't give any useful information for debugging).
if (!e.getMessage().equals("") && !e.getMessage().equals(fileName)) {
Log.e(LOGTAG, "Error while loading asset " + fileName + ": " + e);
}
return new long[] {-1, -1, -1};
} finally {
try {
if (afd != null) {
afd.close();
}
} catch (IOException e2) {
Log.e(LOGTAG, "Unable to close AssetFileDescriptor", e2);
}
}
}
示例2: loadMediaPlayer
import android.content.res.AssetFileDescriptor; //导入方法依赖的package包/类
/**
* Loads the audio or video specified by mediaPath into the given
* MediaPlayer.
*
* @param mediaPlayer the MediaPlayer
* @param form the Form
* @param mediaPath the path to the media
*/
public static void loadMediaPlayer(MediaPlayer mediaPlayer, Form form, String mediaPath)
throws IOException {
MediaSource mediaSource = determineMediaSource(form, mediaPath);
switch (mediaSource) {
case ASSET:
AssetFileDescriptor afd = getAssetsIgnoreCaseAfd(form,mediaPath);
try {
FileDescriptor fd = afd.getFileDescriptor();
long offset = afd.getStartOffset();
long length = afd.getLength();
mediaPlayer.setDataSource(fd, offset, length);
} finally {
afd.close();
}
return;
case REPL_ASSET:
mediaPlayer.setDataSource(replAssetPath(mediaPath));
return;
case SDCARD:
mediaPlayer.setDataSource(mediaPath);
return;
case FILE_URL:
mediaPlayer.setDataSource(fileUrlToFilePath(mediaPath));
return;
case URL:
// This works both for streaming and non-streaming.
// TODO(halabelson): Think about whether we could get improved
// performance if we did buffering control.
mediaPlayer.setDataSource(mediaPath);
return;
case CONTENT_URI:
mediaPlayer.setDataSource(form, Uri.parse(mediaPath));
return;
case CONTACT_URI:
throw new IOException("Unable to load audio or video for contact " + mediaPath + ".");
}
throw new IOException("Unable to load audio or video " + mediaPath + ".");
}