音乐播放器的乱码问题!!

黑白_hb 2016-05-21 10:20:16
最近在做一个播放器玩玩。这个乱码的问题得不到解决。我是用的MediaStore。

这是我的代码。

ContentResolver cr = c.getContentResolver();
Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

if (cursor != null && cursor.getCount() > 0) {

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {

long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
// 歌曲名
String title = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));

// 歌手名
String singer = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
// 歌曲长度
int time = cursor
.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
// 歌曲文件名字
String name = cursor
.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

String suffix = name
.substring(name.length() - 4, name.length());
// 歌曲文件的全路径
String url = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));


File file = new File(url);
BufferedReader reader;
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);
in.reset();

if (first3bytes[0] == (byte) 0xEF
&& first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {
charset = "utf-8";

} else if (first3bytes[0] == (byte) 0xFF

&& first3bytes[1] == (byte) 0xFE) {
charset = "unicode";
Toast.makeText(c, charset, 1).show();
} else if (first3bytes[0] == (byte) 0xFE

&& first3bytes[1] == (byte) 0xFF) {
charset = "utf-16be";
} else if (first3bytes[0] == (byte) 0xFF

&& first3bytes[1] == (byte) 0xFF) {
charset = "utf-16le";
} else {
charset = "GBK";
}

} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 专辑名
String album = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
try {

singer = new String(singer.getBytes(), charset);
title = new String(title.getBytes(), charset);
album = new String(album.getBytes(), charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long albumid = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

if (url.endsWith(".mp3") || url.endsWith(".MP3")) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", id);
map.put("TITLE", title);
map.put("SINGER", singer);
map.put("TIME", time);
map.put("NAME", name);
map.put("SUFFIX", suffix);
map.put("URL", url);
map.put("ALBUM", album);
map.put("ALBUMID", albumid);
musicdata.add(map);
}
}

在网上找的几种方式都试过。有作用,但感觉都不彻底。还有的就是用播放器转什么的。那个太麻烦。有没有简单有效的方法。
...全文
329 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
AS_MSDN 2016-05-24
  • 打赏
  • 举报
回复
乱码,不用考虑很多,肯定是编码格式出现问题了,获取你自己手机上的音乐,你不管它的编码格式试一试,或者用UTF-8
afunx 2016-05-24
  • 打赏
  • 举报
回复
那这个你要和Server端获取相关API,根据协议来调试了。
黑白_hb 2016-05-24
  • 打赏
  • 举报
回复
引用 7 楼 afunx 的回复:
代码里有解析格式的地方啊,first3bytes[0]这一位来解析charset。 try { // 加打印 System.out.println("charset:"+charset); singer = new String(singer.getBytes(), charset); title = new String(title.getBytes(), charset); album = new String(album.getBytes(), charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } 我觉得可能是未解析到正确的charset
应该是。获得的全是GBK。 这个我早知道,就是不知道解析的哪一步错了。
afunx 2016-05-23
  • 打赏
  • 举报
回复
代码里有解析格式的地方啊,first3bytes[0]这一位来解析charset。 try { // 加打印 System.out.println("charset:"+charset); singer = new String(singer.getBytes(), charset); title = new String(title.getBytes(), charset); album = new String(album.getBytes(), charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } 我觉得可能是未解析到正确的charset
黑白_hb 2016-05-23
  • 打赏
  • 举报
回复
引用 5 楼 yueqinglkong 的回复:
你的ide 编码格式没对
ide是什么呢。应该在哪里改,求教
蒲锦_up 2016-05-23
  • 打赏
  • 举报
回复
你的ide 编码格式没对
黑白_hb 2016-05-23
  • 打赏
  • 举报
回复


打印出来是这个
afunx 2016-05-22
  • 打赏
  • 举报
回复
引用 2 楼 kxltsuperr 的回复:
乱码问题一般是GBK UNICODE什么的问题
+1 应该就是编码问题。如果楼主不知道的话,可以添加打印,把title以十六进制的形式打印出来,再看看符合何种方式的编码。 除此之外,一般在HTTP返回中,都会有结果是何种编码方式的Header的。
小小爬虾 2016-05-21
  • 打赏
  • 举报
回复
乱码问题一般是GBK UNICODE什么的问题
黑白_hb 2016-05-21
  • 打赏
  • 举报
回复
没人吗。。。。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧