在这里我附上了我的代码
`公共类下载音乐活动扩展活动{
/**
* Estimates bandwidth by listening to data transfers
*/
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
/**
* The User-Agent string that should be used.
*/
private static final String USER_AGENT = Constants.EXO_PLAYER;
/**
* Data source factory
*/
private static final DefaultHttpDataSourceFactory DATA_SOURCE_FACTORY =
new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER);
/**
* Notification id
*/
int id = 1;
/**
* Calculate download percentage
*/
int calculatePercentage = 0;
/**
* Simple cache file
*/
private SimpleCache cache;
/**
* Temporary folder
*/
private File tempFolder;
/**
* Notification Builder
*/
private NotificationCompat.Builder mBuilder;
/**
* Notification Manager
*/
private NotificationManager mNotifyManager;
/**
* Starts activity
*
* @param context Context of the starting activity
*/
public static void startActivity(Context context) {
Intent intent = new Intent(context, DownloadMusicActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
//tempFolder=tempFolder.createTempFile(getString(R.string.app_folder_name),".m3u8");
tempFolder = Util.createTempDirectory(this, getString(R.string.app_folder_name));
} catch (IOException e) {
LogMessage.INSTANCE.e(e);
}
cache = new SimpleCache(tempFolder, new NoOpCacheEvictor());
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getString(R.string.app_name))
.setColor(getResources().getColor(R.color.signUpButtonGradientStart))
.setStyle(bigTextStyle)
.setContentText(getString(R.string.download_progress_notification))
.setOngoing(true)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.logo);
} else {
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
new DownloadChunksTask().execute();
}
private HlsDownloader getHlsDownloader(String mediaPlaylistUri) {
return new HlsDownloader(
Uri.parse(mediaPlaylistUri), new DownloaderConstructorHelper(cache,
DATA_SOURCE_FACTORY));
}
/**
* Download the chunk files using AsyncTask
*/
public class DownloadChunksTask extends AsyncTask<Integer, Integer, Integer> {
//Condition to check the user not subscribed status
final String audioUrl = "https://contus-skempi-dev.s3.amazonaws.com/encoded/Dancing" +
".mp3-playlists.m3u8";
@Override
protected Integer doInBackground(Integer... voids) {
try {
getHlsDownloader(audioUrl).download(new Downloader.ProgressListener() {
@Override
public void onDownloadProgress(Downloader downloader, float
downloadPercentage, long
downloadedBytes) {
calculatePercentage = Math.round(downloadPercentage);
// Sets the progress indicator to a max calculatePercentage, the current
// completion
// percentage and
// "determinate" state
mBuilder.setProgress(100, Math.round(downloadPercentage), false);
// Displays the progress bar for the first time.
mNotifyManager.notify(id, mBuilder.build());
}
});
} catch (IOException e) {
LogMessage.INSTANCE.e(e);
} catch (InterruptedException e) {
LogMessage.INSTANCE.e(e);
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected void onPostExecute(Integer aVoid) {
super.onPostExecute(aVoid);
if (calculatePercentage == 100) {
mBuilder.setContentText(getString(R.string.download_completed));
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
Toast.makeText(DownloadMusicActivity.this, getString(R.string.download_completed)
, Toast
.LENGTH_SHORT).show();
} else {
mBuilder.setContentText(getString(R.string.download_progress_failed));
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
Toast.makeText(DownloadMusicActivity.this, getString(R.string.check_internet),
Toast
.LENGTH_SHORT).show();
mBuilder.setAutoCancel(true);
}
}
@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
}
`我使用 HlsDownloader 下载播放列表(.m3u8)文件,并将该文件存储在缓存中,缓存存储每个带有 .tmp 扩展名的文件,我将该缓存文件路径 uri 传递给 exoplayer mediaSource 但它不播放.. .