0

**Down here I am trying to build a music app in android studio,my app is working fine displaying all my SD card songs and play too ,all button works but when I click on next button it works normally but don't know why app sometimes gets stopped working and shows runtime exception ->>please open the image of run log of the application.

package com.example.ramandeepsingh.tunes;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.jar.Manifest;

public class PlayerActivity extends AppCompatActivity{
    static MediaPlayer mp;//assigning memory loc once or else multiple songs will play at once
    int position;
    SeekBar sb;
    ArrayList<File> mySongs;
    Thread updateSeekBar;
    Button pause,forward,reverse,next,previous;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        pause = (Button)findViewById(R.id.pause);
        forward = (Button)findViewById(R.id.forward);
        previous = (Button)findViewById(R.id.previous);
        next = (Button)findViewById(R.id.next);
        reverse = (Button)findViewById(R.id.reverse);
        sb=(SeekBar)findViewById(R.id.seekBar);

        updateSeekBar=new Thread(){
            @Override
            public void run(){
                int totalDuration = mp.getDuration();
                int currentPosition = 0;
              //  sb.setMax(totalDuration);
                while(currentPosition < totalDuration){
                    try{
                        sleep(500);
                        currentPosition=mp.getCurrentPosition();

                    }
                    catch (InterruptedException e){
                        e.printStackTrace();
                       System.out.println(e);
                    }
                    sb.setProgress(currentPosition);
                }
            }

        };
        if(mp != null){
            mp.stop();
            mp.release();
        }
        Intent i = getIntent();
        Bundle b = i.getExtras();
        mySongs = (ArrayList) b.getParcelableArrayList("songs");
        position = b.getInt("pos",0);
        Uri u = Uri.parse(mySongs.get(position).toString());
        mp = MediaPlayer.create(getApplicationContext(),u);

        mp.start();
        sb.setMax(mp.getDuration());
        updateSeekBar.start();
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mp.seekTo(seekBar.getProgress());
            }

        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sb.setMax(mp.getDuration());

                if(mp.isPlaying()){
                    pause.setText(">");
                    mp.pause();
                }
                else {
                    pause.setText("||");
                    mp.start();
                }

            }
        });
        forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sb.setMax(mp.getDuration());

                mp.seekTo(mp.getCurrentPosition()+5000);
            }
        });
        reverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sb.setMax(mp.getDuration());

                mp.seekTo(mp.getCurrentPosition()-5000);
            }
        });

        next.setOnClickListener(new
           View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
                mp.reset();

                position=((position+1)%mySongs.size());
                Uri u = Uri.parse(mySongs.get( position).toString());

                mp = MediaPlayer.create(getApplicationContext(),u);
                mp.start();
                sb.setMax(mp.getDuration());
                updateSeekBar.start();

            }
        });
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
                mp.release();
                position=((position-1)<0)?(mySongs.size()-1):(position-1);
                Uri u = Uri.parse(mySongs.get( position).toString());//%mysongs so that it do not go to invalid position
                mp = MediaPlayer.create(getApplicationContext(),u);
                mp.start();
            }
        });
    }}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    What difficulty are you having with **File not found**? – Ken White Feb 27 '18 at 17:05
  • Did you try searching first? See : https://stackoverflow.com/questions/3288619/what-to-do-when-java-io-filenotfoundexception-no-content-provider – fukanchik Feb 27 '18 at 17:07
  • am not having issue with file not found ,the only issue is the exception in thread i used for seekbar please click on the image embedded in the summary of the problem. – Raman Multani Feb 27 '18 at 18:07

0 Answers0