现在的位置: 首页 > 综合 > 正文

从raw中读取MP3文件

2018年02月10日 ⁄ 综合 ⁄ 共 1318字 ⁄ 字号 评论关闭
  1. 读取raw资源文件中的mp3文件,然后通过音乐播放器播放: 
  2.  
  3.         /** 
  4.          * 把mp3文件写入卡 
  5.          *  
  6.          * @param fileName 
  7.          *             输出的文件名(全路径) 
  8.          * @param context 
  9.          *             context对象 
  10.          */ 
  11.         private void writeMP3ToSDcard(String fileName, Context context) { 
  12.                 byte[] buffer = new byte[1024 * 8]; 
  13.                 int read; 
  14.                 BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring)); 
  15.                 try { 
  16.                         BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName)); 
  17.                         while ((read = bin.read(buffer)) > -1) { 
  18.                                 bout.write(buffer, 0, read); 
  19.                         } 
  20.                         bout.flush(); 
  21.                         bout.close(); 
  22.                         bin.close(); 
  23.                 } catch (FileNotFoundException e) { 
  24.                         e.printStackTrace(); 
  25.                 } catch (IOException e) { 
  26.                         e.printStackTrace(); 
  27.                 } 
  28.         } 
  29.  
  30.  
  31. Intent intent = new Intent(); 
  32. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  33. intent.setAction(android.content.Intent.ACTION_VIEW); 
  34. intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路径")),"audio/*"); 
  35. startActivity(intent);

抱歉!评论已关闭.