当前位置:网站首页>There are applications related to web network request API in MATLAB (under update)

There are applications related to web network request API in MATLAB (under update)

2022-06-26 05:26:00 Wake wrist

Matlab There are about web Network request API Related applications of

1、webread Read the content

from RESTful Web service

data = webread(url) 

from url designated Web The service reads the content and displays it in data Content returned in .
Returns an array of structures (struct array) And so on

If you want to store data , For example, save as Excel file

xlswrite  %( Not recommended ) write in  Microsoft Excel  Spreadsheet files 

It is not recommended to use xlswrite. Please switch to writetable、writematrix or writecell.

xlswrite(filename,A) 
 The matrix  A  write in  Microsoft Excel  Spreadsheet workbook  filename  The first worksheet in , From cell  A1  Start writing .

writetable(T)
 Will table  T  Write comma separated text files . The file name is the workspace variable name of the table , Additional extensions  .txt.

writetable(T,filename)
 Write with  filename  Specify the name and extension of the file .
# # First argument must be a table.  struct2table(data)

writetable(___,Name,Value)
 Through one or more  Name,Value  Other options specified for the group parameter write the table to a file , And can include any input parameter in the previous Syntax .
  1. data = webread(url) Get data from network request
  2. table = struct2table(data) Structure array is converted to table form
  3. writetable(table,filename) take table The table is written to the file
data = webread(url,QueryName1,QueryValue1,...,QueryNameN,QueryValueN)

According to one or more names - Value appends the query parameter to... In the way specified for the group parameter url. To put the query into the message body , Please use webwrite.Web Service definition query parameters .

2、webread Call Netease cloud API

First pass Matlab Read music , And realize the function of playing .

[y,Fs] = audioread(filepath)
 From the name  filename  Read the data from the file in the , And return the sample data  y  And the sampling rate of the data  Fs.

player = audioplayer(y,Fs)
 Use sampling rate  Fs  Is the signal  Y  establish  audioplayer  object . This function returns the audio player object  player.

play(player)	 Start playing songs 

stop(player)	 Stop playing the song 

pause(player)	 Pause the song 

resume(player)	 Continue playing the song 

Our method this time is to call Netease cloud music API Interface , Get music source file .

Interface : Process the processed interface : from https://api.vvhan.com/ Provide

 Request example  (mp3  Output ):
https://api.vvhan.com/api/rand.music?sort= Hot song list 

 Request example  (JSON  Output ):
https://api.vvhan.com/api/rand.music?type=json&sort= Hot song list 

The specific process :

data = webread("https://api.vvhan.com/api/rand.music?type=json&sort= Hot song list ")
 Get the data returned by the interface , With variable data preservation 

data The data are as follows :
 Insert picture description here
data.info The data are as follows :
 Insert picture description here

name : The name of the song

auther : The singer name of the song

picUrl : The cover picture of the song Address

mp3url : Song source file MP3 Address

Simple test playback code :

data = webread("https://api.vvhan.com/api/rand.music?type=json&sort= Hot song list ");
music_infor = data.info
%  Get the digital signal of audio y, And sampling frequency Fs
[y,Fs] = audioread(music_infor.mp3url); 
%  Use sampling rate  Fs  Is the signal  Y  establish  audioplayer  object . This function returns the audio player object  player.
player = audioplayer(y,Fs); 
%  Start playing   Audio player object  player
play(player);

3、GUI Random music player

First , introduce player Object functions for

player Object functions for player Introduction to the object function of
get Inquire about audioplayer Property value of object
isplaying Inquire about audioplayer Property value of object
pause Pause playback or recording
play from audioplayer Object to play audio
playblocking Play audioplayer Object , Keep control until playback is complete
resume Resume playback or recording from a paused state
set Set up audioplayer Property value of object
stop Stop playing or recording

Another introduction player Related object properties of

BitsPerSample -  Number of sampling bits .		 Positive integer 

CurrentSample -  Currently playing samples .	 Positive integer 

DeviceID -  Audio device identifier .		 Integers 

NumChannels -  Number of audio channels 			1 | 2

Running -  Audio player status 			on | off

SampleRate -  sampling frequency 			 Numerical scalar 

TotalSamples -  Total length of audio data 	 Integers 

Tag -  label 						 Character vector 

Type -  Object class name 					'audioplayer'

UserData -  User defined data 			[] ( Default ) |  Any data type 

StartFcn -  The function to execute at the beginning 	 Character vector  |  String scalar  |  Function handle 

StopFcn -  The function to execute at the end 		 Character vector  |  String scalar  |  Function handle 

TimerFcn -  The function to be repeated 		 Character vector  |  String scalar  |  Function handle 

TimerPeriod -  Timer cycle 			0.05 ( Default ) |  Numerical scalar 

Gui The interface design is as follows :
 Insert picture description here
Get the code for the new song :

	global player time
	data = webread("https://api.vvhan.com/api/rand.music?type=json&sort= Hot song list ");
	music_infor = data.info;
	app.MusicName.Text = music_infor.name;
	app.MusicSinger.Text = music_infor.auther;
	%  Get the digital signal of audio y, And sampling frequency Fs
	[y,Fs] = audioread(music_infor.mp3url);
	time = (length(y(:,1))/Fs);
	
	Logo=imread(music_infor.picUrl);
	app.MusicLogo.ImageSource = Logo;
	app.MusicTime.Text = append('0:00',' / ',app.sec2ms(time));
	%  Use sampling rate  Fs  Is the signal  Y  establish  audioplayer  object . This function returns the audio player object  player.
	
	app.MusicHistory.Items(end+1) = {music_infor.name};
	player = audioplayer(y,Fs); 
	play(player)
	delete(timerfind)
	function process(timer,event)
	    app.MusicProcess.Value = (player.CurrentSample / player.TotalSamples);
	    app.MusicTime.Text = append(app.sec2ms(time*(player.CurrentSample / player.TotalSamples)),' / ',app.sec2ms(time));
	end
	
	t = timer('StartDelay', 1,'Period', 1,'TasksToExecute', Inf,'ExecutionMode','fixedRate');
	t.TimerFcn = @ process;
	
	start(t)

原网站

版权声明
本文为[Wake wrist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180506485298.html