英文:
error: cannot find symbol import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
问题
在构建项目时出现了错误:
error: cannot find symbol
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
^
symbol: class DefaultHttpDataSourceFactory
location: package com.google.android.exoplayer2.upstream
无法导入DefaultHttpDataSourceFactory
类,但已添加了所有依赖项。这个问题的解决方案是什么?
以下是我的Gradle依赖项:
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.testiptv'
compileSdk 33
defaultConfig {
applicationId "com.example.testiptv"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.android.volley:volley:1.2.1'
implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-hls:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-core:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer:2.11.8'
implementation 'com.google.android.exoplayer:exoplayer-core:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.4'
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.TestIPTV"
tools:targetApi="31">
<activity
android:name=".videoplayer"
android:usesCleartextTraffic="true"
android:screenOrientation="sensor"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是ExoPlayer的XML设计:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".videoplayer"
android:keepScreenOn="true">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
app:show_subtitle_button="false"
app:hide_on_touch="true"
app:use_controller="true"
app:controller_layout_id="@layout/custom_exo_view"
app:show_vr_button="false"
app:resize_mode="fill"
app:show_shuffle_button="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
这是我插入M3U链接并尝试播放ExoPlayer的Java代码:
package com.example.testiptv;
import static android.content.ContentValues.TAG;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
public class videoplayer extends AppCompatActivity {
protected PlayerView playerView;
SimpleExoPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
playerView = findViewById(R.id.player_view);
playvideo();
}
private void playvideo() {
//String videouri = getIntent().getStringExtra("keyName");
String videouri = "https://stmv.video.expressolider.com.br/ghostv/ghostv/playlist.m3u8";
try {
Uri uri = Uri.parse(videouri);
player = new SimpleExoPlayer(videoplayer.this);
playerView.setPlayer(player);
// Create an HTTPS data source factory
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
"exoplayer-codelab",
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true /* allowCrossProtocolRedirects */,
DefaultHttpDataSourceFactory.DEFAULT_SSL_SOCKET_FACTORY);
// Create a media source using the data source factory
MediaSource mediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
Log.d(TAG, "Media source created: " + (mediaSource != null));
// Set the media source for the player
player.setMediaSource(mediaSource);
// Prepare the player
<details>
<summary>英文:</summary>
There is an error when building project
```none
error: cannot find symbol
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
^
symbol: class DefaultHttpDataSourceFactory
location: package com.google.android.exoplayer2.upstream
Can not import the DefaultHttpDataSourceFactory class. but all dependencies are added. What is this problem's solution?
Here is my Gradle dependencies
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.testiptv'
compileSdk 33
defaultConfig {
applicationId "com.example.testiptv"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.android.volley:volley:1.2.1'
implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-hls:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
// implementation 'com.google.android.exoplayer:exoplayer-core:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer:2.11.8'
implementation 'com.google.android.exoplayer:exoplayer-core:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.18.4'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.4'
}
Here is my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.TestIPTV"
tools:targetApi="31">
<activity
android:name=".videoplayer"
android:usesCleartextTraffic="true"
android:screenOrientation="sensor"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is exoplayer xml design
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".videoplayer"
android:keepScreenOn="true">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
app:show_subtitle_button="false"
app:hide_on_touch="true"
app:use_controller="true"
app:controller_layout_id="@layout/custom_exo_view"
app:show_vr_button="false"
app:resize_mode="fill"
app:show_shuffle_button="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Here is my Java code that inserts a M3U link that is trying to play Exoplayer.
package com.example.testiptv;
import static android.content.ContentValues.TAG;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
public class videoplayer extends AppCompatActivity {
protected PlayerView playerView;
SimpleExoPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
playerView = findViewById(R.id.player_view);
playvideo();
}
private void playvideo() {
//String videouri = getIntent().getStringExtra("keyName");
String videouri = "https://stmv.video.expressolider.com.br/ghostv/ghostv/playlist.m3u8";
try {
Uri uri = Uri.parse(videouri);
player = new SimpleExoPlayer(videoplayer.this);
playerView.setPlayer(player);
// Create an HTTPS data source factory
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
"exoplayer-codelab",
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true /* allowCrossProtocolRedirects */,
DefaultHttpDataSourceFactory.DEFAULT_SSL_SOCKET_FACTORY);
// Create a media source using the data source factory
MediaSource mediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
Log.d(TAG, "Media source created: " + (mediaSource != null));
// Set the media source for the player
player.setMediaSource(mediaSource);
// Prepare the player
player.prepare();
// Start playing the video
player.setPlayWhenReady(true);
} catch (Exception e) {
Log.e(TAG, "Error initializing player", e);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
player.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
player.release();
}
}
答案1
得分: 2
如在ExoPlayer发布说明中提到,DefaultHttpDataSourceFactory
在版本2.16.0中已移除,您应该改用DefaultHttpDataSource.Factory
。
英文:
As mentioned in Exoplayer release notes, DefaultHttpDataSourceFactory
is removed in version 2.16.0 and you should use DefaultHttpDataSource.Factory
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论