ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidsystemimpl/player/AudioStoreThread.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.androidsystemimpl.player; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.os.Handler; |
| TMBOY | 45:2aa9f933c8d2 | 19 | |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.util.FileUtil; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | import org.json.JSONException; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import org.json.JSONObject; |
| TMBOY | 45:2aa9f933c8d2 | 25 | |
| TMBOY | 45:2aa9f933c8d2 | 26 | import java.io.File; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import java.io.FileNotFoundException; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import java.io.FileOutputStream; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import java.io.InputStream; |
| TMBOY | 45:2aa9f933c8d2 | 31 | |
| TMBOY | 45:2aa9f933c8d2 | 32 | /** |
| TMBOY | 45:2aa9f933c8d2 | 33 | * 将stream的mp3流保存到文件中 |
| TMBOY | 45:2aa9f933c8d2 | 34 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 35 | * Created by guxiuzhong@baidu.com on 2017/5/17. |
| TMBOY | 45:2aa9f933c8d2 | 36 | */ |
| TMBOY | 45:2aa9f933c8d2 | 37 | public class AudioStoreThread extends Thread { |
| TMBOY | 45:2aa9f933c8d2 | 38 | private static final String TAG = AudioStoreThread.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 39 | private static final int BUFFER_SIZE = 8192; |
| TMBOY | 45:2aa9f933c8d2 | 40 | private volatile boolean mThreadExitFlag; |
| TMBOY | 45:2aa9f933c8d2 | 41 | private InputStream mInputStream; |
| TMBOY | 45:2aa9f933c8d2 | 42 | private Handler mHandler = new Handler(); |
| TMBOY | 45:2aa9f933c8d2 | 43 | |
| TMBOY | 45:2aa9f933c8d2 | 44 | private File file; |
| TMBOY | 45:2aa9f933c8d2 | 45 | private File completedFile; |
| TMBOY | 45:2aa9f933c8d2 | 46 | private FileOutputStream mOutputStream; |
| TMBOY | 45:2aa9f933c8d2 | 47 | |
| TMBOY | 45:2aa9f933c8d2 | 48 | public AudioStoreThread(InputStream is) { |
| TMBOY | 45:2aa9f933c8d2 | 49 | this.mInputStream = is; |
| TMBOY | 45:2aa9f933c8d2 | 50 | } |
| TMBOY | 45:2aa9f933c8d2 | 51 | |
| TMBOY | 45:2aa9f933c8d2 | 52 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 53 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 54 | super.run(); |
| TMBOY | 45:2aa9f933c8d2 | 55 | try { |
| TMBOY | 45:2aa9f933c8d2 | 56 | file = FileUtil.getSpeakFile(); |
| TMBOY | 45:2aa9f933c8d2 | 57 | if (file != null) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | LogUtil.d(TAG, "AudioStoreThread file: " + file.getAbsolutePath()); |
| TMBOY | 45:2aa9f933c8d2 | 59 | this.mOutputStream = new FileOutputStream(file); |
| TMBOY | 45:2aa9f933c8d2 | 60 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 61 | if (mOnDownListener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 62 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 63 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 64 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 65 | try { |
| TMBOY | 45:2aa9f933c8d2 | 66 | mOnDownListener.onDownError( |
| TMBOY | 45:2aa9f933c8d2 | 67 | new JSONObject("AudioStoreThread create temp file failed!")); |
| TMBOY | 45:2aa9f933c8d2 | 68 | } catch (JSONException e1) { |
| TMBOY | 45:2aa9f933c8d2 | 69 | e1.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 70 | } |
| TMBOY | 45:2aa9f933c8d2 | 71 | } |
| TMBOY | 45:2aa9f933c8d2 | 72 | }); |
| TMBOY | 45:2aa9f933c8d2 | 73 | } |
| TMBOY | 45:2aa9f933c8d2 | 74 | } |
| TMBOY | 45:2aa9f933c8d2 | 75 | } catch (FileNotFoundException e) { |
| TMBOY | 45:2aa9f933c8d2 | 76 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 77 | LogUtil.d(TAG, "AudioStoreThread FileNotFoundException "); |
| TMBOY | 45:2aa9f933c8d2 | 78 | } |
| TMBOY | 45:2aa9f933c8d2 | 79 | if (mOnDownListener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 80 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 81 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 82 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 83 | mOnDownListener.onDownStart(); |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | }); |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
| TMBOY | 45:2aa9f933c8d2 | 87 | byte[] audioData = new byte[BUFFER_SIZE]; |
| TMBOY | 45:2aa9f933c8d2 | 88 | int readBytes; |
| TMBOY | 45:2aa9f933c8d2 | 89 | try { |
| TMBOY | 45:2aa9f933c8d2 | 90 | while (!mThreadExitFlag && (readBytes = mInputStream.read(audioData)) != -1) { |
| TMBOY | 45:2aa9f933c8d2 | 91 | LogUtil.d(TAG, "readBytes=" + readBytes); |
| TMBOY | 45:2aa9f933c8d2 | 92 | mOutputStream.write(audioData, 0, readBytes); |
| TMBOY | 45:2aa9f933c8d2 | 93 | } |
| TMBOY | 45:2aa9f933c8d2 | 94 | mOutputStream.flush(); |
| TMBOY | 45:2aa9f933c8d2 | 95 | LogUtil.d(TAG, "AudioStoreThread ok "); |
| TMBOY | 45:2aa9f933c8d2 | 96 | } catch (final IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 97 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 98 | LogUtil.d(TAG, "AudioStoreThread write error ", e); |
| TMBOY | 45:2aa9f933c8d2 | 99 | if (mOnDownListener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 100 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 101 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 102 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 103 | try { |
| TMBOY | 45:2aa9f933c8d2 | 104 | mOnDownListener.onDownError(new JSONObject("AudioStoreThread write error :" |
| TMBOY | 45:2aa9f933c8d2 | 105 | + e.getMessage())); |
| TMBOY | 45:2aa9f933c8d2 | 106 | } catch (JSONException e1) { |
| TMBOY | 45:2aa9f933c8d2 | 107 | e1.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 108 | } |
| TMBOY | 45:2aa9f933c8d2 | 109 | } |
| TMBOY | 45:2aa9f933c8d2 | 110 | }); |
| TMBOY | 45:2aa9f933c8d2 | 111 | } |
| TMBOY | 45:2aa9f933c8d2 | 112 | } finally { |
| TMBOY | 45:2aa9f933c8d2 | 113 | try { |
| TMBOY | 45:2aa9f933c8d2 | 114 | String fileName = file.getName().substring(0, file.getName().length() |
| TMBOY | 45:2aa9f933c8d2 | 115 | - FileUtil.TEMP_POSTFIX.length()); |
| TMBOY | 45:2aa9f933c8d2 | 116 | LogUtil.d(TAG, "AudioStoreThread fileName : " + fileName); |
| TMBOY | 45:2aa9f933c8d2 | 117 | completedFile = new File(file.getParentFile(), fileName); |
| TMBOY | 45:2aa9f933c8d2 | 118 | boolean renamed = file.renameTo(completedFile); |
| TMBOY | 45:2aa9f933c8d2 | 119 | if (!renamed) { |
| TMBOY | 45:2aa9f933c8d2 | 120 | if (mOnDownListener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 121 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 122 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 123 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 124 | try { |
| TMBOY | 45:2aa9f933c8d2 | 125 | mOnDownListener.onDownError(new JSONObject("Error renaming file " |
| TMBOY | 45:2aa9f933c8d2 | 126 | + file |
| TMBOY | 45:2aa9f933c8d2 | 127 | + "to" |
| TMBOY | 45:2aa9f933c8d2 | 128 | + completedFile |
| TMBOY | 45:2aa9f933c8d2 | 129 | + " for completion!")); |
| TMBOY | 45:2aa9f933c8d2 | 130 | } catch (JSONException e) { |
| TMBOY | 45:2aa9f933c8d2 | 131 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 132 | } |
| TMBOY | 45:2aa9f933c8d2 | 133 | } |
| TMBOY | 45:2aa9f933c8d2 | 134 | }); |
| TMBOY | 45:2aa9f933c8d2 | 135 | } |
| TMBOY | 45:2aa9f933c8d2 | 136 | } |
| TMBOY | 45:2aa9f933c8d2 | 137 | mOutputStream.close(); |
| TMBOY | 45:2aa9f933c8d2 | 138 | mInputStream.close(); |
| TMBOY | 45:2aa9f933c8d2 | 139 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 140 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 141 | } |
| TMBOY | 45:2aa9f933c8d2 | 142 | if (mOnDownListener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 143 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 144 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 145 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 146 | LogUtil.d(TAG, "completedFile path: " + completedFile.getAbsolutePath()); |
| TMBOY | 45:2aa9f933c8d2 | 147 | mOnDownListener.onDownComplete(completedFile.getAbsolutePath()); |
| TMBOY | 45:2aa9f933c8d2 | 148 | } |
| TMBOY | 45:2aa9f933c8d2 | 149 | }); |
| TMBOY | 45:2aa9f933c8d2 | 150 | } |
| TMBOY | 45:2aa9f933c8d2 | 151 | } |
| TMBOY | 45:2aa9f933c8d2 | 152 | } |
| TMBOY | 45:2aa9f933c8d2 | 153 | |
| TMBOY | 45:2aa9f933c8d2 | 154 | /** |
| TMBOY | 45:2aa9f933c8d2 | 155 | * 停止保存/下载 |
| TMBOY | 45:2aa9f933c8d2 | 156 | */ |
| TMBOY | 45:2aa9f933c8d2 | 157 | public void stopDown() { |
| TMBOY | 45:2aa9f933c8d2 | 158 | LogUtil.d(TAG, "stopDown"); |
| TMBOY | 45:2aa9f933c8d2 | 159 | mThreadExitFlag = true; |
| TMBOY | 45:2aa9f933c8d2 | 160 | mHandler.removeCallbacksAndMessages(null); |
| TMBOY | 45:2aa9f933c8d2 | 161 | } |
| TMBOY | 45:2aa9f933c8d2 | 162 | |
| TMBOY | 45:2aa9f933c8d2 | 163 | /** |
| TMBOY | 45:2aa9f933c8d2 | 164 | * 删除文件 |
| TMBOY | 45:2aa9f933c8d2 | 165 | * |
| TMBOY | 45:2aa9f933c8d2 | 166 | * @return 是否删除成功 |
| TMBOY | 45:2aa9f933c8d2 | 167 | */ |
| TMBOY | 45:2aa9f933c8d2 | 168 | public boolean delDownFile() { |
| TMBOY | 45:2aa9f933c8d2 | 169 | LogUtil.d(TAG, "delDownFile"); |
| TMBOY | 45:2aa9f933c8d2 | 170 | if (completedFile != null && completedFile.exists()) { |
| TMBOY | 45:2aa9f933c8d2 | 171 | return completedFile.delete(); |
| TMBOY | 45:2aa9f933c8d2 | 172 | } |
| TMBOY | 45:2aa9f933c8d2 | 173 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 174 | } |
| TMBOY | 45:2aa9f933c8d2 | 175 | |
| TMBOY | 45:2aa9f933c8d2 | 176 | public static class SimpleOnDownListener implements OnDownListener { |
| TMBOY | 45:2aa9f933c8d2 | 177 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 178 | public void onDownStart() { |
| TMBOY | 45:2aa9f933c8d2 | 179 | } |
| TMBOY | 45:2aa9f933c8d2 | 180 | |
| TMBOY | 45:2aa9f933c8d2 | 181 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 182 | public void onDownComplete(String path) { |
| TMBOY | 45:2aa9f933c8d2 | 183 | } |
| TMBOY | 45:2aa9f933c8d2 | 184 | |
| TMBOY | 45:2aa9f933c8d2 | 185 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 186 | public void onDownError(JSONObject jsonObject) { |
| TMBOY | 45:2aa9f933c8d2 | 187 | } |
| TMBOY | 45:2aa9f933c8d2 | 188 | } |
| TMBOY | 45:2aa9f933c8d2 | 189 | |
| TMBOY | 45:2aa9f933c8d2 | 190 | public OnDownListener mOnDownListener; |
| TMBOY | 45:2aa9f933c8d2 | 191 | |
| TMBOY | 45:2aa9f933c8d2 | 192 | public void setOnDownListener(OnDownListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 193 | this.mOnDownListener = listener; |
| TMBOY | 45:2aa9f933c8d2 | 194 | } |
| TMBOY | 45:2aa9f933c8d2 | 195 | |
| TMBOY | 45:2aa9f933c8d2 | 196 | /** |
| TMBOY | 45:2aa9f933c8d2 | 197 | * 流保存回调接口 |
| TMBOY | 45:2aa9f933c8d2 | 198 | */ |
| TMBOY | 45:2aa9f933c8d2 | 199 | public interface OnDownListener { |
| TMBOY | 45:2aa9f933c8d2 | 200 | void onDownStart(); |
| TMBOY | 45:2aa9f933c8d2 | 201 | |
| TMBOY | 45:2aa9f933c8d2 | 202 | void onDownComplete(String path); |
| TMBOY | 45:2aa9f933c8d2 | 203 | |
| TMBOY | 45:2aa9f933c8d2 | 204 | void onDownError(JSONObject jsonObject); |
| TMBOY | 45:2aa9f933c8d2 | 205 | } |
| TMBOY | 45:2aa9f933c8d2 | 206 | } |
