-1

In my app I am trying to play a media player from server along with a seek bar. When I tried to play the song from server, my app was working fine but the seek bar was not getting moved ! Also, The seekbar is not working....

It's not displaying MediaPlayer progress

also, It is playing multiple songs at the same time

solution needed for 2 bugs

Here is a screenshot of that app

    import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity2 extends AppCompatActivity {

    private ArrayList<SongInfo> _songs = new ArrayList<SongInfo>();
    RecyclerView recyclerView;
    SeekBar seekBar;
    SongAdapter songAdapter;
    MediaPlayer mediaPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        seekBar = (SeekBar) findViewById(R.id.seekBar);


        SongInfo s = new SongInfo("Cheap Thrills", "sia", "http://176.126.236.250/33Mmt/music/hindi/movies/new/oh_my_god/Go-Go-Govinda_(webmusic.in).mp3");
        _songs.add(s);

        s = new SongInfo("Cheap Thrills", "sia", "http://176.126.236.250/33Mmt/music/hindi/movies/new/oh_my_god/Go-Go-Govinda_(webmusic.in).mp3");
        _songs.add(s);

        songAdapter = new SongAdapter(this, _songs);
        recyclerView.setAdapter(songAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(songAdapter);


        songAdapter.setOnItemClickListener(new SongAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final Button b, View view, SongInfo obj, int position) {
                try {
                    if (b.getText().toString().equals("stop")) {
                        b.setText("Play");
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }else {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setDataSource(obj.getSongUrl());
                        mediaPlayer.prepareAsync();
                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mp.start();
                                b.setText("stop");
                            }
                        });
                    }

                } catch (IOException e) {
                }
            }
        });

    }

}

this is my song adapter code -:

package com.a03.dip.kaliprasadbengalisongs;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongHolder> {

    ArrayList<SongInfo> _songs;
    Context context;

    OnItemClickListener mOnItemClickListener;

    SongAdapter(Context context, ArrayList<SongInfo> songs) {
        this.context = context;
        this._songs = songs;
    }

    public interface OnItemClickListener {
        void onItemClick(Button b ,View view, SongInfo obj, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mOnItemClickListener = mItemClickListener;
    }

    @Override
    public SongHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View myView = LayoutInflater.from(context).inflate(R.layout.row_song,viewGroup,false);
        return new SongHolder(myView);
    }

    @Override
    public void onBindViewHolder(final SongHolder songHolder, final int i) {
        final SongInfo c = _songs.get(i);
        songHolder.songName.setText(_songs.get(i).songName());
        songHolder.artistName.setText(_songs.get(i).artistName());
        songHolder.btnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(songHolder.btnAction,v, c, i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        return _songs.size();
    }

    public class SongHolder extends RecyclerView.ViewHolder {
        TextView songName,artistName;
        Button btnAction;

        public SongHolder(View itemView) {
            super(itemView);
            songName = (TextView) itemView.findViewById(R.id.tvSongName);
            artistName = (TextView) itemView.findViewById(R.id.tvArtistName);
            btnAction = (Button) itemView.findViewById(R.id.btnPlay);

        }
    }
}

and here is songInfo class -----

package com.a03.dip.kaliprasadbengalisongs;

import android.media.MediaPlayer;

public class SongInfo {
        public String songName ,artistName,songUrl;

    public SongInfo() {
    }

    public SongInfo(String songName, String artistName, String songUrl) {
        this.songName = songName;
        this.artistName = artistName;
        this.songUrl = songUrl;
    }

    public String songName() {
        return songName;
    }

    public String artistName() {
        return artistName;
    }

    public String getSongUrl() {
        return songUrl;
    }

}
  • What protocol are you using for streaming? HTTP? And if so, does your server support range requests? You should know that in any case, regular MP3 streams have no built-in timestamp. Seeking into an MP3 file requires a bit of guesswork based on the average bitrate and the byte size of the file. (That is of course until the whole file is downloaded and decoded.) – Brad Nov 25 '17 at 23:18
  • Hi ! Yes ! I am using HTTP ..... My song is playing smoothly... but the problem is I could not synchronize the seekbar with my song.....everything is working fine except seekbar.... i need to get song duration and match with seek bar... – user8622498 Nov 26 '17 at 06:43
  • You forgot to answer a key question... does your server support range requests? Even if it does, you're not going to get an exact duration with a plain MP3 file. – Brad Nov 26 '17 at 06:43
  • No! It doesn't support range request.... – user8622498 Nov 26 '17 at 06:47
  • Give me a code sample which i can use to get duration... – user8622498 Nov 26 '17 at 06:49
  • Pleaseeagerly requesting to you – user8622498 Nov 26 '17 at 06:49
  • I already told you that you can't get the duration, you can only estimate it. Fix your server so that it supports range requests if you want this to work at all. – Brad Nov 26 '17 at 07:04
  • Is there any code that can I use if my server support range request...? Wanna to try ...I am not sure if my server supports range request or not... – user8622498 Nov 26 '17 at 07:14
  • I am facing issue with SeekBar can you please help me... here is my question https://stackoverflow.com/questions/54692981/android-music-seekbar-is-not-working-idle – VikaS GuttE Feb 23 '19 at 17:23

1 Answers1

-1

you have to use seekbar listener on ur activity.

seekBar.setOnSeekBarChangeListener(new >SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int >progress, boolean fromUser) { if (fromUser) { mPlayer.seekTo(progress); } }

Sartaj Roshan
  • 204
  • 2
  • 6
  • Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Nov 24 '17 at 12:04
  • can you please explain? where should I write it? how can I set progress? explain in details – user8622498 Nov 24 '17 at 16:03