編輯:關於Android編程
??最新的android studio2.2引入了cmake可以很好地實現ndk的編寫。
??進入正題,既然是ffmpeg的移植編譯,那麼就先下載ffmpeg,https://ffmpeg.org/download.html。這裡下載的是3.0.3版本。
??新建ffmpeg文件夾,然後新建腳本用來編譯ffmpeg,命名為build.sh,腳本如下:
??主要是實現了四個button,分別是protocol,format,codec和filter。如下圖所示:
??
??修改jni部分的代碼,也就是native-lib.cpp的代碼如下:
#include#include extern "C" { #include #include #include jstring Java_com_jared_helloffmpeg_MainActivity_stringFromJNI( JNIEnv *env, jobject) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } jstring Java_com_jared_helloffmpeg_MainActivity_urlprotocolinfo( JNIEnv *env, jobject) { char info[40000] = {0}; av_register_all(); struct URLProtocol *pup = NULL; struct URLProtocol **p_temp = &pup; avio_enum_protocols((void **) p_temp, 0); while ((*p_temp) != NULL) { sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 0)); } pup = NULL; avio_enum_protocols((void **) p_temp, 1); while ((*p_temp) != NULL) { sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 1)); } return env->NewStringUTF(info); } jstring Java_com_jared_helloffmpeg_MainActivity_avformatinfo( JNIEnv *env, jobject) { char info[40000] = {0}; av_register_all(); AVInputFormat *if_temp = av_iformat_next(NULL); AVOutputFormat *of_temp = av_oformat_next(NULL); while (if_temp != NULL) { sprintf(info, "%sInput: %s\n", info, if_temp->name); if_temp = if_temp->next; } while (of_temp != NULL) { sprintf(info, "%sOutput: %s\n", info, of_temp->name); of_temp = of_temp->next; } return env->NewStringUTF(info); } jstring Java_com_jared_helloffmpeg_MainActivity_avcodecinfo( JNIEnv *env, jobject) { char info[40000] = {0}; av_register_all(); AVCodec *c_temp = av_codec_next(NULL); while (c_temp != NULL) { if (c_temp->decode != NULL) { sprintf(info, "%sdecode:", info); } else { sprintf(info, "%sencode:", info); } switch (c_temp->type) { case AVMEDIA_TYPE_VIDEO: sprintf(info, "%s(video):", info); break; case AVMEDIA_TYPE_AUDIO: sprintf(info, "%s(audio):", info); break; default: sprintf(info, "%s(other):", info); break; } sprintf(info, "%s[%10s]\n", info, c_temp->name); c_temp = c_temp->next; } return env->NewStringUTF(info); } jstring Java_com_jared_helloffmpeg_MainActivity_avfilterinfo( JNIEnv *env, jobject) { char info[40000] = {0}; avfilter_register_all(); AVFilter *f_temp = (AVFilter *)avfilter_next(NULL); while(f_temp != NULL) { sprintf(info, "%s%s\n", info, f_temp->name); f_temp = f_temp->next; } return env->NewStringUTF(info); } }
??這裡要特別注意ffmpeg的頭文件已經調用的代碼要extern “C”裡面,要不然會報錯誤的。
??最後就是java調用代碼了。如下所示:
package com.jared.helloffmpeg; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.jared.helloffmpeg.databinding.MainBinding; import static java.lang.System.loadLibrary; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private MainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.btnProtocol.setOnClickListener(this); binding.btnCodec.setOnClickListener(this); binding.btnFilter.setOnClickListener(this); binding.btnFormat.setOnClickListener(this); // Example of a call to a native method //TextView tv = (TextView) findViewById(R.id.sample_text); //tv.setText(avformatinfo()); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_protocol: binding.tvInfo.setText(urlprotocolinfo()); break; case R.id.btn_format: binding.tvInfo.setText(avformatinfo()); break; case R.id.btn_codec: binding.tvInfo.setText(avcodecinfo()); break; case R.id.btn_filter: binding.tvInfo.setText(avfilterinfo()); break; default: break; } } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public native String stringFromJNI(); public native String urlprotocolinfo(); public native String avformatinfo(); public native String avcodecinfo(); public native String avfilterinfo(); // Used to load the 'native-lib' library on application startup. static { loadLibrary("native-lib"); } }
??都准備好了,那麼就繼續運行看看結果了:
??基本上Android Studio下的cmake編譯ffmpeg就到此結束了,那麼接下去就可以在android手機上使用ffmpeg來做一些音視頻相關的東西了。
繼續練習自定義View。。畢竟熟才能生巧。一直覺得小米的時鐘很精美,那這次就搞它~這次除了練習自定義View,還涉及到使用Camera和Matrix實現3D效果。一個這樣
什麼都先別說,先看預覽圖!預覽圖中是限制了同時最大下載數為 2 的.其實下載管理器的實現是挺簡單的,我們需要弄清楚幾點就行了服務基礎首先我們需要明確一點,下載我們應該使用
現在側滑菜單使用很多,大都是通過SlidingMenu實現。現在也可以通過DrawerLayout創建抽屜布局 activity_main.xml
android檢查更新、下載、安裝前言:由於友盟更新即將下線,我們就修改了更新邏輯,自己檢查更新、下載、安裝,但是檢查更新還是要依賴於友盟中的在線參數:1.MainAct