Java ExoPlayer测试-Can';t在未调用Looper.prepare()的线程内创建处理程序
我正在尝试测试我创建的一个类,该类实例化了一个新的Java ExoPlayer测试-Can';t在未调用Looper.prepare()的线程内创建处理程序,java,android,multithreading,exoplayer,Java,Android,Multithreading,Exoplayer,我正在尝试测试我创建的一个类,该类实例化了一个新的SimpleExoPlayer对象,下面是该类: public class PlayerFactory { public static SimpleExoPlayer getPlayerInstance(Context context, Uri fileUri, String encryptionProtocol, byte[] secretKey, byte[] iv) throws InvalidAlgorit
SimpleExoPlayer
对象,下面是该类:
public class PlayerFactory {
public static SimpleExoPlayer getPlayerInstance(Context context, Uri fileUri, String encryptionProtocol, byte[] secretKey, byte[] iv)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
//Controls track changes
TrackSelector trackSelector = new DefaultTrackSelector();
//Controls buffering
LoadControl loadControl = new DefaultLoadControl();
//Extracts data from data source
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//Factory of EncryptedFileDataSource
DataSource.Factory encryptedFileDataSourceFactory = new EncryptedFileDataSourceFactory(encryptionProtocol, secretKey, iv, null);
MediaSource mediaSource = new ExtractorMediaSource(fileUri, encryptedFileDataSourceFactory, extractorsFactory, null, null);
//Creates a SimpleExoPlayer instance
SimpleExoPlayer simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);
//Attache media source to player
simpleExoPlayer.prepare(mediaSource);
return simpleExoPlayer;
}
}
然后在PlayerTest.java
类中,我尝试测试PlayerFactory.getPlayerInstance()
调用的结果对象是否等于SimpleExoPlayer
类对象
以下是测试:
@RunWith(AndroidJUnit4.class)
public class PlayerTest {
//Encryption algorithm
static final String ENC_ALG = "AES";
//Encryption block mode
static final String ENC_PROTOCOL = ENC_ALG + "/CTR/NoPadding";
//File to be played
static final String ENC_FILE = "song_encrypted_ctr.mp3";
//Secrect key used in encryption
public static final String mKey = "0123456789012345";
//Initialization vector used in encryption
public static final String mIV = "abcdefrtyqowueyr";
@Test
public void testCorrectIqraalyPlayerObject() throws Throwable {
Context context = InstrumentationRegistry.getContext();
SimpleExoPlayer simpleExoPlayer = PlayerFactory.getPlayerInstance(context, Uri.parse("testuri"), ENC_PROTOCOL, mKey.getBytes(), mIV.getBytes());
Assert.assertEquals(SimpleExoPlayer.class.getName(), simpleExoPlayer.getClass().getName());
}
但是我总是得到一个运行时异常
:
java.lang.RuntimeException:无法在该线程内创建处理程序
尚未调用Looper.prepare()
在这一行:
SimpleExoPlayer simpleExoPlayer = PlayerFactory.getPlayerInstance(context, Uri.parse("testuri"), ENC_PROTOCOL, mKey.getBytes(), mIV.getBytes());
我注意到在PlayerFactory
中的ExoPlayerFactory.newSimpleInstance()
方法中有一个新的处理程序创建和传递,我不知道它的用途。如果有人知道为什么会这样,我会很感激的。谢谢
更新: 我尝试使用
@UiThreadTest
而不是@Test
,但出现以下错误:
java.lang.Exception:没有可运行的方法
更新2: 我试图同时使用
@UiThreadTest
和@Test
我得到:
给定的调用程序包com.example.rafael.exoplayerpoc.test不可用
正在进程ProcessRecord{c32179c中运行
10198:com.example.rafael.exoplayerpoc/u0a58}
使用
UiThreadTestRule
在UI线程上运行测试。更多详细信息请查看更新。我得到了没有可运行的方法
您实际上需要这两种方法我得到了另一个错误:给定的调用程序包com.example.rafae.exoplayerpoc.test没有在进程进程记录{c32179c 10198:com.example.rafael.exoplayerpoc/u0a58}中运行