https://community.kde.org/index.php?title=Necessitas/JNI&feed=atom&action=history
Necessitas/JNI - Revision history
2024-03-28T11:15:34Z
Revision history for this page on the wiki
MediaWiki 1.40.2
https://community.kde.org/index.php?title=Necessitas/JNI&diff=29711&oldid=prev
Chao Feng: Fix style. It is not code.
2013-01-26T14:26:02Z
<p>Fix style. It is not code.</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 14:26, 26 January 2013</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Using JNI ==</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Using JNI ==</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> </del>The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly ([http://en.wikipedia.org/wiki/Java_Native_Interface quote from wikipedia]) .</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly ([http://en.wikipedia.org/wiki/Java_Native_Interface quote from wikipedia]) .</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> </del>In this page we are not going to teach you JNI, Java or C/C++, instead we'll try to explain how to use JNI from a Qt application. If you want to lean JNI, then you should start with by reading the following documentations [http://developer.android.com/guide/practices/jni.html Google's JNI page] [http://android-developers.blogspot.ro/2011/11/jni-local-reference-changes-in-ics.html Google's blogs].</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In this page we are not going to teach you JNI, Java or C/C++, instead we'll try to explain how to use JNI from a Qt application. If you want to lean JNI, then you should start with by reading the following documentations [http://developer.android.com/guide/practices/jni.html Google's JNI page] [http://android-developers.blogspot.ro/2011/11/jni-local-reference-changes-in-ics.html Google's blogs].</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> </del>First this you need to know is that you don't need to load your application manually (e.g. don't use '''System.loadLibrary'''), the second thing you also need to know is that your Qt application is running on a different thread than main Android Activity, so if you want to use Activity you should use [http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) runOnUiThread].</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>First this you need to know is that you don't need to load your application manually (e.g. don't use '''System.loadLibrary'''), the second thing you also need to know is that your Qt application is running on a different thread than main Android Activity, so if you want to use Activity you should use [http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) runOnUiThread].</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> </del>In this example we'll create a simple Audio player using Android's [http://developer.android.com/reference/android/media/MediaPlayer.html MediaPlayer]. [http://dl.dropbox.com/u/67435935/untitled7.tar.bz2 Here] you can download the full example.</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In this example we'll create a simple Audio player using Android's [http://developer.android.com/reference/android/media/MediaPlayer.html MediaPlayer]. [http://dl.dropbox.com/u/67435935/untitled7.tar.bz2 Here] you can download the full example.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== The .pro file ===</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== The .pro file ===</div></td></tr>
</table>
Chao Feng
https://community.kde.org/index.php?title=Necessitas/JNI&diff=25766&oldid=prev
Bog dan ro: /* Using JNI */
2012-10-23T17:50:34Z
<p><span dir="auto"><span class="autocomment">Using JNI</span></span></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 17:50, 23 October 2012</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l7">Line 7:</td>
<td colspan="2" class="diff-lineno">Line 7:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> First this you need to know is that you don't need to load your application manually (e.g. don't use '''System.loadLibrary'''), the second thing you also need to know is that your Qt application is running on a different thread than main Android Activity, so if you want to use Activity you should use [http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) runOnUiThread].</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> First this you need to know is that you don't need to load your application manually (e.g. don't use '''System.loadLibrary'''), the second thing you also need to know is that your Qt application is running on a different thread than main Android Activity, so if you want to use Activity you should use [http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) runOnUiThread].</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> In this example we'll create a simple Audio player using Android's [http://developer.android.com/reference/android/media/MediaPlayer.html MediaPlayer].</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> In this example we'll create a simple Audio player using Android's [http://developer.android.com/reference/android/media/MediaPlayer.html MediaPlayer]<ins style="font-weight: bold; text-decoration: none;">. [http://dl.dropbox.com/u/67435935/untitled7.tar.bz2 Here] you can download the full example</ins>.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== The .pro file ===</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== The .pro file ===</div></td></tr>
</table>
Bog dan ro
https://community.kde.org/index.php?title=Necessitas/JNI&diff=25765&oldid=prev
Bog dan ro: Created page with "== Using JNI == The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, nati..."
2012-10-23T17:42:52Z
<p>Created page with "== Using JNI == The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, nati..."</p>
<p><b>New page</b></p><div>== Using JNI ==<br />
<br />
The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly ([http://en.wikipedia.org/wiki/Java_Native_Interface quote from wikipedia]) .<br />
<br />
In this page we are not going to teach you JNI, Java or C/C++, instead we'll try to explain how to use JNI from a Qt application. If you want to lean JNI, then you should start with by reading the following documentations [http://developer.android.com/guide/practices/jni.html Google's JNI page] [http://android-developers.blogspot.ro/2011/11/jni-local-reference-changes-in-ics.html Google's blogs].<br />
<br />
First this you need to know is that you don't need to load your application manually (e.g. don't use '''System.loadLibrary'''), the second thing you also need to know is that your Qt application is running on a different thread than main Android Activity, so if you want to use Activity you should use [http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) runOnUiThread].<br />
<br />
In this example we'll create a simple Audio player using Android's [http://developer.android.com/reference/android/media/MediaPlayer.html MediaPlayer].<br />
<br />
=== The .pro file ===<br />
Edit your profile and add the following lines for Android platform.<br />
<syntaxhighlight lang="make"><br />
android {<br />
SOURCES += androidmediaplayer.cpp<br />
HEADERS += androidmediaplayer.h<br />
}<br />
</syntaxhighlight><br />
<br />
=== The QSimpleAudioPlayer.java file ===<br />
Create QSimpleAudioPlayer.java to your project_root/android/src/org/kde/necessitas/origo/<br />
<br />
<syntaxhighlight lang="java"><br />
package org.kde.necessitas.origo;<br />
<br />
import android.media.AudioManager;<br />
import android.media.MediaPlayer;<br />
import android.util.Log;<br />
<br />
public class QSimpleAudioPlayer {<br />
MediaPlayer m_mediaPlayer;<br />
<br />
public QSimpleAudioPlayer() {<br />
m_mediaPlayer = new MediaPlayer();<br />
}<br />
<br />
boolean setUrl(String path)<br />
{<br />
m_mediaPlayer.reset();<br />
try {<br />
m_mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);<br />
m_mediaPlayer.setDataSource(path);<br />
m_mediaPlayer.prepare();<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
return false;<br />
}<br />
Log.i("Qt", "addTwoNumbers returned "+addTwoNumbers(1,2));<br />
return true;<br />
}<br />
<br />
boolean play()<br />
{<br />
try {<br />
m_mediaPlayer.start();<br />
}<br />
catch (Exception e) {<br />
e.printStackTrace();<br />
return false;<br />
}<br />
return true;<br />
}<br />
<br />
boolean pause()<br />
{<br />
try {<br />
m_mediaPlayer.pause();<br />
}<br />
catch (Exception e) {<br />
e.printStackTrace();<br />
return false;<br />
}<br />
return true;<br />
}<br />
<br />
boolean stop()<br />
{<br />
try {<br />
m_mediaPlayer.stop();<br />
}<br />
catch (Exception e) {<br />
e.printStackTrace();<br />
return false;<br />
}<br />
return true;<br />
}<br />
<br />
boolean release()<br />
{<br />
try {<br />
m_mediaPlayer.release();<br />
}<br />
catch (Exception e) {<br />
e.printStackTrace();<br />
return false;<br />
}<br />
return true;<br />
}<br />
<br />
public static native int addTwoNumbers(int a, int b);<br />
}<br />
</syntaxhighlight><br />
<br />
=== the androidmediaplayer.cpp file ===<br />
<br />
<syntaxhighlight lang="cpp-qt"><br />
#include <QDebug><br />
<br />
#include "androidmediaplayer.h"<br />
<br />
static JavaVM* s_javaVM = 0;<br />
static jclass s_audioPlayerClassID = 0;<br />
static jmethodID s_audioPlayerConstructorMethodID=0;<br />
static jmethodID s_audioPlayerSetUrlMethodID=0;<br />
static jmethodID s_audioPlayerPlayMethodID=0;<br />
static jmethodID s_audioPlayerPauseMethodID=0;<br />
static jmethodID s_audioPlayerStopMethodID=0;<br />
static jmethodID s_audioPlayerReleaseMethodID=0;<br />
<br />
SimpleAndroidMediaPlayer::SimpleAndroidMediaPlayer()<br />
{<br />
JNIEnv* env;<br />
// Qt is running in a different thread than Java UI, so you always Java VM *MUST* be attached to current thread<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return;<br />
}<br />
<br />
// Create a new instance of QSimpleAudioPlayer<br />
m_audioPlayerObject = env->NewGlobalRef(env->NewObject(s_audioPlayerClassID, s_audioPlayerConstructorMethodID));<br />
if (!m_audioPlayerObject)<br />
{<br />
qCritical()<<"Can't create the player";<br />
return;<br />
}<br />
<br />
// Don't forget to detach from current thread<br />
s_javaVM->DetachCurrentThread();<br />
}<br />
<br />
SimpleAndroidMediaPlayer::~SimpleAndroidMediaPlayer()<br />
{<br />
if (!m_audioPlayerObject)<br />
return;<br />
<br />
JNIEnv* env;<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return;<br />
}<br />
<br />
if (!env->CallBooleanMethod(m_audioPlayerObject, s_audioPlayerReleaseMethodID))<br />
qCritical()<<"Releasing media player object failed";<br />
<br />
s_javaVM->DetachCurrentThread();<br />
}<br />
<br />
bool SimpleAndroidMediaPlayer::setUrl(const QString &url)<br />
{<br />
if (!m_audioPlayerObject)<br />
return false;<br />
<br />
JNIEnv* env;<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return false;<br />
}<br />
jstring str = env->NewString(reinterpret_cast<const jchar*>(url.constData()), url.length());<br />
jboolean res = env->CallBooleanMethod(m_audioPlayerObject, s_audioPlayerSetUrlMethodID, str);<br />
env->DeleteLocalRef(str);<br />
s_javaVM->DetachCurrentThread();<br />
return res;<br />
}<br />
<br />
bool SimpleAndroidMediaPlayer::play()<br />
{<br />
if (!m_audioPlayerObject)<br />
return false;<br />
<br />
JNIEnv* env;<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return false;<br />
}<br />
jboolean res = env->CallBooleanMethod(m_audioPlayerObject, s_audioPlayerPlayMethodID);<br />
s_javaVM->DetachCurrentThread();<br />
return res;<br />
}<br />
<br />
bool SimpleAndroidMediaPlayer::pause()<br />
{<br />
if (!m_audioPlayerObject)<br />
return false;<br />
<br />
JNIEnv* env;<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return false;<br />
}<br />
jboolean res = env->CallBooleanMethod(m_audioPlayerObject, s_audioPlayerPauseMethodID);<br />
s_javaVM->DetachCurrentThread();<br />
return res;<br />
}<br />
<br />
bool SimpleAndroidMediaPlayer::stop()<br />
{<br />
if (!m_audioPlayerObject)<br />
return false;<br />
<br />
JNIEnv* env;<br />
if (s_javaVM->AttachCurrentThread(&env, NULL)<0)<br />
{<br />
qCritical()<<"AttachCurrentThread failed";<br />
return false;<br />
}<br />
jboolean res = env->CallBooleanMethod(m_audioPlayerObject, s_audioPlayerStopMethodID);<br />
s_javaVM->DetachCurrentThread();<br />
return res;<br />
}<br />
<br />
// our native method, it is called by the java code above<br />
static int addTwoNumbers(JNIEnv * /*env*/, jobject /*thiz*/,int a, int b)<br />
{<br />
return a+b;<br />
}<br />
<br />
static JNINativeMethod methods[] = {<br />
{"addTwoNumbers", "(II)I", (void *)addTwoNumbers}<br />
};<br />
<br />
// this method is called immediately after the module is load<br />
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)<br />
{<br />
JNIEnv* env;<br />
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {<br />
qCritical()<<"Can't get the enviroument";<br />
return -1;<br />
}<br />
<br />
s_javaVM = vm;<br />
// search for our class<br />
jclass clazz=env->FindClass("org/kde/necessitas/origo/QSimpleAudioPlayer");<br />
if (!clazz)<br />
{<br />
qCritical()<<"Can't find QSimpleAudioPlayer class";<br />
return -1;<br />
}<br />
// keep a global reference to it<br />
s_audioPlayerClassID = (jclass)env->NewGlobalRef(clazz);<br />
<br />
// search for its contructor<br />
s_audioPlayerConstructorMethodID = env->GetMethodID(s_audioPlayerClassID, "<init>", "()V");<br />
if (!s_audioPlayerConstructorMethodID)<br />
{<br />
qCritical()<<"Can't find QSimpleAudioPlayer class contructor";<br />
return -1;<br />
}<br />
<br />
// search for setUrl method<br />
s_audioPlayerSetUrlMethodID = env->GetMethodID(s_audioPlayerClassID, "setUrl", "(Ljava/lang/String;)Z");<br />
if (!s_audioPlayerSetUrlMethodID)<br />
{<br />
qCritical()<<"Can't find setUrl method";<br />
return -1;<br />
}<br />
<br />
// search for play method<br />
s_audioPlayerPlayMethodID = env->GetMethodID(s_audioPlayerClassID, "play", "()Z");<br />
if (!s_audioPlayerPlayMethodID)<br />
{<br />
qCritical()<<"Can't find start method";<br />
return -1;<br />
}<br />
<br />
// search for play method<br />
s_audioPlayerPauseMethodID = env->GetMethodID(s_audioPlayerClassID, "pause", "()Z");<br />
if (!s_audioPlayerPauseMethodID)<br />
{<br />
qCritical()<<"Can't find start method";<br />
return -1;<br />
}<br />
<br />
// search for stop method<br />
s_audioPlayerStopMethodID = env->GetMethodID(s_audioPlayerClassID, "stop", "()Z");<br />
if (!s_audioPlayerStopMethodID)<br />
{<br />
qCritical()<<"Can't find stop method";<br />
return -1;<br />
}<br />
<br />
// search for release method<br />
s_audioPlayerReleaseMethodID = env->GetMethodID(s_audioPlayerClassID, "release", "()Z");<br />
if (!s_audioPlayerReleaseMethodID)<br />
{<br />
qCritical()<<"Can't find release method";<br />
return -1;<br />
}<br />
<br />
// register our native methods<br />
if (env->RegisterNatives(s_audioPlayerClassID, methods, sizeof(methods) / sizeof(methods[0])) < 0)<br />
{<br />
qCritical()<<"RegisterNatives failed";<br />
return -1;<br />
}<br />
<br />
qDebug()<<"Yahooo !";<br />
return JNI_VERSION_1_6;<br />
}<br />
</syntaxhighlight><br />
<br />
=== the androidmediaplayer.h file ===<br />
<br />
<syntaxhighlight lang="cpp-qt"><br />
#ifndef ANDROIDMEDIAPLAYER_H<br />
#define ANDROIDMEDIAPLAYER_H<br />
#include <jni.h><br />
<br />
class SimpleAndroidMediaPlayer<br />
{<br />
public:<br />
SimpleAndroidMediaPlayer();<br />
~SimpleAndroidMediaPlayer();<br />
bool setUrl(const QString &url);<br />
bool play();<br />
bool pause();<br />
bool stop();<br />
<br />
private:<br />
jobject m_audioPlayerObject;<br />
<br />
};<br />
<br />
#endif // ANDROIDMEDIAPLAYER_H<br />
</syntaxhighlight></div>
Bog dan ro