笔记69 | Android获取MP3文件的ID3信息工具类

代码块

HelloJni .java

/* =  * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.hellojni;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.CharArrayReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import java.util.Scanner;import android.app.Activity;import android.app.ActivityManager;import android.app.Instrumentation;import android.app.NotificationManager;import android.app.ActivityManager.RunningTaskInfo;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.SharedPreferences;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.content.pm.ResolveInfo;import android.content.res.AssetManager;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory; import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.text.format.DateFormat;import android.util.DisplayMetrics;import android.util.Log;import android.view.Display;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.Surface;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import android.location.LocationManager;import android.net.Uri;import android.net.wifi.WifiManager;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.provider.Settings;public class HelloJni //extends Activity668842{     static {         System.loadLibrary("hello-jni");    }    public static HelloJni pThis;    public native byte[]  getID3info(String fileName,int index);    public boolean isChinese(char c) {        return c >= 0x4E00 && c <= 0x9FA5;    }    public boolean isChinese(String str) {        if (str == null)            return false;        for (char c : str.toCharArray()) {            if (isChinese(c))                return true;         }        return false;    }    public String getID3(String fileName,int index)    {        byte[] get_arr = getID3info(fileName,index);        if(get_arr == null)  return "";        int arr_len = get_arr[1];        Log.i("FDebug","getID3----------------textEncoding="+get_arr[0] + " textSize="+get_arr[1]);        if(arr_len < 1)  return "";        byte[] info_arr = new byte[arr_len];          for(int i=0;i<arr_len;i++)        {            info_arr[i] = get_arr[5+i];        }        if(info_arr != null)        {                       String outStr = "";            for(int i=0;i<info_arr.length;i++)            {                               outStr += " "+Integer.toHexString(info_arr[i]&0xff);            }        //  Log.i("FDebug","getID3----------------outStr="+outStr);            String str = null;            String info = null;            try {                if(get_arr[0] == 0)                {                    str = new String(info_arr,"ISO_8859_1");                    info = new String(str.getBytes("ISO_8859_1"),"gb2312");                     Log.i("FDebug","getID3----------------str="+str);                                               Log.i("FDebug","getID3----------------info="+ info);                    if(isChinese(info))  return info;                }                if(get_arr[0] == 1)                {                    str = new String(info_arr,"UTF-16");                    info = new String(str.getBytes("UTF-16"),"gb2312");                    Log.i("FDebug","getID3----------------str="+str);                                               Log.i("FDebug","getID3----------------info="+ info);                    if(isChinese(info))  return info;                   }                if(get_arr[0] == 2)                {                    str = new String(info_arr,"UTF-16");                    info = new String(str.getBytes("UTF-16"),"gb2312");                     Log.i("FDebug","getID3----------------str="+str);                                               Log.i("FDebug","getID3----------------info="+ info);                    if(isChinese(info))  return info;                   }                if(get_arr[0] == 3)                {                    str = new String(info_arr,"UTF-8");                    //  info = new String(str.getBytes("UTF-8"),"gb2312");                      Log.i("FDebug","getID3----------------str="+str);                                               Log.i("FDebug","getID3----------------info="+ info);                    if(isChinese(info))  return info;                   }                return str;            } catch (UnsupportedEncodingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }               return "";    }}

获取:

public String g_Title = "";    public String g_Artist = "";    public String g_Album = "";    public String g_Year = "";    public void testID3(String fileName)    {        g_Title = "";        g_Artist = "";        g_Album = "";        HelloJni f = new HelloJni();        g_Title = f.getID3(fileName,0);        g_Artist = f.getID3(fileName,1);        g_Album = f.getID3(fileName,2);    }

原文发布于微信公众号 - 项勇(xiangy_life)

原文发表时间:2018-04-28

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏李蔚蓬的专栏

Binary XML file line #19: Attempt to invoke virtual method 'boolean java.lang.String.equals(java....

APP中需要实现LayoutInflater布局加载器动态加载布局,然而开启程序一运行就闪退。。。

581
来自专栏刘晓杰

Glide生命周期管理

3598
来自专栏Android工程师的修仙之旅

自己动手写Android插件化框架

本文旨在通过两个实例直观的说明插件的实现原理以加深对插件内开发的理解,因此不会深入探讨背景和原理,代码也尽量专注于核心逻辑。

3832
来自专栏上善若水

032android初级篇之Timer的使用及获取栈顶包名

android 5.0 之后官方屏蔽了获取栈顶信息的api,如下的方法在大多数机器上可用:

1194
来自专栏吴小龙同學

Android zxing

二维码一般在项目都会用到,今天分享下我开发二维码的一些心得。 zxing介绍 github地址:https://github.com/zxing/zxin...

3698
来自专栏向治洪

android电话拦截

其实大家可以下载 xxx卫士看下,它设置来电拒接模式后,都是会启动设置MMI指令的界面。然后再去“设置->通话设置->来电转接”,看看 “占线时转接” 设置好的...

4259
来自专栏程序员宝库

Android屏幕截图,View截图(干货)

391
来自专栏项勇

[Android笔记7]之通过DatePickerDialog,TimePickerDialog调用系统时间设置

2763
来自专栏码匠的流水账

聊聊springcloud的featuresEndpoint

spring-cloud-commons-2.0.0.RC1-sources.jar!/org/springframework/cloud/client/Com...

531
来自专栏李蔚蓬的专栏

NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet...

NoSuchMethodException: <init> [class android.content.Context, interface android....

1362

扫码关注云+社区