JamesMusic浅读--------8,音乐文件播放

来源:互联网 发布:南京大学网络教育学位 编辑:程序博客网 时间:2024/05/25 15:54

设置ListView的onItemClick函数为


    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {            Log.i(TAG, "on click"+ position);            mApp.getPlaybackKickstarter().initPlayback(mContext,mQuerySelection,Common.PLAY_ALL_SONGS,                    position, true , false);        }    };


这个函数调用initPlayback,这个函数根据界面情况,启动播放界面,或者后台音播放服务没开启的话,开启服务。这里先实现开启服务



  public void initPlayback(Context context,                             String querySelection,                             int playbackRouteId,                             int currentSongIndex,                             boolean showNowPlayingActivity,                             boolean playAll) {        mApp = (Common) mContext.getApplicationContext();        mQuerySelection = querySelection;        mPlaybackRouteId = playbackRouteId;        mCurrentSongIndex = currentSongIndex;        // TODO: 2016/6/2        startService();        if(showNowPlayingActivity){        }else {        }    }    /**     * Starts AudioPlaybackService. Once the service is running, we get a     * callback to onServiceRunning() (see below). That's where the method to     * build the cursor is called.     */    private void startService() {        Intent intent = new Intent(mContext, AudioPlaybackService.class);        mContext.startService(intent);    }

这儿启动AudioPlaybackService类
public class AudioPlaybackService extends Service {    //Context and Intent.    private Context mContext;    private Service mService;    //Global Objects Provider.    private Common mApp;    //MediaPlayer objects and flags.    private MediaPlayer mMediaPlayer;    private MediaPlayer mMediaPlayer2;    private int mCurrentMediaPlayer = 1;    private boolean mFirstRun = true;    //Handler object.    private Handler mHandler;    //AudioManager.    private AudioManager mAudioManager;    /**     * Constructor that should be used whenever this     * service is being explictly created.     *     * @param context The context being passed in.     */    public AudioPlaybackService(Context context) {        mContext = context;    }    /**     * Empty constructor. Required if a custom constructor     * was explicitly declared (see above).     */    public AudioPlaybackService() {        super();    }    /**     * Prepares the MediaPlayer objects for first use     * and starts the service. The workflow of the entire     * service starts here.     *     * @param intent  Calling intent.     * @param flags   Service flags.     * @param startId Service start ID.     */    @SuppressLint("NewApi")    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        super.onStartCommand(intent, flags, startId);//Context.        mContext = getApplicationContext();        mService = this;        mHandler = new Handler();        mApp = (Common) getApplicationContext();        mApp.setService((AudioPlaybackService) this);        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);        initMediaPlayers();        prepareMediaPlayer(0);        return START_STICKY;    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }    private void initMediaPlayers() {        if (mMediaPlayer != null) {            mMediaPlayer.release();            mMediaPlayer = null;        }        if (mMediaPlayer2 != null) {            // TODO: 2016/6/3        }        mMediaPlayer = new MediaPlayer();        setCurrentMediaPlayer(1);        getMediaPlayer().reset();        // TODO: 2016/6/3        getMediaPlayer().setLooping(true);        try {            getMediaPlayer().setWakeMode(mContext, PowerManager.PARTIAL_WAKE_LOCK);        } catch (Exception e) {            mMediaPlayer = new MediaPlayer();            setCurrentMediaPlayer(1);        }        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);    }    private void setCurrentMediaPlayer(int currentMediaPlayer) {        mCurrentMediaPlayer = currentMediaPlayer;    }    public MediaPlayer getMediaPlayer() {        return mMediaPlayer;    }    // TODO: 2016/6/3    /**     * Grabs the song parameters at the specified index, retrieves its     * data source, and beings to asynchronously prepare mMediaPlayer.     * Once mMediaPlayer is prepared, mediaPlayerPrepared is called.     *     * @return True if the method completed with no exceptions. False, otherwise.     */    public boolean prepareMediaPlayer(int songIndex) {        try{            mMediaPlayer.reset();            mMediaPlayer.setDataSource("/storage/emulated/0/Music/Goldberg_Variations.mp3");            mMediaPlayer.prepare();            mMediaPlayer.start();        }catch (Exception e){            e.printStackTrace();        }        return  true;    }}

这儿的onStartCommand函数里面调用preparMediaPlayer函数,这个函数我们先简单的让它播放音乐。这和源项目不一样,稍后我们去改。
现在试试吧。可以放音乐了。
0 0
原创粉丝点击