Can I play music directly?

200 views
Skip to first unread message

Bill DeWitt

unread,
Aug 29, 2018, 7:15:31 PM8/29/18
to DroidScript
So I've been looking around but I don't see a lot of info on playing music or reading the ID3 tags. Which is what I want to do. 

Am I missing it somehow?
Message has been deleted

Bill DeWitt

unread,
Aug 29, 2018, 8:01:54 PM8/29/18
to DroidScript
Never mind.

I found it under "Audio" but I had been looking for "Music" or "Media".

Jared

unread,
Aug 30, 2018, 7:48:00 AM8/30/18
to DroidScript
I made this basic audio player, even plays Flac Files. doesn't have Playlist but it's something to work with if you like


/*testclient*/

function OnStart () {
new audioPlayer();
audioPlayer.buildLayout();

}


function audioPlayer() {
app.SetOrientation("landscape")
audioPlayer.fileName = this.fileName || ""
audioPlayer.format = this.format || ""
audioPlayer.type = this.type || ""
audioPlayer.LoadAudio = function (fileName){

this.fileName = fileName || audioPlayer.fileName;
return this.fileName ;

}
audioPlayer.buildLayout = function (){
audioPlayer.mediaplayer = app.CreateMediaPlayer();
audioPlayer.mediaplayer.SetFile(audioPlayer.fileName);

audioPlayer.layoutControls = app.CreateLayout("linear","Horizontal,FillXY");
audioPlayer.layoutControls.SetGravity("HCenter");

audioPlayer.mainLayout = app.CreateLayout("linear","Vertical");

audioPlayer.btnReset = app.CreateButton("[fa-step-backward]",-1,-1,"FontAwesome");
audioPlayer.btnReset.SetOnTouch(function () {audioPlayer.btnstouch("reset")});

audioPlayer.btnRewind = app.CreateButton("[fa-backward]",-1,-1,"FontAwesome");
audioPlayer.btnRewind.SetOnTouch(function () {audioPlayer.btnstouch("rewind")});

audioPlayer.btnPlay = app.CreateButton("[fa-play]",-1,-1,"FontAwesome");
audioPlayer.btnPlay.SetOnTouch(function () {audioPlayer.btnstouch("play")});

audioPlayer.btnPause = app.CreateButton("[fa-pause]",-1,-1,"FontAwesome");
audioPlayer.btnPause.SetOnTouch(function () {audioPlayer.btnstouch("pause")});

audioPlayer.btnStop = app.CreateButton("[fa-stop]",-1,-1,"FontAwesome");
audioPlayer.btnStop.SetOnTouch(audioPlayer.btnsontouch);
audioPlayer.btnStop.SetOnTouch(function () {audioPlayer.btnstouch("stop")});

audioPlayer.btnFastForward = app.CreateButton("[fa-forward]",-1,-1,"FontAwesome");
audioPlayer.btnFastForward.SetOnTouch(function () {audioPlayer.btnstouch("forward")});

audioPlayer.btn = app.CreateButton("[fa-forward]",-1,-1,"FontAwesome");
audioPlayer.btnFastForward.SetOnTouch(function () {audioPlayer.btnstouch("forward")});
//"/sdcard/Download/Scripts/IMG_0939.MOV"

audioPlayer.btnFastForward = app.CreateButton("[fa-forward]",-1,-1,"FontAwesome");
audioPlayer.btnFastForward.SetOnTouch(function () {audioPlayer.btnstouch("forward")});

audioPlayer.btnLoad = app.CreateButton("[fa-folder-open]",-1,-1,"FontAwesome");
audioPlayer.btnLoad.SetOnTouch(function () {audioPlayer.btnstouch("load");});


audioPlayer.duration = audioPlayer.mediaplayer.GetDuration()
audioPlayer.barSeek = app.CreateSeekBar(1,-1);
audioPlayer.barSeek.SetRange(1.0);
audioPlayer.barSeek.SetOnTouch(function () {audioPlayer.btnstouch("seek");});

audioPlayer.trackInfoLabel = app.CreateList("Track Info:");
audioPlayer.trackInfoLabel.SetEnabled(false)
audioPlayer.trackInfo = app.CreateText("");
audioPlayer.trackInfo.SetTextSize(14.4)
audioPlayer.trackFormatLabel = app.CreateList("Track Format:");
audioPlayer.trackFormatLabel.SetEnabled(false);
audioPlayer.trackFormat = app.CreateText("");
audioPlayer.trackFormat.SetTextSize(14.4)

audioPlayer.layoutControls.AddChild(audioPlayer.btnReset);
audioPlayer.layoutControls.AddChild(audioPlayer.btnRewind);
audioPlayer.layoutControls.AddChild(audioPlayer.btnPlay);
audioPlayer.layoutControls.AddChild(audioPlayer.btnPause);
audioPlayer.layoutControls.AddChild(audioPlayer.btnStop);
audioPlayer.layoutControls.AddChild(audioPlayer.btnFastForward);
audioPlayer.mainLayout.AddChild(audioPlayer.layoutControls);
audioPlayer.layoutControls.AddChild(audioPlayer.btnLoad);
audioPlayer.mainLayout.AddChild(audioPlayer.barSeek);
audioPlayer.mainLayout.AddChild(audioPlayer.trackInfoLabel);
audioPlayer.mainLayout.AddChild(audioPlayer.trackInfo);
audioPlayer.mainLayout.AddChild(audioPlayer.trackFormatLabel);
audioPlayer.mainLayout.AddChild(audioPlayer.trackFormat)
app.AddLayout(audioPlayer.mainLayout);
}
audioPlayer.update = function () {


if( audioPlayer.mediaplayer.GetDuration())
audioPlayer.barSeek.SetValue( audioPlayer.mediaplayer.GetPosition() / audioPlayer.mediaplayer.GetDuration());
}
audioPlayer.btnstouch = function (title){

switch(title){
case "reset":
audioPlayer.mediaplayer.SeekTo(0);
break;
case "rewind":
audioPlayer.mediaplayer.SeekTo(audioPlayer.mediaplayer.GetPosition() - 30);
break;
case "play":
audioPlayer.mediaplayer.Play();
break;
case "pause":
audioPlayer.mediaplayer.Pause();
break;
case "stop":
audioPlayer.mediaplayer.Stop();

break;
case "forward":
audioPlayer.mediaplayer.SeekTo(audioPlayer.mediaplayer.GetPosition() + 30 );
break;
case "load":
app.ChooseFile(null,"", function (fileName) {
audioPlayer.mediaplayer.SetFile(fileName);
var trackFormat= fileName.substring(fileName.lastIndexOf( ".") + 1)
var trackName = fileName.substring(fileName.lastIndexOf( "/")+ 1, fileName.lastIndexOf( "."))
audioPlayer.trackInfo.SetText(trackName);
audioPlayer.trackFormat.SetText(trackFormat);

audioPlayer.mediaplayer.Play();
})
break;
case "seek":

audioPlayer.mediaplayer.SeekTo( audioPlayer.mediaplayer.GetDuration() * audioPlayer.barSeek.GetValue() );
setInterval( "audioPlayer.update()", 1000);
break;
}//end.switch

}//end audioPlayer.btnstouch
} //audioPlayer.end

Jared

unread,
Aug 30, 2018, 7:49:37 AM8/30/18
to DroidScript
oops, remove first line or fix comment to work

Bill DeWitt

unread,
Aug 30, 2018, 12:26:42 PM8/30/18
to DroidScript
Thanks! I'll look this over!

I'm trying to build a DJ function for my music, where the voice comes on between every few songs and tells me what tracks are going to be played, adds in the time and weather, or other stuff like that.  So I have a lot of work to do. I'm sure this example will help get me started!

Thanks again.  

Bill DeWitt

unread,
Sep 2, 2018, 11:48:32 AM9/2/18
to DroidScript
I still haven't found anything about ID3 tag reading. Perhaps this is a good area for a plugin?

Bill DeWitt

unread,
Sep 2, 2018, 4:12:43 PM9/2/18
to DroidScript
Hmm... just read https://wiki.droidscript.me.uk/doku.php?id=sample_code:random_file_acces

Which makes me think I can actually read the id3 info out of an mp3 file. I read elsewhere that the id3 container has different varieties, so I guess I have to test them all. Basically I will be trying to read the last 128 or first 256 bytes as text. 

That should fill up my spare time for tomorrow.... at least.

BareK

unread,
Sep 2, 2018, 4:14:05 PM9/2/18
to DroidScript
You should have a look at the MediaStore sample.
It allows you to retrieve such as:

title
albumId
album
artistId
artist
duration
size
uri

BareK

unread,
Sep 2, 2018, 4:58:11 PM9/2/18
to DroidScript
And maybe this can help you too (even if I don't believe that it is an universal solution):
Test ID3 Tags.spk

Bill DeWitt

unread,
Sep 2, 2018, 5:06:08 PM9/2/18
to DroidScript
Sorry, I don't know how to use that file. I tried moving it to my phone and opening it, but that didn't seem to do anything. 

BareK

unread,
Sep 2, 2018, 5:17:31 PM9/2/18
to DroidScript
This is an spk, a zip file containing the elements of a DroidScript project.
Normally you only have to download it on your device then selecting DroidScript to open it.
You can also open it on a computer, rename it in .zip and extract the files manually (the .js files will have .js.txt extension).

Bill DeWitt

unread,
Sep 2, 2018, 6:01:36 PM9/2/18
to DroidScript
Ah! Got it! Thanks. 
Oh, that looks like it has already done all the work I was setting down to do.

Jared

unread,
Sep 2, 2018, 7:10:01 PM9/2/18
to DroidScript
Bill, I think this library has a lot of potential, and may help you create what you have in mind. I set up a DS html app using it and got it working pretty easily, you could make a nice interface js app that loads the html files in a web container or something like that.

https://wavesurfer-js.org

Here is the basic code I put together just to get it working basic through steaming a remote file url (it was in the examples section from the site listed above).

It seems to have a lot of UI features and plugins that seem really great for DJ apps.

Hope that helps!

code:

<html>
<head>
<meta name="viewport" content="width=device-width">
<script src='file:///android_asset/app.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/2.0.6/wavesurfer.min.js"></script>

</head>

<script>
//Called after application is started.
function OnStart()
{
var wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});

wavesurfer.load('http://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3')

wavesurfer.on('ready', function () {
wavesurfer.play();
});
}
</script>

<style>

</style>

<body onload="app.Start()">

<div id="waveform"></div>

</body>
</html>

Message has been deleted
Message has been deleted

Jared

unread,
Sep 2, 2018, 11:11:59 PM9/2/18
to DroidScript
another example of that library being used
playeraud.spk

Jared

unread,
Sep 2, 2018, 11:22:57 PM9/2/18
to DroidScript
@barek,how did you load a umd module so easily... does/has app.Script always been sufficient without modifications?

im surprised 🤨, please share how you did that if you don't mind :D,

I have an app that finds and downloads modules from a json api, but haven't been able to get modules to run (npm modules /umd /cdn/etc)

if it's a big answer, you should just email me hltde...@gmail.com so i don't hijack this thread :}

BareK

unread,
Sep 3, 2018, 6:40:51 AM9/3/18
to DroidScript
Don't get too excited, I just used the "browser" version of jDataView:
https://github.com/jDataView/jDataView/tree/master/dist

I don't know another way at the moment sorry :/

Bill DeWitt

unread,
Sep 3, 2018, 9:56:24 AM9/3/18
to DroidScript
Ooo... I appreciate this @jared, but I'm afraid it might be a little bit off track. I'm already in deeper than I should be. 

I might be thinking of a different definition of DJ. I don't mean the guy who performs at a party, I mean the person on the radio who just announces stuff between songs.

<song ends>. "That was Blahblahblah from the album Blahblah. It's 10:05 am and the weather is sunny. You have a new email from Blah. Next up is Blah Blahblah followed by The BlahBlahs." <song begins>

I'm even thinking I don't want to use android's mediastore.  I can't get it not to include my phone and classroom audio recordings. I want it to play a randomly generated list from my music folders on SD without finding every possible media file on my phone.

I'm quite a newbie and have to plod through everything. So I will study this some more. And thanks again for the suggestion.

Steve Garman

unread,
Sep 3, 2018, 11:10:47 AM9/3/18
to DroidScript
To stop mediastore indexing your media, put a file
.nomedia
in the folder with the content

Bill DeWitt

unread,
Sep 3, 2018, 11:31:22 AM9/3/18
to DroidScript
See? Why didn't any of the 30-40 sites I tried to find this answer on, actually give that answer? Thanks!

To be clear, is this a text file with ".nomedia" in it or a file with the name ",nomedia"?

Oh, I guess I could test it. I think a restart of the phone causes mediastore to refresh it's content?

Jared

unread,
Sep 3, 2018, 1:13:36 PM9/3/18
to DroidScript
It is far easier if you get a file explorer. If you use this one (Mix), it has an option in the menu bar to make nomedia files, or just a new file and name it .nomedia.




I would highly recommend that one, it's excellent also solid explorer. This is the classic version [Link], which personally I find to be far better than the newer material design one [link]. 




Bill DeWitt

unread,
Sep 3, 2018, 4:42:01 PM9/3/18
to DroidScript
Grr... I still have to build a playlist using the ID3 tags, at least so that I can rank songs by my playing habits. Or maybe write to the ID3 tag to store my ranking.  

Netpower8

unread,
Sep 3, 2018, 11:12:32 PM9/3/18
to DroidScript
No need to store the ranking on id tag. Just create a json file and put the ranking there and save the file

Netpower8

unread,
Sep 3, 2018, 11:14:45 PM9/3/18
to DroidScript
By putting it a seperate file its easier to reset the stats. If you put it inside the id tag. You have to reset all the id tags. What id you have 100 songs. It will take a while to process all files. By putting the ranking in a seperate file resetting it is simply erase the file.

Bill DeWitt

unread,
Sep 4, 2018, 9:41:30 AM9/4/18
to DroidScript
Yes, json is a good idea. Thanks! I was going to make a database, but json sounds easier. 

Because I don't see any way to write to mediastore and writing to the ID3 is tedious.

But the rankings (I should say "rating") I am thinking of, don't need to ALL be updated (I have thousands of songs). I would just rate each one as I listen to it. Each time I press <next> instead of listening to a song, it drops the rating and is less likely to be put on the playlist for the following days. Pressing <previous> to hear it again, increases the rating.

The idea is to eventually delete files when I really don't like them. You would not believe some of the stuff I have picked up over the years.
Reply all
Reply to author
Forward
0 new messages