前段時間在做視頻播放器的時候碰到一個新需求:需要將視頻的地理位置顯示在視頻的詳情里面。第一反應(yīng)就是每一個視頻文件都可以記錄下來一些信息范咨,這些信息里面包含了經(jīng)緯度等等熊楼,就像照片的ExifInterface類一樣峦睡。于是就去查看了MediaRecorder類辣往,便發(fā)現(xiàn)了這個類里面有public void setLocation(float latitude, float longitude)這么個方法可以給錄制的視頻設(shè)置經(jīng)緯度。
/**
* Set and store the geodata (latitude and longitude) in the output file.
* This method should be called before prepare(). The geodata is
* stored in udta box if the output format is OutputFormat.THREE_GPP
* or OutputFormat.MPEG_4, and is ignored for other output formats.
* The geodata is stored according to ISO-6709 standard.
*
* @param latitude latitude in degrees. Its value must be in the
* range [-90, 90].
* @param longitude longitude in degrees. Its value must be in the
* range [-180, 180].
*
* @throws IllegalArgumentException if the given latitude or
* longitude is out of range.
*
*/
public void setLocation(float latitude, float longitude) {
int latitudex10000 = (int) (latitude * 10000 + 0.5);
int longitudex10000 = (int) (longitude * 10000 + 0.5);
if (latitudex10000 > 900000 || latitudex10000 < -900000) {
String msg = "Latitude: " + latitude + " out of range.";
throw new IllegalArgumentException(msg);
}
if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
String msg = "Longitude: " + longitude + " out of range";
throw new IllegalArgumentException(msg);
}
setParameter("param-geotag-latitude=" + latitudex10000);
setParameter("param-geotag-longitude=" + longitudex10000);
}
所以說明視頻文件里面是存儲了經(jīng)緯度的姿鸿,現(xiàn)在的關(guān)鍵就是找到對應(yīng)的API去獲取視頻文件存儲的經(jīng)緯度谆吴。回憶以前獲取視頻的某一幀圖像使用的是MediaMetadataRetriever類苛预,通過這個對象是否也可以獲取一些別的信息呢句狼?創(chuàng)建mediaMetadataRetriever對象后發(fā)現(xiàn)了這么個方法:mediaMetadataRetriever.extractMetadata(int keyCode);看見這個方法名就感覺找到了(提煉出元數(shù)據(jù)),現(xiàn)在還需要一個關(guān)鍵的keyCode热某。于是進入到這個類里面瀏覽源碼腻菇,發(fā)現(xiàn)了一大堆的key:
/**
* The metadata key to retrieve the numeric string describing the
* order of the audio data source on its original recording.
*/
public static final int METADATA_KEY_CD_TRACK_NUMBER = 0;
/**
* The metadata key to retrieve the information about the album title
* of the data source.
*/
public static final int METADATA_KEY_ALBUM = 1;
/**
* The metadata key to retrieve the information about the artist of
* the data source.
*/
public static final int METADATA_KEY_ARTIST = 2;
/**
* The metadata key to retrieve the information about the author of
* the data source.
*/
public static final int METADATA_KEY_AUTHOR = 3;
/**
* The metadata key to retrieve the information about the composer of
* the data source.
*/
public static final int METADATA_KEY_COMPOSER = 4;
/**
* The metadata key to retrieve the date when the data source was created
* or modified.
*/
public static final int METADATA_KEY_DATE = 5;
/**
* The metadata key to retrieve the content type or genre of the data
* source.
*/
public static final int METADATA_KEY_GENRE = 6;
/**
* The metadata key to retrieve the data source title.
*/
public static final int METADATA_KEY_TITLE = 7;
/**
* The metadata key to retrieve the year when the data source was created
* or modified.
*/
public static final int METADATA_KEY_YEAR = 8;
/**
* The metadata key to retrieve the playback duration of the data source.
*/
public static final int METADATA_KEY_DURATION = 9;
/**
* The metadata key to retrieve the number of tracks, such as audio, video,
* text, in the data source, such as a mp4 or 3gpp file.
*/
public static final int METADATA_KEY_NUM_TRACKS = 10;
/**
* The metadata key to retrieve the information of the writer (such as
* lyricist) of the data source.
*/
public static final int METADATA_KEY_WRITER = 11;
/**
* The metadata key to retrieve the mime type of the data source. Some
* example mime types include: "video/mp4", "audio/mp4", "audio/amr-wb",
* etc.
*/
public static final int METADATA_KEY_MIMETYPE = 12;
/**
* The metadata key to retrieve the information about the performers or
* artist associated with the data source.
*/
public static final int METADATA_KEY_ALBUMARTIST = 13;
/**
* The metadata key to retrieve the numberic string that describes which
* part of a set the audio data source comes from.
*/
public static final int METADATA_KEY_DISC_NUMBER = 14;
/**
* The metadata key to retrieve the music album compilation status.
*/
public static final int METADATA_KEY_COMPILATION = 15;
/**
* If this key exists the media contains audio content.
*/
public static final int METADATA_KEY_HAS_AUDIO = 16;
/**
* If this key exists the media contains video content.
*/
public static final int METADATA_KEY_HAS_VIDEO = 17;
/**
* If the media contains video, this key retrieves its width.
*/
public static final int METADATA_KEY_VIDEO_WIDTH = 18;
/**
* If the media contains video, this key retrieves its height.
*/
public static final int METADATA_KEY_VIDEO_HEIGHT = 19;
/**
* This key retrieves the average bitrate (in bits/sec), if available.
*/
public static final int METADATA_KEY_BITRATE = 20;
/**
* This key retrieves the language code of text tracks, if available.
* If multiple text tracks present, the return value will look like:
* "eng:chi"
* @hide
*/
public static final int METADATA_KEY_TIMED_TEXT_LANGUAGES = 21;
/**
* If this key exists the media is drm-protected.
* @hide
*/
public static final int METADATA_KEY_IS_DRM = 22;
/**
* This key retrieves the location information, if available.
* The location should be specified according to ISO-6709 standard, under
* a mp4/3gp box "@xyz". Location with longitude of -90 degrees and latitude
* of 180 degrees will be retrieved as "-90.0000+180.0000", for instance.
*/
public static final int METADATA_KEY_LOCATION = 23;
/**
* This key retrieves the video rotation angle in degrees, if available.
* The video rotation angle may be 0, 90, 180, or 270 degrees.
*/
public static final int METADATA_KEY_VIDEO_ROTATION = 24;
/**
* This key retrieves the original capture framerate, if it's
* available. The capture framerate will be a floating point
* number.
*/
public static final int METADATA_KEY_CAPTURE_FRAMERATE = 25;
這里我所需要的僅僅是:public static final int METADATA_KEY_LOCATION = 23;
/**
* 獲取視頻保存的地理位置信息
*
* @return +22.000+119.999
*/
public static String getVideoLocationInfo(String path) {
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
try {
metadataRetriever.setDataSource(path);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
}
返回的經(jīng)緯度格式:+22.000+119.999,這里需要將經(jīng)度部分和緯度部分分割開來昔馋。
String s = getVideoLocationInfo(videoPath);
if(!TextUtils.isEmpty(s)){
char[]chars=s.toCharArray();
String latitude=null;
String longitude=null;
for(int i=0;i<chars.length;i++){
if((chars[i]=='+'||chars[i]=='-')&&i>0){
latitude=s.substring(0,i);
longitude=s.substring(i,chars.length);
break;
}
}
double dLat=Double.parseDouble(latitude);
double dLon=Double.parseDouble(longitude);
}
最后通過Geocoder解析出經(jīng)緯度對應(yīng)的具體城市位置:
@WorkerThread
private static void getAddress(Activity a, double latitude, double longitude, final TextView localeTxt) {
Geocoder geocoder = new Geocoder(a);
final StringBuilder stringBuilder = new StringBuilder();
try {
//根據(jù)經(jīng)緯度獲取地理位置信息
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String countryName = address.getCountryName();//國家
String adminArea = address.getAdminArea();//省份
String locality = address.getLocality();//市
String subLocality = address.getSubLocality();//區(qū)
String thoroughfare = address.getThoroughfare();//街道
if (!TextUtils.isEmpty(countryName)) {
stringBuilder.append(countryName).append(a.getResources().getString(R.string.divide));
}
if (!TextUtils.isEmpty(adminArea)) {
stringBuilder.append(adminArea).append(a.getResources().getString(R.string.divide));
}
if (!TextUtils.isEmpty(locality)) {
stringBuilder.append(locality).append(a.getResources().getString(R.string.divide));
}
if (!TextUtils.isEmpty(subLocality)) {
stringBuilder.append(subLocality).append(a.getResources().getString(R.string.divide));
}
if (!TextUtils.isEmpty(thoroughfare)) {
stringBuilder.append(thoroughfare).append(a.getResources().getString(R.string.divide));
}
stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(a.getResources().getString(R.string.divide)));
}
} catch (IOException e) {
e.printStackTrace();
a.runOnUiThread(new Runnable() {
@Override
public void run() {
localeTxt.setVisibility(View.GONE);
}
});
}
final String s = stringBuilder.toString();
if (!TextUtils.isEmpty(s)) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
localeTxt.setText(s);
}
});
} else {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
localeTxt.setVisibility(View.GONE);
}
});
}
}