• mediaPlayer MediaPlayer 读取 字节数组


    经过数天的研究和努力,我发现了解决方案,可能有助于节省别人的时间。这是我的答案。我改变了上面的代码逻辑,像这样

    现在发生了什么,我能够成功地加密文件并将其保存在SD卡中,然后解密它播放。其他人不能播放音频

    public class Main2Activity extends AppCompatActivity { 
    
        private String encryptedFileName = "encrypted_Audio.mp3"; 
        private static String algorithm = "AES"; 
        static SecretKey yourKey = null; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main2); 
    
         //saveFile("Hello From CoderzHeaven asaksjalksjals"); 
         try { 
          saveFile(getAudioFile()); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
         decodeFile(); 
    
        } 
    
        public static SecretKey generateKey(char[] passphraseOrPin, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { 
         // Number of PBKDF2 hardening rounds to use. Larger values increase 
         // computation time. You should select a value that causes computation 
         // to take >100ms. 
         final int iterations = 1000; 
    
         // Generate a 256-bit key 
         final int outputKeyLength = 256; 
    
         SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
         KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength); 
         SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); 
         return secretKey; 
        } 
    
        public static SecretKey generateKey() throws NoSuchAlgorithmException { 
         // Generate a 256-bit key 
         final int outputKeyLength = 256; 
         SecureRandom secureRandom = new SecureRandom(); 
         // Do *not* seed secureRandom! Automatically seeded from system entropy. 
         KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); 
         keyGenerator.init(outputKeyLength, secureRandom); 
         yourKey = keyGenerator.generateKey(); 
         return yourKey; 
        } 
    
        public static byte[] encodeFile(SecretKey yourKey, byte[] fileData) 
          throws Exception { 
         byte[] encrypted = null; 
         byte[] data = yourKey.getEncoded(); 
         SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length, algorithm); 
         Cipher cipher = Cipher.getInstance(algorithm); 
         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
           new byte[cipher.getBlockSize()])); 
         encrypted = cipher.doFinal(fileData); 
         return encrypted; 
        } 
    
        public static byte[] decodeFile(SecretKey yourKey, byte[] fileData) 
          throws Exception { 
         byte[] decrypted = null; 
         Cipher cipher = Cipher.getInstance(algorithm); 
         cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(new byte[cipher.getBlockSize()])); 
         decrypted = cipher.doFinal(fileData); 
         return decrypted; 
        } 
    
        void saveFile(byte[] stringToSave) { 
         try { 
          File file = new File(Environment.getExternalStorageDirectory() + File.separator, encryptedFileName); 
    
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); 
          yourKey = generateKey(); 
          byte[] filesBytes = encodeFile(yourKey, stringToSave); 
          bos.write(filesBytes); 
          bos.flush(); 
          bos.close(); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
    
        void decodeFile() { 
    
         try { 
          byte[] decodedData = decodeFile(yourKey, readFile()); 
          // String str = new String(decodedData); 
          //System.out.println("DECODED FILE CONTENTS : " + str); 
          playMp3(decodedData); 
    
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
    
        public byte[] readFile() { 
         byte[] contents = null; 
    
         File file = new File(Environment.getExternalStorageDirectory() 
           + File.separator, encryptedFileName); 
         int size = (int) file.length(); 
         contents = new byte[size]; 
         try { 
          BufferedInputStream buf = new BufferedInputStream(
            new FileInputStream(file)); 
          try { 
           buf.read(contents); 
           buf.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
         return contents; 
        } 
    
        public byte[] getAudioFile() throws FileNotFoundException 
        { 
         byte[] audio_data = null; 
         byte[] inarry = null; 
         AssetManager am = getAssets(); 
         try { 
          InputStream is = am.open("Sleep Away.mp3"); // use recorded file instead of getting file from assets folder. 
          int length = is.available(); 
          audio_data = new byte[length]; 
          int bytesRead; 
          ByteArrayOutputStream output = new ByteArrayOutputStream(); 
          while ((bytesRead = is.read(audio_data)) != -1) { 
           output.write(audio_data, 0, bytesRead); 
          } 
          inarry = output.toByteArray(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         return inarry; 
    
        } 
    
        private void playMp3(byte[] mp3SoundByteArray) { 
    
         try { 
          // create temp file that will hold byte array 
          File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir()); 
          tempMp3.deleteOnExit(); 
          FileOutputStream fos = new FileOutputStream(tempMp3); 
          fos.write(mp3SoundByteArray); 
          fos.close(); 
          // Tried reusing instance of media player 
          // but that resulted in system crashes... 
          MediaPlayer mediaPlayer = new MediaPlayer(); 
          FileInputStream fis = new FileInputStream(tempMp3); 
          mediaPlayer.setDataSource(fis.getFD()); 
          mediaPlayer.prepare(); 
          mediaPlayer.start(); 
         } catch (IOException ex) { 
          ex.printStackTrace(); 
    
         } 
    
        } 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
  • 相关阅读:
    MyBaits-Plus中@TableField和@TableId用法
    小程序源码:全网独家小程序版本独立微信社群人脉系统社群空间站-多玩法安装简单
    R语言使用ggpubr包的desc_statby函数计算不同分组的描述性统计信息、分组样本数、最小值、最大值、中位数、均值、IQR、mad、sd等
    简单SQL与单行函数
    Python中的异常处理3-1
    element-plus 表格-合并单元格
    【附源码】计算机毕业设计java中学网站设计与实现设计与实现
    自定义MVC原理
    基于springboot信用分析管理系统设计与实现。
    LVGL | 1.LVGL PC模拟器之CodeBlocks
  • 原文地址:https://blog.csdn.net/a518618718/article/details/132898086