My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.io.Mpg123Decoder;
import com.badlogic.gdx.audio.io.VorbisDecoder;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.tests.utils.GdxTest;

/**
* Demonstrates how to read and playback an OGG file with the {@link VorbisDecoder} found
* in the gdx-audio extension.
* @author mzechner
*
*/
public class Mpg123Test extends GdxTest {
/** the file to playback **/
private static final String FILE = "data/8.12.mp3";
/** a VorbisDecoder to read PCM data from the ogg file **/
Mpg123Decoder decoder;
/** an AudioDevice for playing back the PCM data **/
AudioDevice device;

@Override
public void create () {
// copy ogg file to SD card, can't playback from assets
FileHandle externalFile = Gdx.files.external("tmp/test.mp3");
Gdx.files.internal(FILE).copyTo(externalFile);

// Create the decoder and log some properties. Note that we need
// an external or absolute file
decoder = new Mpg123Decoder(externalFile);
Gdx.app.log("Mp3", "channels: " + decoder.getChannels() + ", rate: " + decoder.getRate() + ", length: " + decoder.getLength());

// Create an audio device for playback
device = Gdx.audio.newAudioDevice(decoder.getRate(), decoder.getChannels() == 1? true: false);

// start a thread for playback
Thread playbackThread = new Thread(new Runnable() {
@Override
public void run() {
int readSamples = 0;
// we need a short[] to pass the data to the AudioDevice
short[] samples = new short[2048];

// read until we reach the end of the file
while((readSamples = decoder.readSamples(samples, 0, samples.length)) > 0) {
// write the samples to the AudioDevice
device.writeSamples(samples, 0, readSamples);
}
}
});
playbackThread.setDaemon(true);
playbackThread.start();
}

@Override
public void dispose() {
// we should synchronize with the thread here
// left as an excercise to the reader :)
device.dispose();
decoder.dispose();
// kill the file again
Gdx.files.external("tmp/test.mp3").delete();
}

@Override
public boolean needsGL20 () {
return false;
}
}

Change log

r3251 by badlogicgames on Jan 8, 2012   Diff
[fixed] minor fix in Mpg123Test
Go to: 
Project members, sign in to write a code review

Older revisions

r3250 by badlogicgames on Jan 8, 2012   Diff
[added] new test mp3 and oggs
[changed] Mpg123Test now same as
VorbisTest.
r3035 by badlogicgames on Dec 26, 2011   Diff
[removed] vorbis and mpg123 decoder
from gdx core
r2645 by badlogicgames on Sep 4, 2011   Diff
[updated] to latest box2d version. See
gdx/jni/Box2D/CHANGES
All revisions of this file

File info

Size: 3065 bytes, 87 lines
Powered by Google Project Hosting