Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Custom Mobile Audio Player for Unity3d

  • You cannot reply to this topic
No replies to this topic

#1 Frostbite23   Members   -  Reputation: 111

Like
0Likes
Like

Posted Today, 04:27 PM

Hello code veterans, Im currently working on a project that requires the use of an Audio player in unity3d. Since Im doing this audio player from scratch I have the Play, Pause, Stop and the seekbar working perfectly. (seekbar took an entire day breaking my head off on trying to write a formula to calculate the time and seekbar width accurately).

 

However since this is on mobile, the user has to be able to move the bar circle to the desired time the user wants to listen to. Kind of like youtube where you want to skip to your favorite part of a video by moving the circle. Ive been stuck on this for around 1 1/4 day's and Ive been dead stuck, only making tiny progress. Heres my Bar circle code as well as a bit of my play pause code.

 

Barcircle

#pragma strict

import PlayPause;
var script : PlayPause;
var sensisitivity : float = 300.0f;

var timeCaptured : float = 0.0f;
var totalTimey : float = 0.0f;

var minX : float = 114.5f;
var maxX : float = 0.0f;

function LateUpdate () {
	if(Input.GetButtonDown("Fire1")){
		timeCaptured = script.capturedTime;
	}
	if(Input.GetButton("Fire1")){
		totalTimey = script.gameObject.audio.clip.length; //using a varible = the length of the audioclip (which is read only)
		totalTimey -= script.gameObject.audio.time; //when the audio starts, the time subtracts the length. which makes it count down.
		
		script.gameObject.audio.time = timeCaptured;
		var h : float = sensisitivity * Input.GetAxis("Mouse X");
		gameObject.guiTexture.pixelInset.x += h;
		script.capturedTime = (script.gameObject.audio.time * script.gameObject.guiTexture.pixelInset.x)/script.gameObject.audio.clip.length;
	}
}

and a bit from the playpause

private var paused = false;

public var capturedTime : float = 0;
private var totalTime : float = 0;
private var clickChecker : float = 0;
private var seekbarTime : float = 0;

function Start () {
	gameObject.audio.clip = clip; //insert the clip to the audio source
	play.guiTexture.enabled = true; //play gui
	pause.guiTexture.enabled = false; //stop gui
	seekbar.guiTexture.pixelInset.width = seekbarLimit; //for visual purposes
}

function LateUpdate () {
	totalTime = gameObject.audio.clip.length; //equal the varible to the length of the audioclip (which is read only)
	totalTime -= gameObject.audio.time; //when the audio starts, the time subtracts the length. which makes it count down.
	audioTime.guiText.text = totalTime.ToString(); //just for displaying text
	seekbarTime = gameObject.audio.time;
	
	if(paused == false){
		capturedTime = gameObject.audio.time; //for saving audio time
	}
	else if(paused){
		capturedTime = capturedTime; //when audio is paused, catch the time
	}
	
	seekbar.guiTexture.pixelInset.width = (gameObject.audio.time * seekbarLimit)/gameObject.audio.clip.length; //calculating the time for the seekbar ACCURATLY
	seekbar.guiTexture.pixelInset.width = Mathf.Clamp(seekbar.guiTexture.pixelInset.width, 0.0f, seekbarLimit); //clamp the seekbar so it doesnt go over its boundarys
}

Please excuse my lousy and messy coding but can anyone suggest or think of a solution that best helps what I'm currently trying to do please and thank you :)



Sponsor:




PARTNERS