1

I'm trying to make a music player in android studio but when i try to test my app on my phone or AVD it just flashes on screen (opens and closes in a second). i'm quite new to android studio and i would appriciate if you can give me some tips.

this is my media cursor adapter class:

package com.example.musicplayer;


import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import java.math.BigDecimal;



public class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
    super(context, layout, c, new String[]{MediaStore.MediaColumns.DISPLAY_NAME,
                    MediaStore.MediaColumns.TITLE,MediaStore.Audio.AudioColumns.DURATION},
            new int[]{R.id.displayname, R.id.title, R.id.duration});
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView title = (TextView) view.findViewById(R.id.title);
    TextView name = (TextView) view.findViewById(R.id.displayname);
    TextView duration = (TextView) view.findViewById(R.id.duration);

    name.setText(cursor.getString(cursor.getColumnIndex(
            MediaStore.MediaColumns.DISPLAY_NAME)));
    title.setText(cursor.getString(cursor.getColumnIndex(
            MediaStore.MediaColumns.TITLE)));
    long durationInMS = Long.parseLong(cursor.getString(
            cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));

    double durationInMin = ((double) durationInMS / 1000.0) / 60.0;
    durationInMin = new BigDecimal(Double.toString(durationInMin)).
            setScale(2, BigDecimal.ROUND_UP).doubleValue();
    duration.setText("" + durationInMin);
    view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.item, parent, false);
    bindView(v, context, cursor);
    return v;
    }
}

and this is my main activity class:

package com.example.musicplayer;

import android.app.ListActivity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends ListActivity {
private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;

private TextView selectedfile = null;
private SeekBar seekBar = null;
private MediaPlayer player = null;
private ImageButton prev = null;
private ImageButton play = null;
private ImageButton next = null;
private MediaCursorAdapter adapter = null;

private boolean isStarted = true;
private String currentFile = "";
private boolean isMovingSeekBar = false;
private final Handler handler = new Handler();

private final Runnable updatePositinRunnable = new Runnable() {
    @Override
    public void run() {
        updatePosition();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    selectedfile = (TextView) findViewById(R.id.selecteditem);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    prev = (ImageButton) findViewById(R.id.previous);
    play = (ImageButton) findViewById(R.id.play);
    next = (ImageButton) findViewById(R.id.next);

    player = new MediaPlayer();
    player.setOnCompletionListener(onCompletion);
    player.setOnErrorListener(onError);
    seekBar.setOnSeekBarChangeListener(seekBarChanged);

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.
            EXTERNAL_CONTENT_URI, null, null, null, null);

    if (null != cursor) {
        cursor.moveToFirst();
        adapter = new MediaCursorAdapter(this, R.layout.item, cursor);
        setListAdapter(adapter);
        prev.setOnClickListener(OnButtonClick);
        play.setOnClickListener(OnButtonClick);
        next.setOnClickListener(OnButtonClick);
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    currentFile = (String) v.getTag();
    startPlay(currentFile);
}

private void startPlay(String file) {
    Log.i("Selected: ", file);
    selectedfile.setText(file);
    seekBar.setProgress(0);
    player.stop();
    player.reset();

    try {
        player.setDataSource(file);
        player.prepare();
        player.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    seekBar.setMax(player.getDuration());
    play.setImageResource(android.R.drawable.ic_media_pause);
    updatePosition();
    isStarted = true;
}

private void stopPlay() {
    player.stop();
    player.reset();
    play.setImageResource(android.R.drawable.ic_media_play);
    handler.removeCallbacks(updatePositinRunnable);
    seekBar.setProgress(0);
    isStarted = false;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(updatePositinRunnable);
    player.stop();
    player.reset();
    player.release();
    player = null;
}

private void updatePosition() {
    handler.removeCallbacks(updatePositinRunnable);
    seekBar.setProgress(player.getCurrentPosition());
    handler.postDelayed(updatePositinRunnable, UPDATE_FREQUENCY);
}

private View.OnClickListener OnButtonClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play: {
                if (player.isPlaying()) {
                    handler.removeCallbacks(updatePositinRunnable);
                    player.pause();
                    play.setImageResource(android.R.drawable.ic_media_play);
                } else {
                    if (isStarted) {
                        player.start();
                        play.setImageResource(android.R.drawable.ic_media_pause);
                        updatePosition();
                    } else {
                        startPlay(currentFile);
                    }
                }
                break;
            }

            case R.id.next: {
                int seekto = player.getCurrentPosition() + STEP_VALUE;
                if (seekto > player.getDuration())
                    seekto = player.getDuration();
                player.pause();
                player.seekTo(seekto);
                player.start();
                break;
            }

            case R.id.previous: {
                int seekto = player.getCurrentPosition() - STEP_VALUE;
                if (seekto < 0)
                    seekto = 0;
                player.pause();
                player.seekTo(seekto);
                player.start();
                break;
            }
        }
    }
};
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        stopPlay();
    }

    ;
};
private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }
};

private SeekBar.OnSeekBarChangeListener seekBarChanged =
        new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (isMovingSeekBar) {
                    player.seekTo(progress);
                    Log.i("OnSeekBarChangeListener", "OnProgressChanged");
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isMovingSeekBar = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isMovingSeekBar = false;
            }
        };

and this is the event log: Event log

here is the logcat:

2019-02-27 18:01:08.704 7280-7280/com.example.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.musicplayer, PID: 7280 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7280, uid=10085 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7280, uid=10085 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.createException(Parcel.java:1942) at android.os.Parcel.readException(Parcel.java:1910) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:418) at android.content.ContentResolver.query(ContentResolver.java:802) at android.content.ContentResolver.query(ContentResolver.java:752) at android.content.ContentResolver.query(ContentResolver.java:710) at com.example.musicplayer.MainActivity.onCreate(MainActivity.java:58) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  2019-02-27 18:01:08.787 7280-7280/com.example.musicplayer I/Process: Sending signal. PID: 7280 SIG: 9

rkmjere
  • 31
  • 4
  • 1
    Can you please post the logcat error message. – Kaushal Panchal Feb 27 '19 at 16:42
  • Welcome to Stack Overflow! Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561). – Zoe Feb 27 '19 at 16:44
  • can you please copy and post the logcat ? – Zain Feb 27 '19 at 16:56

0 Answers0