zoukankan      html  css  js  c++  java
  • 11.2 添加视频元数据

        正如在第9章所讨论的那样,除了之前查看的用于图像和音频文件以及元数据的部分之外,Android的MediaStore内容提供器还有一个专门用于视频的部分:MediaStore.Video。

        当通过意图触发Camera应用程序时,返回的新录制视频文件的Uri是一个content://样式的URI,它用于与内容提供器组合使用——在当前情况下是MediaStore。为了添加额外的元数据,可以使用返回的URI来更新在MediaStore中的视频记录。

        像任何内容提供器一样,可以使用从上下文中获得的ContentResolver对象上的update方法,同时传入content://样式的URI以及ContentValues对象形式的新数据。因为已经有一个指向特定记录的URI,所以不需要为最后两个参数——SQL样式的WHERE子句以及WHERE子句参数——指定任何内容。

       ContentValues对象包含名/值对,其中名称是MediaStore.Video的特定列名。可能的名称作为MediaStore.Video.Media中的常量列出,其中大多数都继承自android.provider.BaseColumn、MediaStore.MediaColumn以及MediaStore.Video.videoColumn。

    1             ContentValues values=new ContentValues(1);
    2             values.put(MediaStore.MediaColumns.TITLE, titleEditText.getText().toString().trim());
    3             int result=getContentResolver().update(videoFileUri, values, null, null);

        下面是上述VideoCaptureIntent示例的更新,它允许用户将一个标题与新捕获的视频相关联。

     1 package com.nthm.androidtestActivity;
     2 
     3 import com.nthm.androidtest.R;
     4 import android.app.Activity;
     5 import android.content.ContentValues;
     6 import android.content.Intent;
     7 import android.net.Uri;
     8 import android.os.Bundle;
     9 import android.provider.MediaStore;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 import android.widget.EditText;
    14 
    15 public class VideoCaptureIntent extends Activity implements OnClickListener {
    16     public static int VIDEO_CAPTURE=1;
    17     private Button captureVideoButton;
    18     private Button playVideoButton;
    19     private Button saveVideoButton;

        在这个版本中会有一个EditText对象,允许用户输入视频的标题。

    1     private EditText titleEditText;
    2     private Uri videoFileUri;
    3     @Override
    4     protected void onCreate(Bundle savedInstanceState) {
    5         super.onCreate(savedInstanceState);
    6         setContentView(R.layout.videocaptureitent);
    7         captureVideoButton=(Button) findViewById(R.id.CaptureVideoButton);
    8         playVideoButton=(Button) findViewById(R.id.PlayVideoButton);

        还将有一个saveVideoButton按钮,当按下该按钮时将触发在MediaStore中更新记录的过程。

     1         saveVideoButton=(Button) findViewById(R.id.SaveVideoButton);
     2         titleEditText=(EditText) findViewById(R.id.TitleEditText);
     3         
     4         playVideoButton.setOnClickListener(this);
     5         captureVideoButton.setOnClickListener(this);
     6         saveVideoButton.setOnClickListener(this);
     7         
     8         playVideoButton.setEnabled(false);
     9         saveVideoButton.setEnabled(false);
    10         titleEditText.setEnabled(false);
    11     }

        当按下任何按钮时都会触发的onClick方法将执行大部分的工作。当按下captureVideoButton时,将通过意图触发内置的Camera应用程序。当按下playVideoButton时,将通过意图触发内置的媒体播放器应用程序(而不是与之前一样使用的VideoView)。最后,当按下saveVideoButton时,更新视频文件的MediaStore记录。

    1     @Override
    2     public void onClick(View v) {
    3         if(v==captureVideoButton){
    4             Intent captureVideoIntent=new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
    5             startActivityForResult(captureVideoIntent, VIDEO_CAPTURE);
    6         }else if(v==playVideoButton){
    7             Intent playVideoIntent=new Intent(Intent.ACTION_VIEW, videoFileUri);
    8             startActivity(playVideoIntent);
    9         }else if(v==saveVideoButton){

        首先,创建一个ContentValues对象,并以用户在EditText对象中指定的文本对他进行填充。

    1             ContentValues values=new ContentValues(1);
    2             values.put(MediaStore.MediaColumns.TITLE, titleEditText.getText().toString());

        然后,调用ContentResolver对象上的update方法,并传入所捕获的视频的Uri和ContentValues对象。

     1             int result=getContentResolver().update(videoFileUri, values, null, null);
     2             if(result==1){
     3                 //成功
     4             }else{
     5                 //失败
     6             }
     7         }
     8     }
     9 
    10     @Override
    11     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    12         super.onActivityResult(requestCode, resultCode, data);
    13         if(resultCode==RESULT_OK&&requestCode==VIDEO_CAPTURE){
    14             videoFileUri=data.getData();
    15             playVideoButton.setEnabled(true);
    16             saveVideoButton.setEnabled(true);
    17             titleEditText.setEnabled(true);
    18         }
    19     }
    20 }

        下面是由上述活动引用的布局XML。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical"
     5     >
     6  <Button 
     7      android:layout_width="wrap_content"
     8      android:layout_height="wrap_content"
     9      android:id="@+id/CaptureVideoButton"
    10      android:text="Capture Video"/>
    11  <Button 
    12      android:layout_width="wrap_content"
    13      android:layout_height="wrap_content"
    14      android:id="@+id/PlayVideoButton"
    15      android:text="Play Video"/>
    16  <TextView 
    17      android:id="@+id/TitleTextView"
    18      android:layout_width="wrap_content"
    19      android:layout_height="wrap_content"
    20      android:text="Title:"/>
    21  <EditText 
    22      android:id="@+id/TitleEditText"
    23      android:layout_width="wrap_content"
    24      android:layout_height="wrap_content"
    25      android:text=""/>
    26  <Button 
    27      android:layout_width="wrap_content"
    28      android:layout_height="wrap_content"
    29      android:id="@+id/SaveVideoButton"
    30      android:text="Save Metadata"/>
    31 </LinearLayout>

         之前的示例表明在Android中使用视频的许多方面非常的简单,尤其是在使用意图进行视频捕获和依靠MediaStore处理元数据时。

         应该注意的是,当在MediaStore中更新元数据时,不会更细视频文件本身的数据,相反,它只是更新在MediaStore中关于该视频的记录。Android SDK不提供内置的类来直接修改媒体文件的元数据。

       

  • 相关阅读:
    Spyder的汉化
    Python,Pycharm,Anaconda等的关系与安装过程~为初学者跳过各种坑
    好了,我的第一篇博客!
    Xcode 最低要求和支持的 SDK
    python连接hive (安装impyla)的采坑之旅
    java泛型(泛型接口、泛型类、泛型方法)
    oracle命令查看表结构及表索引
    Linux环境下安装、配置Nginx1.14.2(CentOS Linux release 7.6.1810)
    Caffe入门随笔
    Gradient Boosting算法简介
  • 原文地址:https://www.cnblogs.com/ZSS-Android/p/3959147.html
Copyright © 2011-2022 走看看