我正在尝试制作一个音频流应用程序,其url存储在数组列表中。我的asynctask正在缓冲但不播放url中的音频。我到处找答案,但都没找到。我看过的网站:https://www.codeproject.com/questions/1204993/how-do-i-stream-audio-on-android-using-url
package com.koddev.notificationmusicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.MediaStore;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.koddev.notificationmusicplayer.Services.OnClearFromRecentService;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Playable {
ImageButton play;
TextView title;
private MediaPlayer mPlayer;
NotificationManager notificationManager;
private boolean initialStage = true;
private ProgressDialog progressDialog;
private boolean playPause;
List<Track> tracks;
int position = 0;
String music;
boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (ImageButton) findViewById(R.id.play);
title = findViewById(R.id.title);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
progressDialog = new ProgressDialog(this);
popluateTracks();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createChannel();
registerReceiver(broadcastReceiver, new IntentFilter("TRACKS_TRACKS"));
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
}
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying){
onTrackPause();
} else {
onTrackPlay();
}
}
});
}
private void createChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(CreateNotification.CHANNEL_ID,
"KOD Dev", NotificationManager.IMPORTANCE_LOW);
notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null){
notificationManager.createNotificationChannel(channel);
}
}
}
//populate list with tracks
private void popluateTracks(){
tracks = new ArrayList<>();
tracks.add(new Track("Dubstep", "example", R.drawable.t1, "www.example1.com/example.mp3"));
tracks.add(new Track("Sun", "example", R.drawable.t2, "www.example1.com/example.mp3"));
tracks.add(new Track("Day", "example", R.drawable.t3, "www.example1.com/example.mp3"));
tracks.add(new Track("Energy", "example", R.drawable.t4, "www.example1.com/example.mp3"));
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getExtras().getString("actionname");
switch (action){
case CreateNotification.ACTION_PREVIUOS:
onTrackPrevious();
break;
case CreateNotification.ACTION_PLAY:
if (isPlaying){
onTrackPause();
} else {
onTrackPlay();
}
break;
case CreateNotification.ACTION_NEXT:
onTrackNext();
break;
}
}
};
class Player extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Boolean prepared = false;
try {
mPlayer.setDataSource(strings[0]);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
initialStage = true;
playPause = false;
mPlayer.stop();
mPlayer.reset();
}
});
mPlayer.prepare();
prepared = true;
}catch (Exception e) {
Log.e("MyAudioStreamingApp", e.getMessage());
prepared = false;
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (progressDialog.isShowing()) {
progressDialog.cancel();
}
mPlayer.start();
initialStage = false;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Buffering");
progressDialog.show();
}
}
@Override
public void onTrackPrevious() {
position--;
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.ic_pause_black_24dp, position, tracks.size()-1);
title.setText(tracks.get(position).getTitle());
onTrackPause();
}
@Override
public void onTrackPlay() {
CreateNotification.createNotification(MainActivity.this, tracks.get(position), R.drawable.ic_pause_black_24dp, position, tracks.size()-1);
play.setImageResource(R.drawable.ic_pause_black_24dp);
title.setText(tracks.get(position).getTitle());
isPlaying = false;
music = (tracks.get(position).getMusic());
Toast toast = Toast.makeText(MainActivity.this, music , Toast.LENGTH_LONG);
toast.show();
if (!playPause) {
if (initialStage) {
new Player().execute(music);
} else {
if(!mPlayer.isPlaying()) {
mPlayer.start();
}
playPause = true;
}
} else {
if (mPlayer.isPlaying()) {
mPlayer.pause();
}
playPause = false;
}
}
@Override
protected void onPause() {
super.onPause();
if (mPlayer !=null) {
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
}
@Override
public void onTrackPause() {
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.ic_play_arrow_black_24dp, position, tracks.size()-1);
play.setImageResource(R.drawable.ic_play_arrow_black_24dp);
title.setText(tracks.get(position).getTitle());
isPlaying = false;
mPlayer.pause();
}
@Override
public void onTrackNext() {
position++;
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.ic_pause_black_24dp, position, tracks.size()-1);
title.setText(tracks.get(position).getTitle());
onTrackPause();
mPlayer.reset();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationManager.cancelAll();
}
unregisterReceiver(broadcastReceiver);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!