PlayStation MobileでBGMを制御するプログラムを作る
BGMをイントロ、メイン(ループあり)、コーダの順で再生させる。
BgmPlayerクラスでもループに関する制御はあるが、別の方法を試す。
イントロ、メイン、コーダの各ファイルを順に読み込ませる。
複数のBGMに対して同時にCreatePlayerメソッドを使うとエラーが出る。
それを知らずに苦労するが、完成する。
using System;
using Sce.PlayStation.Core.Audio;
namespace Sample {
public class MyBgm {
private Bgm intro = null;
private Bgm coda = null;
private Bgm main = null;
private BgmPlayer introPlayer = null;
private BgmPlayer codaPlayer = null;
private BgmPlayer mainPlayer = null;
private bool isReady = false;
public MyBgm(string fileName) {
coda = new Bgm(fileName);
}
public MyBgm(string fileName_intro, string fileName_main, string fileName_coda) {
intro = new Bgm(fileName_intro);
main = new Bgm(fileName_main);
coda = new Bgm(fileName_coda);
}
public void Term() {
if (introPlayer!=null) {
introPlayer.Dispose();
introPlayer = null;
}
if (intro!=null) {
intro.Dispose();
intro = null;
}
if (mainPlayer!=null) {
mainPlayer.Dispose();
mainPlayer = null;
}
if (main!=null) {
main.Dispose();
main = null;
}
if (codaPlayer!=null) {
codaPlayer.Dispose();
codaPlayer = null;
}
if (coda!=null) {
coda.Dispose();
coda = null;
}
}
private void Init() {
if (intro!=null) {
introPlayer = intro.CreatePlayer();
introPlayer.Play();
} else if (coda!=null) {
codaPlayer = coda.CreatePlayer();
codaPlayer.Play();
}
}
public void Update() {
if (isReady) {
if (introPlayer!=null) {
if (introPlayer.Status==BgmStatus.Stopped) {
introPlayer.Dispose();
introPlayer = null;
mainPlayer = main.CreatePlayer();
mainPlayer.Loop = true;
mainPlayer.Play();
}
} else if (mainPlayer!=null) {
if (mainPlayer.Status==BgmStatus.Stopped) {
mainPlayer.Dispose();
mainPlayer = null;
codaPlayer = coda.CreatePlayer();
codaPlayer.Play();
}
} else if (codaPlayer!=null) {
if (codaPlayer.Status==BgmStatus.Stopped) Term();
}
} else {
isReady = true;
Init();
}
}
public void GoToCoda(bool now) {
if (mainPlayer!=null) {
if (now) {
mainPlayer.Stop();
} else {
mainPlayer.Loop = false;
}
}
}
public void SetVolume(float volume) {
if (introPlayer!=null) introPlayer.Volume = volume;
if (codaPlayer!=null) codaPlayer.Volume = volume;
if (mainPlayer!=null) mainPlayer.Volume = volume;
}
}
}
2014/05/06 01:23