新手求教activity跳转问题

karlnonfu 发布于 2012/05/27 18:14
阅读 420
收藏 0
新手刚入门android,想写个音乐播放器,碰到了一些问题,求教各位大神:
Activity A :使用ListView输出音乐列表
package local.hk.hkSongPlayer;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.*;
import android.os.Bundle;
import android.provider.MediaStore;

public class HkSongPlayerActivity extends Activity {
    /** Called when the activity is first created. */
private ListView songList = null;
private Cursor songCursor = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.songList = (ListView)HkSongPlayerActivity.this.findViewById(R.id.songList);
        this.songCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        String[] columns = {MediaStore.Audio.AudioColumns.DISPLAY_NAME};
        SimpleCursorAdapter songCursorAdapter = new SimpleCursorAdapter(this, R.layout.songlistpart, songCursor,columns, new int[]{R.id.songName});
        this.songList.setAdapter(songCursorAdapter);
//定义OnItemClickListener
        this.songList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       
         public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
        int position = pos;
        String chooseSong = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DISPLAY_NAME));
//选中ListView中的Item后使用Intent进行跳转至PlayingActivity
        Intent intent = new Intent(HkSongPlayerActivity.this,playingActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("NAME", chooseSong);
        startActivity(intent);
}   
}); 
    }
}
Activity B:选中要播放的歌曲后,跳转的Activity文件;定义了一个MediaPlayer根据Intent传来的数据播放音乐
package local.hk.hkSongPlayer;

import java.io.IOException;

import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.*;

public class playingActivity extends Activity {
/** Called when the activity is first created. */
private TextView showPlayingSongName = null;
private MediaPlayer songMedia;
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.playinglayout);
       Bundle bundle = this.getIntent().getExtras();
       String playingSongName = bundle.getString("NAME");
       int position = bundle.getInt("position",-1);
       this.showPlayingSongName = (TextView)playingActivity.this.findViewById(R.id.palyingSongName);
       //this.showPlayingSongName.setText(playingSongName);
       Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null, null, null, null);
       cursor.moveToPosition(position);
       String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
       MediaPlayer songMedia = new MediaPlayer();
       try {
        if(songMedia==null)
        {songMedia = new MediaPlayer();}
        else
        {songMedia.reset();}
       
songMedia.setDataSource(path);
songMedia.prepare();
songMedia.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}           
   }    
}

从音乐列表界面选择了一首歌曲后成功跳转至了播放界面,即从Activity A成功跳转至了Activity B,且播放选中的歌曲,但接下来出现了如下问题:
1、完成上述操作后,使用Back键从播放界面退回至音乐列表界面,音乐没有停止。
2、紧接着在音乐列表界面选择另外一首歌曲,重新回到播放界面,该首歌曲开始播放,但前面未播放完毕的歌曲没有停止,即出现了两首歌曲同时播放的现象。

将两个activity的lauchMode配置成singleTask,也没有解决该问题。

PS. 暂不考虑使用多线程、service等方案解决,主要想明白上述情况是为什么发生的


加载中
0
k
karlnonfu
在线等啊,求各位大神了    
返回顶部
顶部