1 /* 2 * Copyright 2020 The Android Open Source Project 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 package org.hyphonate.megaaudio.player; 17 18 import android.util.Log; 19 20 import org.hyphonate.megaaudio.common.BuilderBase; 21 22 /** 23 * Class to construct contrete Player objects. 24 */ 25 public class PlayerBuilder extends BuilderBase { 26 @SuppressWarnings("unused") 27 private static final String TAG = PlayerBuilder.class.getSimpleName(); 28 @SuppressWarnings("unused") 29 private static final boolean LOG = false; 30 31 /** 32 * Provides audio data for this stream. 33 */ 34 private AudioSourceProvider mSourceProvider; 35 PlayerBuilder()36 public PlayerBuilder() { 37 } 38 39 // 40 // Player-Specific Attributes 41 // 42 /** 43 * Specifies the player type 44 * @param playerType Composed from API Types & API subtypes (defined in BuilderBase) 45 * @return this PlayerBuilder (for cascaded calls) 46 */ setPlayerType(int playerType)47 public PlayerBuilder setPlayerType(int playerType) { 48 mType = playerType; 49 return this; 50 } 51 52 /** 53 * Specifies the AudioSourceProvider which will allocate an AudioSource subclass object 54 * to provide audio data for this stream. 55 * @param sourceProvider Allocates the AudioSource to for provide audio 56 * for the created stream. 57 * @return this PlayerBuilder (for cascaded calls) 58 */ setSourceProvider(AudioSourceProvider sourceProvider)59 public PlayerBuilder setSourceProvider(AudioSourceProvider sourceProvider) { 60 mSourceProvider = sourceProvider; 61 return this; 62 } 63 64 /** 65 * Allocates an initializes an API-specific player stream. 66 * @return The allocated player or null in case of error or if a player type of TYPE_NONE 67 * is specified. 68 * @throws BadStateException if an invalid API has been specified. 69 */ build()70 public Player build() throws BadStateException { 71 if (LOG) { 72 Log.i(TAG, "build() mSourceProvider:" + mSourceProvider); 73 } 74 if (mSourceProvider == null) { 75 throw new BadStateException(); 76 } 77 78 Player player = null; 79 int playerType = mType & TYPE_MASK; 80 switch (playerType) { 81 case TYPE_NONE: 82 // NOP 83 break; 84 85 case TYPE_JAVA: 86 player = new JavaPlayer(this, mSourceProvider); 87 break; 88 89 case TYPE_OBOE: { 90 int playerSubType = mType & SUB_TYPE_MASK; 91 player = new OboePlayer(this, mSourceProvider, playerSubType); 92 } 93 break; 94 95 default: 96 throw new BadStateException(); 97 } 98 99 return player; 100 } 101 102 /** 103 * Exception class used to signal a failure to allocate an API-specific stream in the build() 104 * method. 105 */ 106 public class BadStateException extends Throwable { 107 } 108 } 109