- Forum-Beiträge: 2
22.03.2021, 19:18:51 via Website
22.03.2021 19:18:51 via Website
Guten Abend,
wie kann ich diesen Fehler beheben?
Fehlermeldung:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.audioplayer, PID: 9599
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getDuration()' on a null object reference
at com.example.audioplayer.MusicService.getDuration(MusicService.java:85)
at com.example.audioplayer.PlayerActivity.onServiceConnected(PlayerActivity.java:551)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2084)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2116)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
MusicService:
package com.example.audioplayer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import static com.example.audioplayer.PlayerActivity.listSongs;
public class MusicService extends Service implements MediaPlayer.OnCompletionListener {
IBinder mBinder = new MyBinder();
MediaPlayer mediaPlayer;
ArrayList musicFiles = new ArrayList<>();
Uri uri;
int position = -1;
ActionPlaying actionPlaying;
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("Bind", "Method");
return mBinder;
}
public class MyBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int myPosition = intent.getIntExtra("servicePosition", -1);
if (myPosition != -1)
{
playMedia(myPosition);
}
return START_STICKY;
}
private void playMedia(int StartPosition) {
musicFiles = listSongs;
position = StartPosition;
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
if (musicFiles != null) {
createMediaPlayer(position);
mediaPlayer.start();
}
}
else
{
createMediaPlayer(position);
mediaPlayer.start();
}
}
void start() {
mediaPlayer.start();
}
boolean isPlaying() {
return mediaPlayer.isPlaying();
}
void stop() {
mediaPlayer.stop();
}
void release() {
mediaPlayer.release();
}
int getDuration() {
return mediaPlayer.getDuration();
}
void seekTo(int position) {
mediaPlayer.seekTo(position);
}
int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
void createMediaPlayer(int position) {
uri = Uri.parse(musicFiles.get(position).getPath());
mediaPlayer = MediaPlayer.create(getBaseContext(), uri);
}
void pause() {
mediaPlayer.pause();
}
void OnCompleted() {
mediaPlayer.setOnCompletionListener(this);
}
@Override
public void onCompletion(MediaPlayer mp) {
if (actionPlaying != null)
{
actionPlaying.nextBtnClicked();
}
createMediaPlayer(position);
mediaPlayer.start();
OnCompleted();
}
}
PlayerActivity:
com.example.audioplayer;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.palette.graphics.Palette;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.media.Image;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.media.session.MediaSessionCompat;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Random;
import static com.example.audioplayer.AlbumDetailsAdapter.albumFiles;
import static com.example.audioplayer.ApplicationClass.ACTION_PLAY;
import static com.example.audioplayer.ApplicationClass.ACTION_PREVIOUS;
import static com.example.audioplayer.ApplicationClass.CHANNEL_ID_2;
import static com.example.audioplayer.MainActivity.musicFiles;
import static com.example.audioplayer.MainActivity.repeatBoolean;
import static com.example.audioplayer.MainActivity.shuffleBoolean;
import static com.example.audioplayer.MusicAdapter.mFiles;
public class PlayerActivity extends AppCompatActivity
implements ActionPlaying, ServiceConnection {
private TextView song_name, artist_name, duration_played, duration_total;
private ImageView cover_art, nextBtn, prevBtn, backBtn, shuffleBtn, repeatBtn;
private FloatingActionButton playPauseBtn;
private SeekBar seekBar;
private int position = -1;
public static ArrayList<MusicFiles> listSongs = new ArrayList<>();
public static Uri uri;
//public static MediaPlayer mediaPlayer;
private Handler handler = new Handler();
private Thread playThread, prevThread, nextThread;
MusicService musicService;
MediaSessionCompat mediaSessionCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.black));
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
mediaSessionCompat = new MediaSessionCompat(getBaseContext(), "My Audio");
initViews();
getIntentMethod();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (musicService != null && fromUser)
{
musicService.seekTo(progress * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
duration_played.setText(formattedTime(mCurrentPosition));
}
handler.postDelayed(this, 1000);
}
});
shuffleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (shuffleBoolean)
{
shuffleBoolean = false;
shuffleBtn.setImageResource(R.drawable.ic_shuffle_off);
}
else
{
shuffleBoolean = true;
shuffleBtn.setImageResource(R.drawable.ic_shuffle_on);
}
}
});
repeatBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (repeatBoolean)
{
repeatBoolean = false;
repeatBtn.setImageResource(R.drawable.ic_repeat_off);
}
else
{
repeatBoolean = true;
repeatBtn.setImageResource(R.drawable.ic_repeat_on);
}
}
});
}
@Override
protected void onResume() {
Intent intent = new Intent(this, MusicService.class);
bindService(intent, this, BIND_AUTO_CREATE);
playThreadBtn();
nextThreadBtn();
prevThreadBtn();
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
unbindService(this);
}
private void prevThreadBtn() {
prevThread = new Thread()
{
public void run() {
super.run();
prevBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prevBtnClicked();
}
});
}
};
prevThread.start();
}
public void prevBtnClicked() {
if (musicService.isPlaying())
{
musicService.stop();
musicService.release();
if (shuffleBoolean && !repeatBoolean)
{
position = getRandom(listSongs.size() - 1);
}
else if (!shuffleBoolean && !repeatBoolean) {
position = ((position - 1) < 0 ? (listSongs.size() - 1) : (position - 1));
}
uri = Uri.parse(listSongs.get(position).getPath());
musicService.createMediaPlayer(position);
metaData(uri);
song_name.setText(listSongs.get(position).getTitle());
artist_name.setText(listSongs.get(position).getArtist());
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
musicService.OnCompleted();
showNotification(R.drawable.ic_pause);
playPauseBtn.setBackgroundResource(R.drawable.ic_pause);
musicService.start();
}
else
{
musicService.stop();
musicService.release();
if (shuffleBoolean && !repeatBoolean)
{
position = getRandom(listSongs.size() - 1);
}
else if (!shuffleBoolean && !repeatBoolean) {
position = ((position - 1) < 0 ? (listSongs.size() - 1) : (position - 1));
}
uri = Uri.parse(listSongs.get(position).getPath());
musicService.createMediaPlayer(position);
metaData(uri);
song_name.setText(listSongs.get(position).getTitle());
artist_name.setText(listSongs.get(position).getArtist());
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
musicService.OnCompleted();
showNotification(R.drawable.ic_play);
playPauseBtn.setBackgroundResource(R.drawable.ic_play);
}
}
private void nextThreadBtn() {
nextThread = new Thread()
{
public void run() {
super.run();
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextBtnClicked();
}
});
}
};
nextThread.start();
}
public void nextBtnClicked() {
if (musicService.isPlaying())
{
musicService.stop();
musicService.release();
if (shuffleBoolean && !repeatBoolean)
{
position = getRandom(listSongs.size() - 1);
}
else if (!shuffleBoolean && !repeatBoolean) {
position = ((position + 1) % listSongs.size());
}
//else position will be position...
position = ((position + 1) % listSongs.size());
uri = Uri.parse(listSongs.get(position).getPath());
musicService.createMediaPlayer(position);
metaData(uri);
song_name.setText(listSongs.get(position).getTitle());
artist_name.setText(listSongs.get(position).getArtist());
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
musicService.OnCompleted();
playPauseBtn.setBackgroundResource(R.drawable.ic_pause);
musicService.start();
}
else
{
musicService.stop();
musicService.release();
if (shuffleBoolean && !repeatBoolean)
{
position = getRandom(listSongs.size() - 1);
}
else if (!shuffleBoolean && !repeatBoolean) {
position = ((position + 1) % listSongs.size());
}
uri = Uri.parse(listSongs.get(position).getPath());
musicService.createMediaPlayer(position);
metaData(uri);
song_name.setText(listSongs.get(position).getTitle());
artist_name.setText(listSongs.get(position).getArtist());
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
musicService.OnCompleted();
playPauseBtn.setBackgroundResource(R.drawable.ic_play);
}
}
private int getRandom(int i) {
Random random = new Random();
return random.nextInt(i + 1);
}
private void playThreadBtn() {
playThread = new Thread()
{
public void run() {
super.run();
playPauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playPauseBtnClicked();
}
});
}
};
playThread.start();
}
public void playPauseBtnClicked() {
if (musicService.isPlaying())
{
playPauseBtn.setImageResource(R.drawable.ic_play);
showNotification(R.drawable.ic_play);
musicService.pause();
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
}
else
{
playPauseBtn.setImageResource(R.drawable.ic_pause);
showNotification(R.drawable.ic_pause);
musicService.start();
seekBar.setMax(musicService.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (musicService != null)
{
int mCurrentPosition = musicService.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
}
}
private String formattedTime(int mCurrentPosition) {
String totalout = "";
String totalNew = "";
String seconds = String.valueOf(mCurrentPosition % 60);
String minutes = String.valueOf(mCurrentPosition / 60);
totalout = minutes + ":" + seconds;
totalNew = minutes + ":" + "0" + seconds;
if (seconds.length() == 1)
{
return totalNew;
}
else
{
return totalout;
}
}
private void getIntentMethod() {
position = getIntent().getIntExtra("position", -1);
String sender = getIntent().getStringExtra("sender");
if (sender != null && sender.equals("albumDetails")) {
listSongs = albumFiles;
} else
{
listSongs = mFiles;
}
if (listSongs != null)
{
playPauseBtn.setImageResource(R.drawable.ic_pause);
uri = Uri.parse(listSongs.get(position).getPath());
}
showNotification(R.drawable.ic_pause);
Intent intent = new Intent(this, MusicService.class);
intent.putExtra("service", position);
startService(intent);
}
private void initViews() {
song_name = findViewById(R.id.song_name);
artist_name = findViewById(R.id.song_artist);
duration_played = findViewById(R.id.durationPlayed);
duration_total = findViewById(R.id.durationTotal);
cover_art = findViewById(R.id.cover_art);
nextBtn = findViewById(R.id.nextBtn);
prevBtn = findViewById(R.id.prevBtn);
backBtn = findViewById(R.id.back_btn);
shuffleBtn = findViewById(R.id.shuffleBtn);
repeatBtn = findViewById(R.id.repeatBtn);
playPauseBtn = findViewById(R.id.play_pause);
seekBar = findViewById(R.id.seekBar);
}
private void metaData( Uri uri)
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uri.toString());
int durationTotal = Integer.parseInt(listSongs.get(position).getDuration()) / 1000;
duration_total.setText(formattedTime(durationTotal));
byte[] art = retriever.getEmbeddedPicture();
Bitmap bitmap;
if (art != null)
{
bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
ImageAnimation(this, cover_art, bitmap);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(@Nullable Palette palette) {
Palette.Swatch swatch = palette.getDominantSwatch();
if (swatch != null)
{
ImageView gredient = findViewById(R.id.imageViewGredient);
RelativeLayout mContainer = findViewById(R.id.mContainer);
gredient.setBackgroundResource(R.drawable.gredient_bg);
mContainer.setBackgroundResource(R.drawable.main_bg);
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{swatch.getRgb(), 0x00000000});
gredient.setBackground(gradientDrawable);
GradientDrawable gradientDrawableBg = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{swatch.getRgb(), swatch.getRgb()});
mContainer.setBackground(gradientDrawableBg);
song_name.setTextColor(swatch.getTitleTextColor());
artist_name.setTextColor(swatch.getBodyTextColor());
}
else
{
ImageView gredient = findViewById(R.id.imageViewGredient);
RelativeLayout mContainer = findViewById(R.id.mContainer);
gredient.setBackgroundResource(R.drawable.gredient_bg);
mContainer.setBackgroundResource(R.drawable.main_bg);
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{0xff000000, 0x00000000});
gredient.setBackground(gradientDrawable);
GradientDrawable gradientDrawableBg = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{0xff000000, 0xff000000});
mContainer.setBackground(gradientDrawableBg);
song_name.setTextColor(Color.WHITE);
artist_name.setTextColor(Color.DKGRAY);
}
}
});
}
else
{
Glide.with(this)
.asBitmap()
.load(R.drawable.bewedoc)
.into(cover_art);
ImageView gredient = findViewById(R.id.imageViewGredient);
RelativeLayout mContainer = findViewById(R.id.mContainer);
gredient.setBackgroundResource(R.drawable.gredient_bg);
mContainer.setBackgroundResource(R.drawable.main_bg);
song_name.setTextColor(Color.WHITE);
artist_name.setTextColor(Color.DKGRAY);
}
}
public void ImageAnimation(final Context context, final ImageView imageview, final Bitmap bitmap)
{
Animation animOut = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
Animation animIn = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
animOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Glide.with(context).load(bitmap).into(imageview);
animIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageview.startAnimation(animIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageview.startAnimation(animOut);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MyBinder myBinder = (MusicService.MyBinder) service;
musicService = myBinder.getService();
Toast.makeText(this, "Connected" + musicService, Toast.LENGTH_SHORT).show();
seekBar.setMax(musicService.getDuration() / 1000);
//seekBar.setMax(musicService.getDuration() / 1000);
metaData(uri);
song_name.setText(listSongs.get(position).getTitle());
artist_name.setText(listSongs.get(position).getArtist());
musicService.OnCompleted();
}
@Override
public void onServiceDisconnected(ComponentName name) {
musicService = null;
}
void showNotification(int playPauseBtn)
{
Intent intent = new Intent(this, PlayerActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent prevIntent = new Intent(this, NotificationReceiver.class)
.setAction(ACTION_PREVIOUS);
PendingIntent prevPending = PendingIntent
.getBroadcast(this, 0, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(this, NotificationReceiver.class)
.setAction(ACTION_PLAY);
PendingIntent pausePending = PendingIntent
.getBroadcast(this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent(this, NotificationReceiver.class)
.setAction(ACTION_PREVIOUS);
PendingIntent nextPending = PendingIntent
.getBroadcast(this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
byte[] picture = null;
picture = getAlbumArt(musicFiles.get(position).getPath());
Bitmap thumb = null;
if (picture != null)
{
thumb = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
else
{
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.bewedoc);
}
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
.setSmallIcon(playPauseBtn)
.setLargeIcon(thumb)
.setContentTitle(musicFiles.get(position).getTitle())
.setContentText(musicFiles.get(position).getArtist())
.addAction(R.drawable.ic_skip_previous, "Previous", prevPending)
.addAction(R.drawable.ic_pause, "Pause", pausePending)
.addAction(R.drawable.ic_skip_next, "Next", nextPending)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSessionCompat.getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
private byte[] getAlbumArt(String uri)
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uri);
byte[] art = retriever.getEmbeddedPicture();
return art;
}
}
Schonmal Danke im Voraus!
MfG
Herbert
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.