/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "jmediaplayer.h" #include #include #include #include static jclass mediaPlayerClass = Q_NULLPTR; typedef QMap MediaPlayerMap; Q_GLOBAL_STATIC(MediaPlayerMap, mediaPlayers) QT_BEGIN_NAMESPACE JMediaPlayer::JMediaPlayer() : QObject() { const jlong id = reinterpret_cast(this); mMediaPlayer = QJNIObjectPrivate(mediaPlayerClass, "(Landroid/app/Activity;J)V", QtAndroidPrivate::activity(), id); (*mediaPlayers)[id] = this; } JMediaPlayer::~JMediaPlayer() { mediaPlayers->remove(reinterpret_cast(this)); } void JMediaPlayer::release() { mMediaPlayer.callMethod("release"); } void JMediaPlayer::reset() { mMediaPlayer.callMethod("reset"); } int JMediaPlayer::getCurrentPosition() { return mMediaPlayer.callMethod("getCurrentPosition"); } int JMediaPlayer::getDuration() { return mMediaPlayer.callMethod("getDuration"); } bool JMediaPlayer::isPlaying() { return mMediaPlayer.callMethod("isPlaying"); } int JMediaPlayer::volume() { return mMediaPlayer.callMethod("getVolume"); } bool JMediaPlayer::isMuted() { return mMediaPlayer.callMethod("isMuted"); } jobject JMediaPlayer::display() { return mMediaPlayer.callObjectMethod("display", "()Landroid/view/SurfaceHolder;").object(); } void JMediaPlayer::play() { mMediaPlayer.callMethod("start"); } void JMediaPlayer::pause() { mMediaPlayer.callMethod("pause"); } void JMediaPlayer::stop() { mMediaPlayer.callMethod("stop"); } void JMediaPlayer::seekTo(qint32 msec) { mMediaPlayer.callMethod("seekTo", "(I)V", jint(msec)); } void JMediaPlayer::setMuted(bool mute) { mMediaPlayer.callMethod("mute", "(Z)V", jboolean(mute)); } void JMediaPlayer::setDataSource(const QString &path) { QJNIObjectPrivate string = QJNIObjectPrivate::fromString(path); mMediaPlayer.callMethod("setDataSource", "(Ljava/lang/String;)V", string.object()); } void JMediaPlayer::prepareAsync() { mMediaPlayer.callMethod("prepareAsync"); } void JMediaPlayer::setVolume(int volume) { mMediaPlayer.callMethod("setVolume", "(I)V", jint(volume)); } void JMediaPlayer::setDisplay(jobject surfaceHolder) { mMediaPlayer.callMethod("setDisplay", "(Landroid/view/SurfaceHolder;)V", surfaceHolder); } QT_END_NAMESPACE static void onErrorNative(JNIEnv *env, jobject thiz, jint what, jint extra, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->error(what, extra); } static void onBufferingUpdateNative(JNIEnv *env, jobject thiz, jint percent, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->bufferingChanged(percent); } static void onProgressUpdateNative(JNIEnv *env, jobject thiz, jint progress, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->progressChanged(progress); } static void onDurationChangedNative(JNIEnv *env, jobject thiz, jint duration, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->durationChanged(duration); } static void onInfoNative(JNIEnv *env, jobject thiz, jint what, jint extra, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->info(what, extra); } static void onStateChangedNative(JNIEnv *env, jobject thiz, jint state, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->stateChanged(state); } static void onVideoSizeChangedNative(JNIEnv *env, jobject thiz, jint width, jint height, jlong id) { Q_UNUSED(env); Q_UNUSED(thiz); JMediaPlayer *const mp = (*mediaPlayers)[id]; if (!mp) return; Q_EMIT mp->videoSizeChanged(width, height); } QT_BEGIN_NAMESPACE bool JMediaPlayer::initJNI(JNIEnv *env) { jclass jClass = env->FindClass("org/qtproject/qt5/android/multimedia/QtAndroidMediaPlayer"); if (jClass) { mediaPlayerClass = static_cast(env->NewGlobalRef(jClass)); JNINativeMethod methods[] = { {"onErrorNative", "(IIJ)V", reinterpret_cast(onErrorNative)}, {"onBufferingUpdateNative", "(IJ)V", reinterpret_cast(onBufferingUpdateNative)}, {"onProgressUpdateNative", "(IJ)V", reinterpret_cast(onProgressUpdateNative)}, {"onDurationChangedNative", "(IJ)V", reinterpret_cast(onDurationChangedNative)}, {"onInfoNative", "(IIJ)V", reinterpret_cast(onInfoNative)}, {"onVideoSizeChangedNative", "(IIJ)V", reinterpret_cast(onVideoSizeChangedNative)}, {"onStateChangedNative", "(IJ)V", reinterpret_cast(onStateChangedNative)} }; if (env->RegisterNatives(mediaPlayerClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { return false; } } return true; } QT_END_NAMESPACE