blob: 58b38ac411014e7b5f0addf40912f11bb4d8dd5e (
plain)
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
|
/*
* Copyright (C) 2017 Google Inc.
*
* 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.google.android.mobly.snippet.bundled;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.rpc.Rpc;
import java.io.IOException;
/* Snippet class to control media playback. */
public class MediaSnippet implements Snippet {
private final MediaPlayer mPlayer;
public MediaSnippet() {
mPlayer = new MediaPlayer();
}
@Rpc(description = "Resets snippet media player to an idle state, regardless of current state.")
public void mediaReset() {
mPlayer.reset();
}
@Rpc(description = "Play an audio file stored at a specified file path in external storage.")
public void mediaPlayAudioFile(String mediaFilePath) throws IOException {
mediaReset();
if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
mPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build());
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mPlayer.setDataSource(mediaFilePath);
mPlayer.prepare(); // Synchronous call blocks until the player is ready for playback.
mPlayer.start();
}
@Rpc(description = "Stops media playback.")
public void mediaStop() throws IOException {
mPlayer.stop();
}
@Override
public void shutdown() {}
}
|