gpt4 book ai didi

java - ExoPlayer 测试 - 无法在未调用 Looper.prepare() 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-11-30 00:39:20 25 4
gpt4 key购买 nike

我正在尝试测试我创建的实例化新 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());
}

但我总是得到一个RuntimeException:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

在这一行:

SimpleExoPlayer simpleExoPlayer = PlayerFactory.getPlayerInstance(context, Uri.parse("testuri"), ENC_PROTOCOL, mKey.getBytes(), mIV.getBytes());

我注意到在 PlayerFactoryExoPlayerFactory.newSimpleInstance() 方法中有一个新的 Handler 创建和传递,我不知道它是做什么用的。如果有人知道为什么会这样,我将不胜感激。谢谢。


更新:我尝试使用 @UiThreadTest 而不是 @Test,但出现了这个错误:

java.lang.Exception: No runnable methods


更新 2:我尝试同时使用 @UiThreadTest@Test 我得到了:

Given caller package com.example.rafael.exoplayerpoc.test is not running in process ProcessRecord{c32179c 10198:com.example.rafael.exoplayerpoc/u0a58}

最佳答案

使用 UiThreadTestRule 在 UI 线程上运行测试。更多详情 here

关于java - ExoPlayer 测试 - 无法在未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42795905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com