[Android]BASS

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

[Android]BASS

Post by Lena »

Creating an Android application using BASS.
1. Download the archives for Android and Windows from the BASS site.
2.
a) For Delphi 10.3.3, open the bass.pas file from the Windows archive. Instead of AnsiChar we write Byte, place PAnsiChar we write MarshaledAString.
b) For C ++ Builder 10.3.3, open bass.h and comment on the declaration
// typedef uint32_t DWORD;
c) Include these files in the uses / include project.
3. Add SO files from the armeabi / armeabi-v7a / arm64-v8a folders to the project Deployment and specify the Remote path in the Deployment (library \ lib \ armeabi-v7a and others).
4. In the project options, add the path to the arm64-v8a folder in the Seach Path.

I try use Delphi 64bit because I want publish app in Google Play Market.

Code: Select all

var
  Form1: TForm1;

  smp: HSAMPLE;
  str: HSTREAM;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
  M: TMarshaller;

begin

 BASS_StreamFree(str);
 str := BASS_StreamCreateURL(M.AsAnsi('http://91.199.194.34:8000').ToPointer, 0, 0, nil, nil);
 BASS_SetVolume(TrackBar1.Value/100.0);
 BASS_ChannelPlay(str, True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 BASS_ChannelStop(str);
 BASS_SampleFree (smp);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 TrackBar1.Value := 10;
 if not BASS_Init(-1, 44100, 0, nil, nil) then
	  ShowMessage('Error initializing audio!');
end;


procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  BASS_SetVolume(TrackBar1.Value/100.0);
end;

When i press the button nothing happens. :(
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

Code: Select all

//str := BASS_StreamCreateURL(M.AsAnsi('http://91.199.194.34:8000').ToPointer, 0, 0, nil, nil);
 //str := BASS_StreamCreateURL(('http://91.199.194.34:8000'), 0, 0, nil, nil);
 //str := BASS_StreamCreateURL(Pchar('http://91.199.194.34:8000'), 0, 0, nil, nil);
 str := BASS_StreamCreateURL(PChar('http://91.199.194.34:8000'),0,BASS_STREAM_STATUS or BASS_UNICODE,nil,nil);

 if(str = 0) then
  begin
    ShowMessage('Problem!');
  end;
I've tried different options. I see all the time ShowMessage. :(
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

Code: Select all

//ErrorCode=41 str := BASS_StreamCreateURL(M.AsAnsi('http://91.199.194.34:8000').ToPointer, 0, 0, nil, nil);
//ErrorCode=20 str := BASS_StreamCreateURL(('http://91.199.194.34:8000'), 0, 0, nil, nil);
//ErrorCode=20 str := BASS_StreamCreateURL(Pchar('http://91.199.194.34:8000'), 0, 0, nil, nil);
//ErrorCode=41 str := BASS_StreamCreateURL(PChar('http://91.199.194.34:8000'),0,BASS_STREAM_STATUS or BASS_UNICODE,nil,nil);
 ShowMessage(IntToStr(BASS_ErrorGetCode));
BASS_ErrorGetCode
HsiaLin
BCBJ Master
BCBJ Master
Posts: 333
Joined: Sun Jul 08, 2007 6:29 pm

Re: [Android]BASS

Post by HsiaLin »

I don't use phone code but i have used BASS in the older C++ Builder. Maybe i can post you a snippet or 2 to point you in right direction.
Do note that in XE5 (which is what i use) you have to delay load BASS.DLL by adding it to the delay load option in the project settings.
You also need BASS.LIB that will work with C++ Builder and add it to project.

To init you should use something like this;

//define in header or top of .cpp
HSTREAM Channel;

Code: Select all

    BASS_SetConfig(BASS_CONFIG_NET_PLAYLIST, 1); //enable playlist processing
    BASS_SetConfig(BASS_CONFIG_NET_BUFFER, 5120);
    BASS_SetConfig(BASS_CONFIG_NET_PREBUF_WAIT, 0); //disable BASS_StreamCreateURL pre-buffering
    BOOL BassLoaded = BASS_Init(-1, 44100, BASS_UNICODE | BASS_STREAM_AUTOFREE, 0, NULL);

Code: Select all

String url = L"http://91.199.194.34:8000";
Channel = BASS_StreamCreateURL(url.c_str(), 0, BASS_STREAM_BLOCK | BASS_STREAM_AUTOFREE, NULL, NULL);
BASS_ChannelPlay(Channel, false);
...
//BASS_ChannelStop(Channel);
//BASS_StreamFree(Channel);

Code: Select all

//---------------------------------------------------------------------------
//to change volume you must act on the channel
void __fastcall TForm1::TrackVolChange(TObject *Sender)
{

   BASS_ChannelSetAttribute(Channel, BASS_ATTRIB_VOL, float(TrackVol->Position / 100.0));
 
}

//---------------------------------------------------------------------------

I tried your url in my test program and it plays good.
Attachments
BASSTestCppB.zip
(244.87 KiB) Downloaded 89 times
Last edited by HsiaLin on Fri Oct 02, 2020 3:50 am, edited 3 times in total.
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

Thank you.
I tried your url in my test program and it plays good.
In Windows platform no problem. On Android problem.

Code: Select all

//Windows
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 BASS_Init(-1, 44100, BASS_DEVICE_3D, 0, NULL);
 str=BASS_StreamCreateURL(PAnsiChar("http://91.199.194.34:8000"), 0, 0, NULL,0);
 BASS_SetVolume(TrackBar1->Value/100.0);
 BASS_ChannelPlay(str, false);
}

you have to delay load BASS.DLL by adding it to the delay load option in the project settings
BASS.DLL only for Windows platform.
HsiaLin
BCBJ Master
BCBJ Master
Posts: 333
Joined: Sun Jul 08, 2007 6:29 pm

Re: [Android]BASS

Post by HsiaLin »

I`m glad i`m retired now so i don't have to deal with iOS and android lol.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: [Android]BASS

Post by rlebeau »

HsiaLin wrote: Tue Sep 29, 2020 5:31 am I don't use phone code but i have used BASS in the older C++ Builder. Maybe i can post you a snippet or 2 to point you in right direction.
Just be aware that String is UnicodeString in C++Builder 2009+, so use the BASS_UNICODE flag when calling BASS_StreamCreateURL(), not on BASS_Init():

Code: Select all

BOOL BassLoaded = BASS_Init(-1, 44100, 0, nullptr, nullptr);

String url = L"http://91.199.194.34:8000";
Channel = BASS_StreamCreateURL(reinterpret_cast<char*>(url.c_str()), 0, BASS_UNICODE | BASS_STREAM_BLOCK | BASS_STREAM_AUTOFREE, nullptr, nullptr);

...
Remy Lebeau (TeamB)
Lebeau Software
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

Code: Select all

str := BASS_StreamCreateURL(PChar('http://91.199.194.34:8000'), 0, BASS_UNICODE, nil, nil);
ShowMessage(IntToStr(BASS_ErrorGetCode)); 
//ErrorCode=41

str := BASS_StreamCreateURL(Pchar('http://91.199.194.34:8000'), 0, 0, nil, nil);
ShowMessage(IntToStr(BASS_ErrorGetCode)); 
//ErrorCode=20
No sound on Android. :(
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

Steps:
1. Download ZIP file.
2. Open - P.R.Chernivtsi - .m3u with MCP player. Will you hear the music. In the MCP window title 91.199.194.34:8000.
3. Open - P.R.Chernivtsi - .m3u with Notepad. You will see http://91.199.194.34:8000.
When I create a project for Windows+Bass, no problem. I hear music.
- P.R.Chernivtsi - .zip
(226 Bytes) Downloaded 88 times
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: [Android]BASS

Post by rlebeau »

Lena wrote: Tue Sep 29, 2020 9:50 am

Code: Select all

str := BASS_StreamCreateURL(PChar('http://91.199.194.34:8000'), 0, BASS_UNICODE, nil, nil);
ShowMessage(IntToStr(BASS_ErrorGetCode)); 
//ErrorCode=41
Error Code 41 is BASS_ERROR_FILEFORM ("The file's format is not recognised/supported"), which means the HTTP server did not send an audio file/stream. When I request http://91.199.194.34:8000 in my web browser, I get an HTML page that describes an audio stream, not the audio stream itself. When I request http://publicradio.mybb.online in my web browser, I get an HTML page with an embedded audio player that plays an audio stream, not the audio stream itself. So double-check that you are using a correct URL for the actual audio stream that you want to access.
Lena wrote: Tue Sep 29, 2020 9:50 am

Code: Select all

str := BASS_StreamCreateURL(Pchar('http://91.199.194.34:8000'), 0, 0, nil, nil);
ShowMessage(IntToStr(BASS_ErrorGetCode)); 
//ErrorCode=20
Error Code 20 is BASS_ERROR_ILLPARAM ("url is not a valid URL"), which makes sense as you are not specifying the BASS_UNICODE flag when passing in a UTF-16 string. So it will be expecting a UTF-8 string instead.
Remy Lebeau (TeamB)
Lebeau Software
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

I have uploaded new SO files. It's okay. I hear a sound.
www.un4seen.com/stuff/bass-android-test.zip
But I can't change the sound volume. :(

Code: Select all

smp: HSAMPLE;
  str: HSTREAM;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);

Begin

 BASS_StreamFree(str);
 str := BASS_StreamCreateURL(PChar('http://91.199.194.34:8000'), 0, BASS_UNICODE, nil, nil);
 //ShowMessage(IntToStr(BASS_ErrorGetCode));

  If BASS_ErrorGetCode = 40 Then
  Begin
    ShowMessage('No internet connection');
    exit;
  End;

  If BASS_ErrorGetCode = 0 Then
  Begin
     BASS_SetVolume(TrackBar1.Value/100.0);
     BASS_ChannelPlay(str, True);
     Viewport3D1.Visible := True;
     FloatAnimation1.Enabled := True;
   End;
End;

procedure TForm1.Button2Click(Sender: TObject);
begin
 BASS_ChannelStop(str);
 BASS_SampleFree (smp);
 Viewport3D1.Visible := False;
 FloatAnimation1.Enabled := False;
end;

procedure TForm1.FormCreate(Sender: TObject);
Begin
 TrackBar1.Value := 10;

 if not BASS_Init(-1, 44100, 0, nil, nil) Then
	  ShowMessage('Problem!');
End;

procedure TForm1.TrackBar1Change(Sender: TObject);
Begin
  BASS_SetVolume(TrackBar1.Value/100.0);//<---- Does not work!
End;
TrackBar Min=0 Max=100
HsiaLin
BCBJ Master
BCBJ Master
Posts: 333
Joined: Sun Jul 08, 2007 6:29 pm

Re: [Android]BASS

Post by HsiaLin »

Have you tried using

BASS_ChannelSetAttribute(str, BASS_ATTRIB_VOL, float(TrackVol->Position / 100.0));

or whatever version of that call that android uses.

BASS_SetVolume() changes the OS volume.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: [Android]BASS

Post by rlebeau »

This is stated as much in the BASS_SetVolume() documentation:
This function affects the volume level of all applications using the same output device. If you wish to only affect the level of your application's sounds, the BASS_ATTRIB_VOL attribute and/or the BASS_CONFIG_GVOL_MUSIC / BASS_CONFIG_GVOL_SAMPLE / BASS_CONFIG_GVOL_STREAM config options should be used instead.
Also, make sure you are performing error handling on ALL of your BASS function calls.
Remy Lebeau (TeamB)
Lebeau Software
Lena
BCBJ Master
BCBJ Master
Posts: 709
Joined: Sun Feb 06, 2011 1:28 pm

Re: [Android]BASS

Post by Lena »

If I minimize my application and lock the screen on the phone, then after a couple of minutes the player stops playing. How do I make the player work in the background?
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: [Android]BASS

Post by rlebeau »

Lena wrote: Mon Oct 05, 2020 4:31 am If I minimize my application and lock the screen on the phone, then after a couple of minutes the player stops playing.
Android is likely killing your app process when it goes into the background and stays there for a period of time.

Understand the Activity Lifecycle
Lena wrote: Mon Oct 05, 2020 4:31 am How do I make the player work in the background?
By moving your playback code into a background service that is separate from your front-end UI code. The UI tells the service what to play, and the service handles the actual playback, even while the UI is minimized/closed.

Media app architecture overview

Audio app overview
Remy Lebeau (TeamB)
Lebeau Software
Post Reply