1/*******************************************************************************
2 * Copyright 2013 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.backends.iosrobovm;
18
19import org.robovm.apple.foundation.NSObject;
20
21import com.badlogic.gdx.Gdx;
22import com.badlogic.gdx.audio.Music;
23import com.badlogic.gdx.backends.iosrobovm.objectal.AVAudioPlayerDelegateAdapter;
24import com.badlogic.gdx.backends.iosrobovm.objectal.OALAudioTrack;
25
26/** @author Niklas Therning */
27public class IOSMusic implements Music {
28	private final OALAudioTrack track;
29	OnCompletionListener onCompletionListener;
30
31	public IOSMusic (OALAudioTrack track) {
32		this.track = track;
33		this.track.setDelegate(new AVAudioPlayerDelegateAdapter() {
34			@Override
35			public void didFinishPlaying (NSObject player, boolean success) {
36				final OnCompletionListener listener = onCompletionListener;
37				if (listener != null) {
38					Gdx.app.postRunnable(new Runnable() {
39						@Override
40						public void run () {
41							listener.onCompletion(IOSMusic.this);
42						}
43					});
44				}
45			}
46		});
47	}
48
49	@Override
50	public void play () {
51		if (track.isPaused()) {
52			track.setPaused(false);
53		} else if (!track.isPlaying()) {
54			track.play();
55		}
56	}
57
58	@Override
59	public void pause () {
60		if (track.isPlaying()) {
61			track.setPaused(true);
62		}
63	}
64
65	@Override
66	public void stop () {
67		track.stop();
68	}
69
70	@Override
71	public boolean isPlaying () {
72		return track.isPlaying() && !track.isPaused();
73	}
74
75	@Override
76	public void setLooping (boolean isLooping) {
77		track.setNumberOfLoops(isLooping ? -1 : 0);
78	}
79
80	@Override
81	public boolean isLooping () {
82		return track.getNumberOfLoops() == -1;
83	}
84
85	@Override
86	public void setVolume (float volume) {
87		track.setVolume(volume);
88	}
89
90	@Override
91	public void setPosition (float position) {
92		track.setCurrentTime(position);
93	}
94
95	@Override
96	public float getPosition () {
97		return (float)(track.getCurrentTime());
98	}
99
100	@Override
101	public void dispose () {
102		track.clear();
103	}
104
105	@Override
106	public float getVolume () {
107		return track.getVolume();
108	}
109
110	@Override
111	public void setPan (float pan, float volume) {
112		track.setPan(pan);
113		track.setVolume(volume);
114	}
115
116	@Override
117	public void setOnCompletionListener (OnCompletionListener listener) {
118		this.onCompletionListener = listener;
119	}
120
121}
122