Skip to content

Instantly share code, notes, and snippets.

@niusounds
Created December 26, 2013 12:45
Show Gist options
  • Save niusounds/8133434 to your computer and use it in GitHub Desktop.
Save niusounds/8133434 to your computer and use it in GitHub Desktop.
GStreamer SDKのチュートリアルが複雑すぎたので、なるべく簡単に音声を再生するサンプルコードを書いてみた。参考サイトはこちら。 http://docs.gstreamer.com/display/GstSDK/Android+tutorial+2%3A+A+running+pipeline
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := NativeApplication
LOCAL_SRC_FILES := NativeApplication.c
LOCAL_SHARED_LIBRARIES := gstreamer_android
LOCAL_LDLIBS := -landroid
include $(BUILD_SHARED_LIBRARY)
GSTREAMER_SDK_ROOT := $(GSTREAMER_SDK_ROOT_ANDROID)
GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_SDK_ROOT)/share/gst-android/ndk-build/
include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk
GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_SYS)
include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer.mk
#G_IO_MODULES := gnutls
#GSTREAMER_EXTRA_DEPS := gstreamer-interfaces-0.10 gstreamer-video-0.10
#
#include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer.mk
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.gstreamer.GStreamer;
public class MainActivity extends Activity {
private native void nativeInit();
private native void nativeFinalize();
private native void nativePlay();
private native void nativeStop();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
GStreamer.init(this);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
setContentView(R.layout.activity_main);
findViewById(R.id.play).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nativePlay();
}
});
findViewById(R.id.stop).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nativeStop();
}
});
nativeInit();
}
@Override
protected void onDestroy() {
nativeFinalize();
super.onDestroy();
}
static {
System.loadLibrary("gstreamer_android");
System.loadLibrary("NativeApplication");
}
}
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <gst/gst.h>
static GstElement *pipeline;
void Java_com_example_test_MainActivity_nativeInit(JNIEnv* env, jobject thiz) {
GError *error = NULL;
pipeline = gst_parse_launch(
"audiotestsrc ! audioconvert ! audioresample ! autoaudiosink",
&error);
if (error) {
g_clear_error(&error);
return;
}
}
void Java_com_example_test_MainActivity_nativeFinalize(JNIEnv* env,
jobject thiz) {
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
}
void Java_com_example_test_MainActivity_nativePlay(JNIEnv* env, jobject thiz) {
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
void Java_com_example_test_MainActivity_nativeStop(JNIEnv* env, jobject thiz) {
gst_element_set_state(pipeline, GST_STATE_PAUSED);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment