Skip to content

Commit e040f9f

Browse files
author
fagan_zhong
committed
first commit
1 parent 7c13677 commit e040f9f

35 files changed

+1503
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
buildToolsVersion "29.0.2"
6+
defaultConfig {
7+
applicationId "com.zfg.audiodemo"
8+
minSdkVersion 21
9+
targetSdkVersion 28
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
implementation fileTree(dir: 'libs', include: ['*.jar'])
24+
implementation 'com.android.support:appcompat-v7:28.0.0'
25+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
26+
testImplementation 'junit:junit:4.12'
27+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
28+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.zfg.audiodemo;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.zfg.audiodemo", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.zfg.audiodemo">
4+
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/AppTheme">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
</manifest>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package com.zfg.audiodemo;
2+
3+
import android.media.MediaCodec;
4+
import android.media.MediaExtractor;
5+
import android.media.MediaFormat;
6+
import android.os.Environment;
7+
import android.util.Log;
8+
9+
import java.io.File;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.nio.ByteBuffer;
14+
15+
import static com.zfg.audiodemo.AudioEncoder.ENCODER_FILE;
16+
import static com.zfg.audiodemo.MainActivity.FOLDER_NAME;
17+
import static com.zfg.audiodemo.MainActivity.TAG;
18+
19+
/**
20+
* 音频硬解码
21+
*/
22+
public class AudioDecoder {
23+
24+
//用于分离出音频轨道
25+
private MediaExtractor mMediaExtractor;
26+
private MediaCodec mMediaCodec;
27+
private MediaCodec.BufferInfo mBufferInfo;
28+
private File mTargetFile;
29+
private File mPCMFile;
30+
private FileOutputStream mFileOutputStream;
31+
private IDecodeDelegate mIDecodeDelegate;
32+
33+
/**
34+
* AAC格式
35+
*/
36+
private String MINE_TYPE_AAC = "audio/mp4a-latm";
37+
38+
/**
39+
* 解码后的pcm文件
40+
*/
41+
public static final String DECODER_FILE = "decoder_file";
42+
43+
/**
44+
* 初始化解码器
45+
*/
46+
public void initDecoder(IDecodeDelegate iDecodeDelegate) {
47+
48+
mIDecodeDelegate = iDecodeDelegate;
49+
50+
File fileFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + FOLDER_NAME);
51+
String fileFolderPath = fileFolder.getAbsolutePath();
52+
mTargetFile = new File(fileFolderPath + "/" + ENCODER_FILE + ".aac");
53+
if (!mTargetFile.exists()) {
54+
Log.e(TAG, "The source file does not exist!");
55+
return;
56+
}
57+
mPCMFile = new File(fileFolderPath + "/" + DECODER_FILE + ".pcm");
58+
if (!mPCMFile.exists()) {
59+
try {
60+
mPCMFile.createNewFile();
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
try {
66+
mFileOutputStream = new FileOutputStream(mPCMFile.getAbsoluteFile());
67+
} catch (FileNotFoundException e) {
68+
e.printStackTrace();
69+
}
70+
mMediaExtractor = new MediaExtractor();
71+
try {
72+
//设置资源
73+
mMediaExtractor.setDataSource(mTargetFile.getAbsolutePath());
74+
//获取含有音频的MediaFormat
75+
MediaFormat mediaFormat = createMediaFormat();
76+
mMediaCodec = MediaCodec.createDecoderByType(MINE_TYPE_AAC);
77+
//解码时最后一个参数设置为0
78+
mMediaCodec.configure(mediaFormat, null, null, 0);
79+
if (mMediaCodec == null) {
80+
Log.e(TAG, "Create media decode failed");
81+
return;
82+
}
83+
//进入Runnable状态
84+
mMediaCodec.start();
85+
mBufferInfo = new MediaCodec.BufferInfo();
86+
Log.i(TAG, "Create media decode succeed");
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
92+
private MediaFormat createMediaFormat() {
93+
//获取文件的轨道数,做循环得到含有音频的mediaFormat
94+
for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) {
95+
MediaFormat mediaFormat = mMediaExtractor.getTrackFormat(i);
96+
//MediaFormat键值对应
97+
String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
98+
if (mime.contains("audio/")) {
99+
mMediaExtractor.selectTrack(i);
100+
return mediaFormat;
101+
}
102+
}
103+
return null;
104+
}
105+
106+
/**
107+
* 开始解码
108+
*/
109+
public void decode() {
110+
111+
boolean inputSawEos = false;
112+
boolean outputSawEos = false;
113+
long timeout = 5000;
114+
115+
while (!outputSawEos) {
116+
if (!inputSawEos) {
117+
//每5000毫秒查询一次
118+
int inputIndex = mMediaCodec.dequeueInputBuffer(timeout);
119+
//输入缓存index可用
120+
if (inputIndex >= 0) {
121+
//获取可用的输入缓存
122+
ByteBuffer inputBuffer = mMediaCodec.getInputBuffer(inputIndex);
123+
//从MediaExtractor读取数据到输入缓存中,返回读取长度
124+
int bufferSize = mMediaExtractor.readSampleData(inputBuffer, 0);
125+
if (bufferSize <= 0) {//已经读取完
126+
//标志输入完毕
127+
inputSawEos = true;
128+
//做标识
129+
mMediaCodec.queueInputBuffer(inputIndex, 0, 0, timeout, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
130+
} else {
131+
long time = mMediaExtractor.getSampleTime();
132+
//将输入缓存放入MediaCodec中
133+
mMediaCodec.queueInputBuffer(inputIndex, 0, bufferSize, time, 0);
134+
//指向下一帧
135+
mMediaExtractor.advance();
136+
}
137+
}
138+
}
139+
//获取输出缓存,需要传入MediaCodec.BufferInfo 用于存储ByteBuffer信息
140+
int outputIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, timeout);
141+
if (outputIndex >= 0) {
142+
if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
143+
mMediaCodec.releaseOutputBuffer(outputIndex, false);
144+
continue;
145+
}
146+
//有输出数据
147+
if (mBufferInfo.size > 0) {
148+
//获取输出缓存
149+
ByteBuffer outputBuffer = mMediaCodec.getOutputBuffer(outputIndex);
150+
//设置ByteBuffer的position位置
151+
outputBuffer.position(mBufferInfo.offset);
152+
//设置ByteBuffer访问的结点
153+
outputBuffer.limit(mBufferInfo.offset + mBufferInfo.size);
154+
byte[] targetData = new byte[mBufferInfo.size];
155+
//将数据填充到数组中
156+
outputBuffer.get(targetData);
157+
try {
158+
mFileOutputStream.write(targetData);
159+
} catch (IOException e) {
160+
e.printStackTrace();
161+
}
162+
}
163+
//释放输出缓存
164+
mMediaCodec.releaseOutputBuffer(outputIndex, false);
165+
//判断缓存是否完结
166+
if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
167+
outputSawEos = true;
168+
}
169+
}
170+
}
171+
//释放资源
172+
try {
173+
mFileOutputStream.flush();
174+
mFileOutputStream.close();
175+
mMediaCodec.stop();
176+
mMediaCodec.release();
177+
mMediaExtractor.release();
178+
if (null != mIDecodeDelegate) {
179+
mIDecodeDelegate.decodeResult(true);
180+
}
181+
} catch (IOException e) {
182+
e.printStackTrace();
183+
}
184+
}
185+
186+
/**
187+
* 是否解码完成回调
188+
*/
189+
interface IDecodeDelegate {
190+
void decodeResult(boolean isFinish);
191+
}
192+
}

0 commit comments

Comments
 (0)