简体   繁体   English

不停止广播接收器中的当前歌曲

[英]Does not stop the current song in broadcastrecevier

I trying a music app. 我正在尝试一个音乐应用。 I use MediaPlayer object and My musics play in Broadcastrecevier. 我使用MediaPlayer对象,并且我的音乐在Broadcastrecevier中播放。 When I touch any songs in list, Music is playing. 当我触摸列表中的任何歌曲时,音乐正在播放。 But my stop, reset or pause function not working. 但是我的停止,重置或暂停功能不起作用。

MediaPlayerBroadcast.java MediaPlayerBroadcast.java

public class MediaPlayerBroadcast extends BroadcastReceiver {

    private MediaPlayer mPlayer = new MediaPlayer();

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle get_extras      = intent.getExtras();
        if(get_extras.getString("music_name").equals("stop")) {
                mPlayer.stop();
        }
        else {
            String url      = "http://www.tupbebekailesi.com/musics/" + get_extras.getString("music_name");
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mPlayer.setDataSource(url);
            } catch (IllegalArgumentException e) {
                Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (SecurityException e) {
                Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IllegalStateException e) {
                Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e) {
                Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            }
            mPlayer.start();
        }
    }

}

and MusicActivity.java 和MusicActivity.java

public class MusicActivity extends Fragment implements AdapterView.OnItemClickListener {

    private ListView music_list;
    private MusicAdapter musicAdapter;
    private List<String> music_list_name;
    private Button stop_music;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView       = inflater.inflate(R.layout.music_activity,container,false);
        music_list          = (ListView) rootView.findViewById(R.id.music_list);
        stop_music          = (Button) rootView.findViewById(R.id.stop);

        music_list_name     = new ArrayList<String>();
        music_list_name.add("deadmau5-Alone-With-You.mp3");
        music_list_name.add("deadmau5-Bad-Selection.mp3");
        music_list_name.add("Deadmau5-Clockwork.mp3");
        music_list_name.add("Deadmau5-Faxing-Berlin.mp3");
        music_list_name.add("Deadmau5-Ghosts-n-Stuff.mp3");

        music_list.setOnItemClickListener(this);

        stop_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent start_music  = new Intent("com.olkun.medyasef.merhaba");
                //Here I am stop music.
                start_music.putExtra("music_name","stop");
                getActivity().sendBroadcast(start_music);

            }
        });

        musicAdapter        = new MusicAdapter(getActivity(),music_list_name);
        music_list.setAdapter(musicAdapter);
        return rootView;

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String name = music_list_name.get(position);
        Log.d("BASLA",name);

        //here I am start any music.
        Intent start_music  = new Intent("com.olkun.medyasef.merhaba");
        start_music.putExtra("music_name",name);
        getActivity().sendBroadcast(start_music);
    }


    private class MusicAdapter extends BaseAdapter {

        private Context mContext;
        private List<String> music_array_list;



        private MusicAdapter(Context mContext, List<String> music_array_list) {
            this.mContext = mContext;
            this.music_array_list = music_array_list;
        }

        @Override
        public int getCount() {
            return music_array_list.size();
        }

        @Override
        public String getItem(int position) {
            return music_array_list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertview, ViewGroup parent) {
            ViewHolder holder           = null;
            String music_list           = getItem(position);

            if(convertview == null){
                convertview         = LayoutInflater.from(mContext).inflate(R.layout.music_activity_area,parent,false);
                holder              = new ViewHolder();
                holder.music_name   = (TextView) convertview.findViewById(R.id.music_name);
                convertview.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertview.getTag();
            }
            holder.music_name.setText(music_list);

            return convertview;
        }
    }

    /*private view holder class*/
    private class ViewHolder {
        TextView music_name;
    }

}

That is because everytime you press these buttons (play, stop) a new instance of BrodcastReceiver is created (for every new incomming broadcast). 这是因为每次您按下这些按钮(播放,停止)时, BrodcastReceiver为新的BrodcastReceiver广播创建BrodcastReceiver的新实例。 So once you've started playing in the first instance of the MediaPlayer you can't stop it in another instance of it. 因此,一旦开始在MediaPlayer的第一个实例中MediaPlayer ,就无法在它的另一个实例中停止MediaPlayer Besides instationating MediaPlayer (and using it) inside BroadcastReceiver is a bad idea. 除了在BroadcastReceiver安装MediaPlayer (并使用它)之外,这是一个坏主意。

When you play music on touch you send put extra value to broadcast. 当您播放音乐时,您可以发送更多的广播价值。 when you trying to stop or pause you send another put extra ,but with different values and check in broadcast receiver if stop value exist then stop your music player. 当您尝试停止或暂停时,您会另外发送一个put,但具有不同的值,并检查广播接收器是否存在停止值,然后停止音乐播放器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM