android-当我在mediaplayer中单击列表视图时,正在播放多首歌曲

android-当我在mediaplayer中单击列表视图时,正在播放多首歌曲,第1张

概述您好,我正在创建音乐播放器应用.我有一个问题,当我单击列表视图时会播放歌曲.当我再次单击列表视图时,歌曲将一起播放.上一首歌曲不会停止.我关注了很多tut和博客以及stackoverflow的答案,但是没有任何效果.这是我的活动,其中包含歌曲列表视图:-publicclasssonglistextend

您好,我正在创建音乐播放器应用.

我有一个问题,当我单击列表视图时会播放歌曲.

当我再次单击列表视图时,歌曲将一起播放.

上一首歌曲不会停止.

我关注了很多tut和博客以及stackoverflow的答案,但是没有任何效果.

这是我的活动,其中包含歌曲列表视图:-

public class songList extends AppCompatActivity    implements NavigationVIEw.OnNavigationItemSelectedListener {    private ListVIEw lv_songList;    public Cursor cursor;    private MediaCursorAdapter mediaAdapter = null;    private String currentfile;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.songList);        lv_songList = (ListVIEw) findVIEwByID(R.ID.songList);        cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);        if (null != cursor)        {            cursor.movetoFirst();            mediaAdapter = new MediaCursorAdapter(this, R.layout.Listitem, cursor);            lv_songList.setAdapter(mediaAdapter);            lv_songList.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {                @OverrIDe                public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) {                    currentfile = (String) vIEw.getTag();                    Intent myIntent = new Intent(songList.this,                        Now_playing.class);                    myIntent.putExtra("song",""+currentfile);                    myIntent.putExtra("cursor-position",cursor.getposition());                    startActivity(myIntent);                }            });        }        Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);        setSupportActionbar(toolbar);        floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab);        fab.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                Intent myIntent = new Intent(songList.this,                    Now_playing.class);                myIntent.putExtra("song","");                startActivity(myIntent);            }        });        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        ActionbarDrawerToggle toggle = new ActionbarDrawerToggle(            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);        drawer.setDrawerListener(toggle);        toggle.syncState();        NavigationVIEw navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);        navigationVIEw.setNavigationItemSelectedListener(this);    }    @OverrIDe    public voID onBackpressed() {        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        if (drawer.isDrawerOpen(GravityCompat.START))         {            drawer.closeDrawer(GravityCompat.START);        }         else         {            super.onBackpressed();        }    }        @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.songList, menu);        return true;    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        int ID = item.getItemID();        if (ID == R.ID.action_settings) {            return true;        }        return super.onoptionsItemSelected(item);    }    @SuppressWarnings("StatementWithEmptyBody")    @OverrIDe    public boolean onNavigationItemSelected(MenuItem item) {        int ID = item.getItemID();        if (ID == R.ID.nav_camera)        {        }         else if (ID == R.ID.nav_gallery) {        } else if (ID == R.ID.nav_slIDeshow) {        } else if (ID == R.ID.nav_manage) {        } else if (ID == R.ID.nav_share) {        } else if (ID == R.ID.nav_send) {        }        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        drawer.closeDrawer(GravityCompat.START);        return true;    }    private 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)));            Duration d = new Duration();            String durationInMin = d.convertDuration(durationInMs);            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.Listitem, parent, false);            bindVIEw(v, context, cursor);            return v;        }    }    }

这是我的音乐演奏活动,可以一起播放歌曲:-

public class Now_playing extends AppCompatActivity implements VIEw.OnClickListener{    private MediaPlayer mp = new MediaPlayer();    private Cursor cursor;    private relativeLayout rl_album;    private TextVIEw Title_Now_playing,duration_Now_playing;    private String Title,Duration,currentSong;    private int cursorposition;    private Imagebutton playPrev,playNext,seekFwd,seekBwd;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.Now_playing);        setTitle("");        cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);        getSupportActionbar().setdisplayHomeAsUpEnabled(true);        androID.support.v7.app.Actionbar mActionbar = getSupportActionbar();        LayoutInflater mInflater = LayoutInflater.from(this);        VIEw mCustomVIEw = mInflater.inflate(R.layout.custom_layout_Now_playing_for_action_bar, null);        Title_Now_playing = (TextVIEw) mCustomVIEw.findVIEwByID(R.ID.Title_text);        duration_Now_playing = (TextVIEw) findVIEwByID(R.ID.duration);        playNext = (Imagebutton) findVIEwByID(R.ID.playNext);        rl_album = (relativeLayout) findVIEwByID(R.ID.rl_album);        Title_Now_playing.setText(Title);        mActionbar.setCustomVIEw(mCustomVIEw);        mActionbar.setdisplayShowCustomEnabled(true);        Intent in = getIntent();        currentSong = in.getStringExtra("song");        cursorposition = in.getIntExtra("cursor-position",1);            displayDetails();        playNext.setonClickListener(this);    }    voID displayDetails(){        mp.stop();        mp.reset();        if(!currentSong.equals(""))        {            cursor.move(cursorposition+1);            try {                mp.setDataSource(currentSong);                mp.prepare();                mp.start();            } catch (IllegalArgumentException e) {                e.printstacktrace();            } catch (IllegalStateException e) {                e.printstacktrace();            } catch (IOException e) {                e.printstacktrace();            }            Title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.disPLAY_name));            int duration = cursor.getColumnIndex                (MediaStore.Audio.Media.DURATION);            long SongDuration = cursor.getLong(duration);            com.example.davda.musicmain.Duration d = new Duration();            Duration = d.convertDuration(SongDuration);            duration_Now_playing.setText(Duration);            Title_Now_playing.setText(Title);            ContentResolver musicResolve = getContentResolver();            Uri smusicUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;            Cursor musicCursorTogetAlbum =musicResolve.query(smusicUri,null         //should use where clause(_ID==albumID)                ,null, null, null);            musicCursorTogetAlbum.move(cursorposition+1);        //i put only one song in my external storage to keep things simple;        try{            int x = musicCursorTogetAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);            String thisArt = musicCursorTogetAlbum.getString(x);            Bitmap bm = BitmapFactory.decodefile(thisArt);            Drawable dr = new BitmapDrawable(getResources(), bm);            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {                rl_album.setBackground(dr);            }        }catch (RuntimeException re)        {            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {                rl_album.setBackgroundcolor(color.BLACK);            }        }    }}public boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.Now_playing_menu, menu);    return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    switch (item.getItemID()) {        case androID.R.ID.home:            Intent myIntent = new Intent(Now_playing.this,                    songList.class);            startActivity(myIntent);            return true;        case R.ID.custom_equalizer:            Toast.makeText(Now_playing.this,"Custom Equalizer button pressed. Good broooooooooooooooooooooo...",Toast.LENGTH_LONG).show();            return true;        }        return super.onoptionsItemSelected(item);    }    @OverrIDe    public voID onClick(VIEw v) {        switch (v.getID()) {            case R.ID.playNext: {                cursor.movetoNext();                currentSong = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));                displayDetails();            }        }    }}

解决方法:

因为您每次在活动中都在创建MediaPlayer的新实例.

使用MediaPlayer的单个实例.

创建一个单例类,然后从那里使用MediaPlayer.

或者,您可以在ListVIEw Activity中将MediaPlayer设为public static.

public static MediaPlayer mp = new MediaPlayer();

并在Now_playing活动中使用此MediaPlayer

总结

以上是内存溢出为你收集整理的android-当我在mediaplayer中单击列表视图时,正在播放多首歌曲全部内容,希望文章能够帮你解决android-当我在mediaplayer中单击列表视图时,正在播放多首歌曲所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1120920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存