如果您在本教程中发现任何错误或bug,请发送电子邮件至[email protected]告知我们。请随时发送给我们Github pull request,帮助我们解决任何问题。在本教程中,您将学习如何使用MediaManager与飞机相机SD卡上的文件系统进行交互。在本教程结束时,您将拥有一个应用程序,您可以使用它来预览照片、播放视频、下载或删除文件等。然而,为了让我们的应用程序管理照片和视频,它必须首先能够拍摄和记录它们。幸运的是,通过使用DJI Android UX SDK,您可以使用标准的DJI Go ui轻松实现拍照和录制视频的功能。您可以从这个Github页面下载本教程的最终示例项目。我们使用Mavic Pro和Nexus 5作为例子来做这个演示。有关为iPhone设备定制布局的更多细节,请查看教程的Github示例项目。
对于国内使用的DJI SDK移动应用,需要激活该应用并绑定飞机到用户的DJI账号。
如果应用程序没有激活,飞机不绑定(如果需要的话),或遗留版本的SDK(< 4.1)使用,所有相机生活流将被禁用,和飞行将有限区域的100米直径和30米高度,以确保飞机保持在视线之内。
要了解如何实现此功能,请查看本教程中的应用程序激活和飞机绑定。
现在,在Android Studio中创建一个新项目,打开Android Studio,选择File -> new -> new project来创建一个新项目,命名为‘MediaManagerDemo’。输入您想要的公司域名和软件包名称(这里我们使用“com.dji.mediaManagerDemo”),然后按下Next。将最小SDK版本设置为API 19: Android 4.4 (KitKat)用于“手机和平板”,然后按下Next。然后选择“空活动”并按下Next。最后,保持活动名称为“MainActivity”,布局名称为“activity_main”,按“Finish”键创建项目。
请查看开始与UX SDK教程学习
现在,在com.dji中创建一个名为“DefaultLayoutActivity”的新空活动类。mediaManagerDemo包。将代码替换为以下代码,记住要按照Android Studio的建议导入相关类:
public class DefaultLayoutActivity extends AppCompatActivity implements View.OnClickListener{
private Button mMediaManagerBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_default_layout);
mMediaManagerBtn = (Button)findViewById(R.id.btn_mediaManager);
mMediaManagerBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_mediaManager: {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
}
default:
break;
}
}
}
在上面的代码中,我们创建了一个按钮变量mMediaManagerBtn,并在onCreate()方法中初始化它。此外,实现按钮的onClick()方法并调用startActivity()方法来启动MainActivity。
接下来,继续打开“activity_default_layout”。,并将代码替换为以下代码:
在上面的xml文件中,我们实现了UX SDK的默认布局。要了解更多细节,请查看UX SDK入门教程。
完成以上步骤后,打开color.xml”。,并将内容替换为以下内容:
#3F51B5
#303F9F
#FF4081
#000000
#FFFFFF
#242d34
#00000000
#80000000
此外,打开“style.xml”。,并将内容替换为以下内容:
完成上述步骤后,让我们打开“activity_main”。,并将内容替换为以下内容:
在上面的xml中,我们定义了以下UI元素:
完成上述步骤后,让我们打开“MainActivity.java"文件,并将代码替换为以下代码:
public class MainActivity extends Activity implements View.OnClickListener {
private static final String TAG = MainActivity.class.getName();
private static MainActivity activity;
private Button mBackBtn, mDeleteBtn, mReloadBtn, mDownloadBtn, mStatusBtn;
private Button mPlayBtn, mResumeBtn, mPauseBtn, mStopBtn, mMoveToBtn;
private RecyclerView listView;
private SlidingDrawer mPushDrawerSd;
private ImageView mDisplayImageView;
private TextView mPushTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
void initUI() {
//Init RecyclerView
listView = (RecyclerView) findViewById(R.id.filelistView);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, OrientationHelper.VERTICAL,false);
listView.setLayoutManager(layoutManager);
mPushDrawerSd = (SlidingDrawer)findViewById(R.id.pointing_drawer_sd);
mPushTv = (TextView)findViewById(R.id.pointing_push_tv);
mBackBtn = (Button) findViewById(R.id.back_btn);
mDeleteBtn = (Button) findViewById(R.id.delete_btn);
mDownloadBtn = (Button) findViewById(R.id.download_btn);
mReloadBtn = (Button) findViewById(R.id.reload_btn);
mStatusBtn = (Button) findViewById(R.id.status_btn);
mPlayBtn = (Button) findViewById(R.id.play_btn);
mResumeBtn = (Button) findViewById(R.id.resume_btn);
mPauseBtn = (Button) findViewById(R.id.pause_btn);
mStopBtn = (Button) findViewById(R.id.stop_btn);
mMoveToBtn = (Button) findViewById(R.id.moveTo_btn);
mDisplayImageView = (ImageView) findViewById(R.id.imageView);
mDisplayImageView.setVisibility(View.VISIBLE);
mBackBtn.setOnClickListener(this);
mDeleteBtn.setOnClickListener(this);
mDownloadBtn.setOnClickListener(this);
mReloadBtn.setOnClickListener(this);
mDownloadBtn.setOnClickListener(this);
mStatusBtn.setOnClickListener(this);
mPlayBtn.setOnClickListener(this);
mResumeBtn.setOnClickListener(this);
mPauseBtn.setOnClickListener(this);
mStopBtn.setOnClickListener(this);
mMoveToBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_btn: {
break;
}
case R.id.delete_btn:{
break;
}
case R.id.reload_btn: {
break;
}
case R.id.download_btn: {
break;
}
case R.id.status_btn: {
break;
}
case R.id.play_btn: {
break;
}
case R.id.resume_btn: {
break;
}
case R.id.pause_btn: {
break;
}
case R.id.stop_btn: {
break;
}
case R.id.moveTo_btn: {
break;
}
default:
break;
}
}
}
在上述代码中,我们实现了以下功能:
当您完成以上步骤后,让我们用您在大疆开发者网站上申请的App密钥注册我们的应用程序。如果您不熟悉App Key,请查看Get Started。
此外,关于MApplication、DemoApplication和ConnectionActivity的详细实现,请参阅创建相机应用程序教程和本教程中的示例项目
现在让我们构建并运行该项目,并将其安装到您的Android设备上。如果一切顺利,你应该看到“注册成功”的textView像下面的屏幕.
现在,继续进行“MainActivity.java”文件。首先,在MainActivity类的开头添加以下代码:
private FileListAdapter mListAdapter;
private List mediaFileList = new ArrayList();
private MediaManager mMediaManager;
private MediaManager.FileListState currentFileListState = MediaManager.FileListState.UNKNOWN;
private ProgressDialog mLoadingDialog;
private FetchMediaTaskScheduler scheduler;
private int lastClickViewIndex =-1;
private View lastClickView;
在上面的代码中,我们为FileListAdapter、List、MediaManager、FetchMediaTaskScheduler等创建了变量。
接下来,使用以下方法改进onDestroy()方法:
@Override
protected void onDestroy() {
lastClickView = null;
DemoApplication.getCameraInstance().setMode(SettingsDefinitions.CameraMode.SHOOT_PHOTO, new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError mError) {
if (mError != null){
setResultToToast("Set Shoot Photo Mode Failed" + mError.getDescription());
}
}
});
if (mediaFileList != null) {
mediaFileList.clear();
}
super.onDestroy();
}
在这里,我们实现了以下几点:
//Init FileListAdapter
mListAdapter = new FileListAdapter();
listView.setAdapter(mListAdapter);
//Init Loading Dialog
mLoadingDialog = new ProgressDialog(MainActivity.this);
mLoadingDialog.setMessage("Please wait");
mLoadingDialog.setCanceledOnTouchOutside(false);
mLoadingDialog.setCancelable(false);
在上面的代码中,我们首先初始化mListAdapter并调用可回收视图的setAdapter()方法来将其设置为listView的适配器。然后初始化mLoadingDialog以显示消息。
此外,实现以下两个方法来显示和隐藏mLoadingDialog对话框:
private void showProgressDialog() {
runOnUiThread(new Runnable() {
public void run() {
if (mLoadingDialog != null) {
mLoadingDialog.show();
}
}
});
}
private void hideProgressDialog() {
runOnUiThread(new Runnable() {
public void run() {
if (null != mLoadingDialog && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
}
});
}
最后,继续实施以下方法:
private MediaManager.FileListStateListener updateFileListStateListener = new MediaManager.FileListStateListener() {
@Override
public void onFileListStateChange(MediaManager.FileListState state) {
currentFileListState = state;
}
};
private void initMediaManager() {
if (DemoApplication.getProductInstance() == null) {
mediaFileList.clear();
mListAdapter.notifyDataSetChanged();
DJILog.e(TAG, "Product disconnected");
return;
} else {
if (null != DemoApplication.getCameraInstance() && DemoApplication.getCameraInstance().isMediaDownloadModeSupported()) {
mMediaManager = DemoApplication.getCameraInstance().getMediaManager();
} else if (null != DemoApplication.getCameraInstance()
&& !DemoApplication.getCameraInstance().isMediaDownloadModeSupported()) {
setResultToToast("Media Download Mode not Supported");
}
}
return;
}
在上述代码中,我们实现了以下功能:
一旦我们完成了上述步骤,我们可以开始从相机SD卡获取媒体文件列表,并在listView上显示它们。
实现如下方法:
private void getFileList() {
mMediaManager = DemoApplication.getCameraInstance().getMediaManager();
if (mMediaManager != null) {
if ((currentFileListState == MediaManager.FileListState.SYNCING) || (currentFileListState == MediaManager.FileListState.DELETING)){
DJILog.e(TAG, "Media Manager is busy.");
}else{
mMediaManager.refreshFileListOfStorageLocation(SettingsDefinitions.StorageLocation.SDCARD, new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {
if (null == djiError) {
hideProgressDialog();
//Reset data
if (currentFileListState != MediaManager.FileListState.INCOMPLETE) {
mediaFileList.clear();
lastClickViewIndex = -1;
lastClickView = null;
}
mediaFileList = mMediaManager.getSDCardFileListSnapshot();
Collections.sort(mediaFileList, new Comparator() {
@Override
public int compare(MediaFile lhs, MediaFile rhs) {
if (lhs.getTimeCreated() < rhs.getTimeCreated()) {
return 1;
} else if (lhs.getTimeCreated() > rhs.getTimeCreated()) {
return -1;
}
return 0;
}
});
scheduler.resume(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (error == null) {
getThumbnails();
}
}
});
} else {
hideProgressDialog();
setResultToToast("Get Media File List Failed:" + djiError.getDescription());
}
}
});
}
}
}
private void getThumbnailByIndex(final int index) {
FetchMediaTask task = new FetchMediaTask(mediaFileList.get(index), FetchMediaTaskContent.THUMBNAIL, taskCallback);
scheduler.moveTaskToEnd(task);
}
private void getThumbnails() {
if (mediaFileList.size() <= 0) {
setResultToToast("No File info for downloading thumbnails");
return;
}
for (int i = 0; i < mediaFileList.size(); i++) {
getThumbnailByIndex(i);
}
}
private FetchMediaTask.Callback taskCallback = new FetchMediaTask.Callback() {
@Override
public void onUpdate(MediaFile file, FetchMediaTaskContent option, DJIError error) {
if (null == error) {
if (option == FetchMediaTaskContent.PREVIEW) {
runOnUiThread(new Runnable() {
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
if (option == FetchMediaTaskContent.THUMBNAIL) {
runOnUiThread(new Runnable() {
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
} else {
DJILog.e(TAG, "Fetch Media Task Failed" + error.getDescription());
}
}
};
在上面的代码中,我们实现了以下特性:
if (null != mMediaManager) {
mMediaManager.addUpdateFileListStateListener(this.updateFileListStateListener);
DemoApplication.getCameraInstance().setMode(SettingsDefinitions.CameraMode.MEDIA_DOWNLOAD, new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (error == null) {
DJILog.e(TAG, "Set cameraMode success");
showProgressDialog();
getFileList();
} else {
setResultToToast("Set cameraMode failed");
}
}
});
if (mMediaManager.isVideoPlaybackSupported()) {
DJILog.e(TAG, "Camera support video playback!");
} else {
setResultToToast("Camera does not support video playback!");
}
scheduler = mMediaManager.getScheduler();
}
在上面的代码中,我们实现了以下特性:
在上面的代码中,我们在listView中定义了ItemHolder的布局。在左侧添加一个ImageView,在右侧添加四个textview,如下图所示:
最后,继续实现以下方法在listView中显示获取的媒体文件:
private class ItemHolder extends RecyclerView.ViewHolder {
ImageView thumbnail_img;
TextView file_name;
TextView file_type;
TextView file_size;
TextView file_time;
public ItemHolder(View itemView) {
super(itemView);
this.thumbnail_img = (ImageView) itemView.findViewById(R.id.filethumbnail);
this.file_name = (TextView) itemView.findViewById(R.id.filename);
this.file_type = (TextView) itemView.findViewById(R.id.filetype);
this.file_size = (TextView) itemView.findViewById(R.id.fileSize);
this.file_time = (TextView) itemView.findViewById(R.id.filetime);
}
}
private class FileListAdapter extends RecyclerView.Adapter {
@Override
public int getItemCount() {
if (mediaFileList != null) {
return mediaFileList.size();
}
return 0;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.media_info_item, parent, false);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(ItemHolder mItemHolder, final int index) {
final MediaFile mediaFile = mediaFileList.get(index);
if (mediaFile != null) {
if (mediaFile.getMediaType() != MediaFile.MediaType.MOV && mediaFile.getMediaType() != MediaFile.MediaType.MP4) {
mItemHolder.file_time.setVisibility(View.GONE);
} else {
mItemHolder.file_time.setVisibility(View.VISIBLE);
mItemHolder.file_time.setText(mediaFile.getDurationInSeconds() + " s");
}
mItemHolder.file_name.setText(mediaFile.getFileName());
mItemHolder.file_type.setText(mediaFile.getMediaType().name());
mItemHolder.file_size.setText(mediaFile.getFileSize() + " Bytes");
mItemHolder.thumbnail_img.setImageBitmap(mediaFile.getThumbnail());
mItemHolder.thumbnail_img.setOnClickListener(ImgOnClickListener);
mItemHolder.thumbnail_img.setTag(mediaFile);
mItemHolder.itemView.setTag(index);
if (lastClickViewIndex == index) {
mItemHolder.itemView.setSelected(true);
} else {
mItemHolder.itemView.setSelected(false);
}
mItemHolder.itemView.setOnClickListener(itemViewOnClickListener);
}
}
}
private View.OnClickListener itemViewOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
lastClickViewIndex = (int) (v.getTag());
if (lastClickView != null && lastClickView != v) {
lastClickView.setSelected(false);
}
v.setSelected(true);
lastClickView = v;
}
};
private View.OnClickListener ImgOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaFile selectedMedia = (MediaFile) v.getTag();
if (selectedMedia != null && mMediaManager != null) {
addMediaTask(selectedMedia);
}
}
};
private void addMediaTask(final MediaFile mediaFile) {
final FetchMediaTaskScheduler scheduler = mMediaManager.getScheduler();
final FetchMediaTask task =
new FetchMediaTask(mediaFile, FetchMediaTaskContent.PREVIEW, new FetchMediaTask.Callback() {
@Override
public void onUpdate(final MediaFile mediaFile, FetchMediaTaskContent fetchMediaTaskContent, DJIError error) {
if (null == error) {
if (mediaFile.getPreview() != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final Bitmap previewBitmap = mediaFile.getPreview();
mDisplayImageView.setVisibility(View.VISIBLE);
mDisplayImageView.setImageBitmap(previewBitmap);
}
});
} else {
setResultToToast("null bitmap!");
}
} else {
setResultToToast("fetch preview image failed: " + error.getDescription());
}
}
});
scheduler.resume(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (error == null) {
scheduler.moveTaskToNext(task);
} else {
setResultToToast("resume scheduler failed: " + error.getDescription());
}
}
});
}
上面的代码实现了:
完成上述步骤后,继续在MainActivity类的开头添加以下代码:
private ProgressDialog mDownloadDialog;
File destDir = new File(Environment.getExternalStorageDirectory().getPath() + "/MediaManagerDemo/");
private int currentProgress = -1;
在上面的代码中,我们定义了用于媒体文件下载的mDownloadDialog、destDir和currentProgress变量。
在initUI()方法中添加以下代码:
//Init Download Dialog
mDownloadDialog = new ProgressDialog(MainActivity.this);
mDownloadDialog.setTitle("Downloading file");
mDownloadDialog.setIcon(android.R.drawable.ic_dialog_info);
mDownloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDownloadDialog.setCanceledOnTouchOutside(false);
mDownloadDialog.setCancelable(true);
mDownloadDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mMediaManager != null) {
mMediaManager.exitMediaDownloading();
}
}
});
在这里,我们初始化mDownloadDialog并为它配置不同的设置。另外,覆盖OnCancelListener的onCancel()回调方法,并调用MediaManager的exitMediaDownloading()方法,以在按下cancel按钮时停止媒体下载过程。
接下来,实现以下方法来显示和隐藏download mDownloadDialog:
private void ShowDownloadProgressDialog() {
if (mDownloadDialog != null) {
runOnUiThread(new Runnable() {
public void run() {
mDownloadDialog.incrementProgressBy(-mDownloadDialog.getProgress());
mDownloadDialog.show();
}
});
}
}
private void HideDownloadProgressDialog() {
if (null != mDownloadDialog && mDownloadDialog.isShowing()) {
runOnUiThread(new Runnable() {
public void run() {
mDownloadDialog.dismiss();
}
});
}
}
完成以上工作后,继续实现以下两种方法:
private void downloadFileByIndex(final int index){
if ((mediaFileList.get(index).getMediaType() == MediaFile.MediaType.PANORAMA)
|| (mediaFileList.get(index).getMediaType() == MediaFile.MediaType.SHALLOW_FOCUS)) {
return;
}
mediaFileList.get(index).fetchFileData(destDir, null, new DownloadListener() {
@Override
public void onFailure(DJIError error) {
HideDownloadProgressDialog();
setResultToToast("Download File Failed" + error.getDescription());
currentProgress = -1;
}
@Override
public void onProgress(long total, long current) {
}
@Override
public void onRateUpdate(long total, long current, long persize) {
int tmpProgress = (int) (1.0 * current / total * 100);
if (tmpProgress != currentProgress) {
mDownloadDialog.setProgress(tmpProgress);
currentProgress = tmpProgress;
}
}
@Override
public void onStart() {
currentProgress = -1;
ShowDownloadProgressDialog();
}
@Override
public void onSuccess(String filePath) {
HideDownloadProgressDialog();
setResultToToast("Download File Success" + ":" + filePath);
currentProgress = -1;
}
});
}
private void deleteFileByIndex(final int index) {
ArrayList fileToDelete = new ArrayList();
if (mediaFileList.size() > index) {
fileToDelete.add(mediaFileList.get(index));
mMediaManager.deleteFiles(fileToDelete, new CommonCallbacks.CompletionCallbackWithTwoParam, DJICameraError>() {
@Override
public void onSuccess(List x, DJICameraError y) {
DJILog.e(TAG, "Delete file success");
runOnUiThread(new Runnable() {
public void run() {
MediaFile file = mediaFileList.remove(index);
//Reset select view
lastClickViewIndex = -1;
lastClickView = null;
//Update recyclerView
mListAdapter.notifyItemRemoved(index);
}
});
}
@Override
public void onFailure(DJIError error) {
setResultToToast("Delete file failed");
}
});
}
}
在上面的代码中,我们实现了以下特性:
case R.id.delete_btn:{
deleteFileByIndex(lastClickViewIndex);
break;
}
case R.id.reload_btn: {
getFileList();
break;
}
case R.id.download_btn: {
downloadFileByIndex(lastClickViewIndex);
break;
}
有关实现的更多细节,请在Github上查看本教程的示例代码。
现在,我们可以测试到目前为止已经实现的特性。构建并运行该项目,并将其安装到您的Android设备上。如果一切顺利,你应该看到类似于下面的gif动画:
# 完成视频回放
现在,继续实现MediaManager的视频回放功能。在onDestroy()方法中,在lastClickView = null下面插入以下代码;
if (mMediaManager != null) {
mMediaManager.stop(null);
mMediaManager.removeFileListStateCallback(this.updateFileListStateListener);
mMediaManager.removeMediaUpdatedVideoPlaybackStateListener(updatedVideoPlaybackStateListener);
mMediaManager.exitMediaDownloading();
if (scheduler!=null) {
scheduler.removeAllTasks();
}
}
在这里,我们调用MediaManager的stop()方法来停止播放视频。然后调用removeMediaUpdatedVideoPlaybackStateListener()方法来删除侦听器。此外,调用removeFileListStateCallback()方法来删除文件列表状态回调。接下来,调用exitMediaDownloading()方法来退出MEDIA_DOWNLOAD模式并进入SHOOT_PHOTO模式。最后,如果调度器不为空,则调用removeAllTasks()方法来删除所有现有的媒体任务。
接下来,在mMediaManager.addUpdateFileListStateListener(this.updateFileLi)下面插入以下代码:
mMediaManager.addMediaUpdatedVideoPlaybackStateListener(this.updatedVideoPlaybackStateListener);
在上面的代码中,调用addMediaUpdatedVideoPlaybackStateListener()方法,并传递updatedVideoPlaybackStateListener变量来添加用于视频播放状态更新的侦听器。
此外,初始化updatedVideoPlaybackStateListener变量并实现updateStatusTextView()和addLineToSB()方法,如下所示:
private MediaManager.VideoPlaybackStateListener updatedVideoPlaybackStateListener =
new MediaManager.VideoPlaybackStateListener() {
@Override
public void onUpdate(MediaManager.VideoPlaybackState videoPlaybackState) {
updateStatusTextView(videoPlaybackState);
}
};
private void updateStatusTextView(MediaManager.VideoPlaybackState videoPlaybackState) {
final StringBuffer pushInfo = new StringBuffer();
addLineToSB(pushInfo, "Video Playback State", null);
if (videoPlaybackState != null) {
if (videoPlaybackState.getPlayingMediaFile() != null) {
addLineToSB(pushInfo, "media index", videoPlaybackState.getPlayingMediaFile().getIndex());
addLineToSB(pushInfo, "media size", videoPlaybackState.getPlayingMediaFile().getFileSize());
addLineToSB(pushInfo,
"media duration",
videoPlaybackState.getPlayingMediaFile().getDurationInSeconds());
addLineToSB(pushInfo, "media created date", videoPlaybackState.getPlayingMediaFile().getDateCreated());
addLineToSB(pushInfo,
"media orientation",
videoPlaybackState.getPlayingMediaFile().getVideoOrientation());
} else {
addLineToSB(pushInfo, "media index", "None");
}
addLineToSB(pushInfo, "media current position", videoPlaybackState.getPlayingPosition());
addLineToSB(pushInfo, "media current status", videoPlaybackState.getPlaybackStatus());
addLineToSB(pushInfo, "media cached percentage", videoPlaybackState.getCachedPercentage());
addLineToSB(pushInfo, "media cached position", videoPlaybackState.getCachedPosition());
pushInfo.append("\n");
setResultToText(pushInfo.toString());
}
}
private void addLineToSB(StringBuffer sb, String name, Object value) {
if (sb == null) return;
sb.
append((name == null || "".equals(name)) ? "" : name + ": ").
append(value == null ? "" : value + "").
append("\n");
}
private void setResultToText(final String string) {
if (mPushTv == null) {
setResultToToast("Push info tv has not be init...");
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mPushTv.setText(string);
}
});
}
在这里,我们实现了以下功能:
private void playVideo() {
mDisplayImageView.setVisibility(View.INVISIBLE);
MediaFile selectedMediaFile = mediaFileList.get(lastClickViewIndex);
if ((selectedMediaFile.getMediaType() == MediaFile.MediaType.MOV) || (selectedMediaFile.getMediaType() == MediaFile.MediaType.MP4)) {
mMediaManager.playVideoMediaFile(selectedMediaFile, new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null != error) {
setResultToToast("Play Video Failed" + error.getDescription());
} else {
DJILog.e(TAG, "Play Video Success");
}
}
});
}
}
private void moveToPosition(){
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt_input_position, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String ms = userInput.getText().toString();
mMediaManager.moveToPosition(Integer.parseInt(ms),
new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null != error) {
setResultToToast("Move to video position failed" + error.getDescription());
} else {
DJILog.e(TAG, "Move to video position successfully.");
}
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
在这里,我们实现了以下功能:
在上面的代码中,我们创建了一个TextView和一个EditText,如下所示
最后,我们实现status_btn、play_btn、resume_btn、pause_btn、stop_btn、moveTo_btn的onClick()方法,如下图所示:
case R.id.status_btn: {
if (mPushDrawerSd.isOpened()) {
mPushDrawerSd.animateClose();
} else {
mPushDrawerSd.animateOpen();
}
break;
}
case R.id.play_btn: {
playVideo();
break;
}
case R.id.resume_btn: {
mMediaManager.resume(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null != error) {
setResultToToast("Resume Video Failed" + error.getDescription());
} else {
DJILog.e(TAG, "Resume Video Success");
}
}
});
break;
}
case R.id.pause_btn: {
mMediaManager.pause(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null != error) {
setResultToToast("Pause Video Failed" + error.getDescription());
} else {
DJILog.e(TAG, "Pause Video Success");
}
}
});
break;
}
case R.id.stop_btn: {
mMediaManager.stop(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null != error) {
setResultToToast("Stop Video Failed" + error.getDescription());
} else {
DJILog.e(TAG, "Stop Video Success");
}
}
});
break;
}
case R.id.moveTo_btn: {
moveToPosition();
break;
}
在上面的代码中,我们实现了以下特性:
在本教程中,您已经学习了如何使用MediaManager预览照片、播放视频、下载或删除文件,还学习了如何获取和显示视频播放状态信息。通过使用MediaManager,用户可以获得所有多媒体文件的元数据,并可以访问每个单独的多媒体文件。希望你喜欢!