Exoplayerを使用して、バイト配列からオーディオファイルを再生しようとしています。私はByteArrayDataSourceを使用しようとしているが、コンストラクタを呼び出すときにエラーを取得しています:ここでnew ByteArrayDataSource(data);
は、私が作ってみたコードは次のとおりです。Exoplayerがバイト配列からオーディオを再生する - ByteArrayDataSource
private void prepareExoPlayerFromByteArray(byte[] data){
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
exoPlayer.addListener(eventListener);
final ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(data);
/*
DataSpec dataSpec = new DataSpec(byteArrayDataSource.getUri());
try {
byteArrayDataSource.open(dataSpec);
} catch (IOException e) {
e.printStackTrace();
}
*/
DataSource.Factory factory = new DataSource.Factory() {
@Override
public DataSource createDataSource() {
return byteArrayDataSource;
}
};
MediaSource audioSource = new ExtractorMediaSource(byteArrayDataSource.getUri(),
factory, new DefaultExtractorsFactory(),null,null);
exoPlayer.prepare(audioSource);
}
私は取得していますエラーはこれです:
E/ExoPlayerImplInternal: Internal runtime error.
java.lang.NullPointerException
at com.google.android.exoplayer2.util.Assertions.checkNotNull(Assertions.java:107)
at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.<init>(ExtractorMediaPeriod.java:591)
at com.google.android.exoplayer2.source.ExtractorMediaPeriod.startLoading(ExtractorMediaPeriod.java:452)
at com.google.android.exoplayer2.source.ExtractorMediaPeriod.prepare(ExtractorMediaPeriod.java:165)
at com.google.android.exoplayer2.ExoPlayerImplInternal.maybeUpdateLoadingPeriod(ExoPlayerImplInternal.java:1260)
at com.google.android.exoplayer2.ExoPlayerImplInternal.updatePeriods(ExoPlayerImplInternal.java:1102)
at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:447)
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:300)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:234)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.google.android.exoplayer2.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40)
エラーは、ByteArrayDataSourceのコンストラクタから来ているようで、渡されたデータがnullであると言っています。これは、呼び出し前にチェックしていないので、その配列に14002427バイトあります。
/**
* @param data The data to be read.
*/
public ByteArrayDataSource(byte[] data) {
Assertions.checkNotNull(data);
Assertions.checkArgument(data.length > 0);
this.data = data;
}
私は間違っていますが、どうすれば回避できますか?誰かがExoplayerでオーディオファイルを再生する作業例がありますか?バイト配列をソースとして渡しますか?
@pskink私はMediaPlayerのを使用しなかった場合でも、あなたが提案した方法は、バイト配列を取ることはありません。無関係なtho、私はExoplayerを使用してこれを行う必要があります。 – ak93
これにはどんな解決策ですか?私はまったく同じ問題に直面しています – Chandru
私の答えはOKの場合は参照してください、次にupvoteしてください。あなたに感謝:) – RaghavPai