Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Translate code to C++



Permlink Replies: 10 - Last Post: Oct 21, 2016 12:30 AM Last Post By: Lena Ilicheva Threads: [ Previous | Next ]
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2016 10:48 PM
Eli M

Posts: 1,346
Registered: 11/9/13
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2016 11:17 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2016 9:08 AM   in response to: Eli M in response to: Eli M

I tried to add this срр and use:
 
#include <fmx.h>
#pragma hdrstop
 
#include "Unit1.h"
#include "AudioManager.h"
 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//С++ Builder don't understand the constant kSystemSoundID_vibrate 
AudioServicesPlaySystemSound(1);
}
//---------------------------------------------------------------------------


[ld Error] Error: "_AudioServicesPlaySystemSound", referenced from: __ZN6TForm112Button1ClickEPN6System7TObjectE in .\
\iOSDevice32\
\Debug
Unit1.o;
[ld Warning] ld: symbol(s) not found for architecture armv7
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2016 11:46 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
Lena wrote:

Please help translate this code to C++ Builder 10.1:

You do know that you can use Delphi code in a C++Builder project, don't you?
You could just copy the relevant portions of that Delphi code to its own
.pas unit, add that file to your C++ project, compile it to generate a .hpp
header file, #include that file in the rest of your C++ code where needed,
and then call its public functions like any other.

For example:

unit UVibrate;
 
interface
 
procedure Vibrate;
 
implementation
 
{$IFDEF ANDROID}
uses
  Androidapi.JNI.Os,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.Helpers,
  Androidapi.JNIBridge;
{$ENDIF}
{$IFDEF IOS}
uses
  IOSapi.MediaPlayer,
  IOSapi.CoreGraphics,
  FMX.Platform,
  FMX.Platform.IOS,
  IOSapi.UIKit,
  Macapi.ObjCRuntime,
  Macapi.ObjectiveC,
  iOSapi.Cocoatypes,
  Macapi.CoreFoundation,
  iOSapi.Foundation,
  iOSapi.CoreImage,
  iOSapi.QuartzCore,
  iOSapi.CoreData;
{$ENDIF}
 
{$IFDEF IOS}
const
  libAudioToolbox = '/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox';
  kSystemSoundID_vibrate = $FFF;
 
procedure AudioServicesPlaySystemSound( inSystemSoundID: integer ); cdecl; 
External libAudioToolbox Name _PU + 'AudioServicesPlaySystemSound';
{$ENDIF}
 
procedure Vibrate;
{$IFDEF ANDROID}
var
  Vibrator: JVibrator;
{$ENDIF}
begin
{$IFDEF ANDROID}
  Vibrator := TJVibrator.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.VIBRATOR_SERVICE) 
as ILocalObject).GetObjectID);
  // Vibrate for 500 milliseconds
  Vibrator.vibrate(500);
{$ENDIF}
{$IFDEF IOS}
  AudioServicesPlaySystemSound( kSystemSoundID_vibrate );
{$ENDIF}
end;
 
end.


Then you can do this:

#include "UVibrate.hpp";
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Vibrate();
}

That being said, here is a rough translation of the Community code:

#include <System.SysUtils.hpp>
#include <System.Types.hpp>
#include <System.UITypes.hpp>
#include <System.Classes.hpp>
#include <System.Variants.hpp>
#include <FMX.Types.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Graphics.hpp>
#include <FMX.Dialogs.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.StdCtrls.hpp>
 
#ifdef __ANDROID__
#include <Androidapi.JNI.Os.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNIBridge>
#endif
 
#if defined(__APPLE__) && !defined(__MACH__)
#include <IOSapi.MediaPlayer.hpp>
#include <IOSapi.CoreGraphics.hpp>
#include <FMX.Platform.hpp>
#include <FMX.Platform.IOS.hpp>
#include <IOSapi.UIKit.hpp>
#include <Macapi.ObjCRuntime.hpp>
#include <Macapi.ObjectiveC.hpp>
#include <iOSapi.Cocoatypes.hpp>
#include <Macapi.CoreFoundation.hpp>
#include <iOSapi.Foundation.hpp>
#include <iOSapi.CoreImage.hpp>
#include <iOSapi.QuartzCore.hpp>
#include <iOSapi.CoreData.hpp>
 
//static const char* libAudioToolbox = "/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox";
static const int kSystemSoundID_vibrate = 0xFFF;
 
// sorry, I don't know how to statically link to external functions on iOS,
// you will have to figure that out. You will likely have to use dynamic 
loading,
// but I don't know the relevant APIs for that...
extern void __cdecl AudioServicesPlaySystemSound(int inSystemSoundID); // 
External libAudioToolbox Name _PU + "AudioServicesPlaySystemSound";
#endif
 
void TForm1::Button1Click(TObject *Sender)
{
    #ifdef __ANDROID__
    _di_JVibrator Vibrator = TJVibrator::Wrap(((_di_ILocalObject)SharedActivityContext()->getSystemService(TJContext::JavaClass->VIBRATOR_SERVICE))->GetObjectID());
    // Vibrate for 500 milliseconds
    Vibrator->vibrate(500);
  #endif
  #if defined(__APPLE__) && !defined(__MACH__)
  AudioServicesPlaySystemSound(kSystemSoundID_vibrate);
  #endif
}


--
Remy Lebeau (TeamB)
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2016 11:37 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi.
I tried your recommendation. Create *.pas file add to project and build all in C++ Builder 10.1 UP1:

unit UVibrate;
 
interface
 
procedure Vibrate;
 
implementation
 
{$IFDEF ANDROID}
uses
  Androidapi.JNI.Os,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.Helpers,
  Androidapi.JNIBridge;
{$ENDIF}
{$IFDEF IOS}
uses
  IOSapi.MediaPlayer,
  IOSapi.CoreGraphics,
  FMX.Platform,
  FMX.Platform.IOS,
  IOSapi.UIKit,
  Macapi.ObjCRuntime,
  Macapi.ObjectiveC,
  iOSapi.Cocoatypes,
  Macapi.CoreFoundation,
  iOSapi.Foundation,
  iOSapi.CoreImage,
  iOSapi.QuartzCore,
  iOSapi.CoreData;
{$ENDIF}
 
{$IFDEF IOS}
const
  libAudioToolbox = '/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox';
  kSystemSoundID_vibrate = $FFF;
 
procedure AudioServicesPlaySystemSound( inSystemSoundID: integer ); cdecl; 
External libAudioToolbox Name _PU + 'AudioServicesPlaySystemSound';
{$ENDIF}
 
procedure Vibrate;
{$IFDEF ANDROID}
var
  Vibrator: JVibrator;
{$ENDIF}
begin
{$IFDEF ANDROID}
  Vibrator := TJVibrator.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.VIBRATOR_SERVICE) 
as ILocalObject).GetObjectID);
  // Vibrate for 500 milliseconds
  Vibrator.vibrate(500);
{$ENDIF}
{$IFDEF IOS}
  AudioServicesPlaySystemSound( kSystemSoundID_vibrate );
{$ENDIF}
end;
 
end.


Then:
#include "UVibrate.hpp"; //<--[bcciosarm Warning] Unit1.cpp(6): extra tokens at end of #include directive [-Wextra-tokens]
#include "Unit1.h"
 
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Vibrate();
}


But error:
[bcciosarm Warning] Unit1.cpp(6): extra tokens at end of #include directive [-Wextra-tokens]

[ld Error] Error: "_AudioServicesPlaySystemSound", referenced from: __ZN8Uvibrate28AudioServicesPlaySystemSoundEi in .
\ \iOSDevice32\ \Debug\ \UVibrate.o;

[ld Warning] ld: symbol(s) not found for architecture armv7

Where my mistake?
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2016 11:55 PM   in response to: Lena Ilicheva in response to: Lena Ilicheva
del

Edited by: Lena Ilicheva on Oct 11, 2016 11:56 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Translate code to C++ [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 12, 2016 10:33 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
Lena wrote:

#include "UVibrate.hpp"; //<--[bcciosarm Warning] Unit1.cpp(6):
extra tokens at end of #include directive [-Wextra-tokens]

An #include statement cannot have a semicolon at the end of it.

--
Remy Lebeau (TeamB)
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++ [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 12:09 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Sorry byt stiil error:
[ld Error] Error: "_AudioServicesPlaySystemSound", referenced from: __ZN8Uvibrate28AudioServicesPlaySystemSoundEi in .
\ \iOSDevice32\ \Debug\ \UVibrate.o;
[ld Warning] ld: symbol(s) not found for architecture armv7
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++ [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 18, 2016 12:40 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
John Clark

Posts: 13
Registered: 5/21/15
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2016 11:59 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
Lena Ilicheva wrote:
Hi.
Please help translate this code to C++ Builder 10.1:
http://community.embarcadero.com/blogs/entry/how-to-vibrate-ios-and-android-phones-using-firemonkey-and-xe8
Thanks!

You need to add the AudioToolbox to the iOS SDK. Under Tools | Options and go to the iOS SDK and click on a framework and add a Framework.

Path on remote maching
$(SDKROOT)/System/Library/Frameworks
Framework Name:
AudioToolbox

Add this function to TAudioManager.cpp
void __fastcall TAudioManager::VibrateDevice()
{
	try
	{
#if defined(_PLAT_ANDROID)
		_di_JObject obj = SharedActivityContext()->getSystemService(TJContext::JavaClass->VIBRATOR_SERVICE);
		_di_JVibrator Vibrator = TJVibrator::Wrap(((_di_ILocalObject)obj)->GetObjectID());
 
		Vibrator->vibrate(500);
#endif
 
#if defined(_PLAT_IOS)
		AudioServicesPlaySystemSound(kSystemSoundID_vibrate);
#endif
	}
	catch( Exception & E )
	{
		_Log(lt_Error, "[TAudioManager::VibrateDevice] : Exception = %s\n", _A(E.Message));
	}
}
//---------------------------------------------------------------------------


Edited by: John Clark on Oct 20, 2016 12:00 PM

Edited by: John Clark on Oct 20, 2016 12:01 PM
Lena Ilicheva

Posts: 83
Registered: 9/22/06
Re: Translate code to C++
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 12:30 AM   in response to: John Clark in response to: John Clark
John Clark wrote:
Lena Ilicheva wrote:
Hi.
Please help translate this code to C++ Builder 10.1:
http://community.embarcadero.com/blogs/entry/how-to-vibrate-ios-and-android-phones-using-firemonkey-and-xe8
Thanks!

You need to add the AudioToolbox to the iOS SDK. Under Tools | Options and go to the iOS SDK and click on a framework and add a Framework.

Path on remote maching
$(SDKROOT)/System/Library/Frameworks
Framework Name:
AudioToolbox

Add this function to TAudioManager.cpp
void __fastcall TAudioManager::VibrateDevice()
{
	try
	{
#if defined(_PLAT_ANDROID)
		_di_JObject obj = SharedActivityContext()->getSystemService(TJContext::JavaClass->VIBRATOR_SERVICE);
		_di_JVibrator Vibrator = TJVibrator::Wrap(((_di_ILocalObject)obj)->GetObjectID());
 
		Vibrator->vibrate(500);
#endif
 
#if defined(_PLAT_IOS)
		AudioServicesPlaySystemSound(kSystemSoundID_vibrate);
#endif
	}
	catch( Exception & E )
	{
		_Log(lt_Error, "[TAudioManager::VibrateDevice] : Exception = %s\n", _A(E.Message));
	}
}
//---------------------------------------------------------------------------


Thank You!

P.S.
How to upgrade the procedure VibrateDevice to be able to call or vibration or sound?
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02