SDA Assignment 2
SDA Assignment 2
Submitted By:
1. Rimsha Arif (FA18-BSE-006)
2. Arose Niazi (FA18-BSE-010)
Music Player
Source
Arose-Niazi/Music-Player-Unity (github.com)
Description
This Music Player application was developed just for learning purposes. This application is
developed in unity.
Language
C#
Platform
Can be compiled and run into any unity supported platforms.
Connectors Use
d
1. Procedure call..................................................................................................................................2
1.1. Code.........................................................................................................................................2
1.2. Explanation..............................................................................................................................2
2. Data access......................................................................................................................................3
2.1. Code.........................................................................................................................................3
2.2. Explanation..............................................................................................................................3
3. Event................................................................................................................................................3
3.1. Code.........................................................................................................................................3
3.2. Explanation..............................................................................................................................3
4. Stream.............................................................................................................................................3
4.1. Code.........................................................................................................................................3
4.2. Explanation..............................................................................................................................4
5. Linkage.............................................................................................................................................4
5.1. Code.........................................................................................................................................4
5.2. Explanation..............................................................................................................................4
6. Arbitrator.........................................................................................................................................4
6.1. Code.........................................................................................................................................4
6.2. Explanation..............................................................................................................................4
Connectors Explained
1. Procedure call
1.1. Code
Call
if (!_currentPlayList.AddSong(song))
Debug.LogWarning("Already in playlist");
Function
public bool AddSong(Song song)
{
foreach (Song oldSongs in _songs)
{
if (oldSongs.GetName().Equals(song.GetName()))
return false;
}
_songs.Add(song);
return true;
}
1.2. Explanation
This connector is performing both coordination and communication. The AddSong function is called on
the Playlist class’s object. If the song is already in the playlist, it will not add it and return false, else will
return true. The condition where it is called check if the function returns false, negates it, and logs and
warning message.
2. Data access
2.1. Code
string connectionString =
"Server=51.210.180.137;" +
"Database=MusicPlayer;" +
"User ID=MusicPlayer;" +
"Password=Rimsha#Arose;" +
"Pooling=false";
IDbConnection dbcon;
dbcon = new MySqlConnection(connectionString);
dbcon.Open();
2.2. Explanation
This connector is used to access the data, the connection string is defined which hold all the details
regarding how and what data to be accessed, while the MySqlConnection object is created and then
using Open function the data is accessed.
3. Event
3.1. Code
btn.GetComponent<Button>().onClick.AddListener(
delegate
{
LoadPlayList(playlist);
});
3.2. Explanation
This code here, btn is an object of class GameObject. The GameObject has GetComponent Function
which gets the Button component which has an onclick event binder. A listener is added to it that
delegates the LoadPlayList function, the playlist to load is passed as a parameter.
4. Stream
4.1. Code
public void Load(Script mainScript)
{
string path = "playlists/" + _name + ".playlist";
StreamReader reader = new StreamReader(path);
while (!reader.EndOfStream)
{
mainScript.AddSong(reader.ReadLine());
}
reader.Close();
}
4.2. Explanation
This connector is performing communication through the parameters. This function creates a stream
between file and program using StreamReader, the path is passed the file is opened and each line is
read at a time until the end of the file is reached. In the end, the file is closed.
5. Linkage
5.1. Code
public class BarSpawner : MonoBehaviour
5.2. Explanation
There is a direct linkage between the BarSpawner class and the MonoBehaviour class. This is a parent-
child linkage example.
6. Arbitrator
6.1. Code
Call
StartCoroutine(PlaySong(_currentPlayList.GetSong((_currentIndex+1) %
_currentPlayList.TotalSongs())));
Function
IEnumerator PlaySong(Song song)
{
_currentIndex = _currentPlayList.Find(song);
using (UnityWebRequest www =
UnityWebRequestMultimedia.GetAudioClip(song.GetPath(), song.GetType()))
{
yield return www.SendWebRequest();
AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
audioPlayer.clip = myClip;
songTimer.maxValue = myClip.length;
_beforePauseValue = -1f;
songTimer.value = 0;
if(!_pause) audioPlayer.Play();
_started = true;
}
}
6.2. Explanation
This creates a thread that starts the multi-tasking process, goes into PlaySong, searches for the song
which we need to play. Sends a request to read the AudioClip, suspends the thread until the request for
getting the song is completed. Once it is completed it just assigns the clip to the audio player and starts
it, it is not paused.