zoukankan      html  css  js  c++  java
  • Android第三次作业

    1、实现的功能

    • 实现播放,暂停,播放上一首,下一首功能
    • 显示播放列表
    • 可以播放3首歌曲

    2、实现步骤

    1、截图:

     

    2、主要功能代码:

     java代码:

    
    
    package com.example.administrator.myapplication;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Timer;
    import java.util.TimerTask;
    import android.app.Activity;
    import android.database.Cursor;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ImageButton;
    import android.widget.ListView;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.widget.SimpleCursorAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.musicplayer.R;
    public class MainActivity extends Activity {
    public ImageButton ibutton_play,ibutton_backward,ibutton_forward,ibutton_stop;
    public TextView text1,text2,text3;
    public ListView listview;
    public SeekBar seekbar;
    public MediaPlayer player = null;
    private static int flag = 0;
    private Timer timer;
    private TimerTask timertask_music;
    private volatile boolean ischanging=false;
    private static final int msgKey1 = 1;
    private static int listPosition = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findView();
    initListview();
    player = new MediaPlayer();
    viewAction();
    timer=new Timer();
    timertask_music=new TimerTask() {
    @Override
    public void run() {
    // TODO Auto-generated method stub
    if(ischanging==true){
    return;
    }
    else{
    seekbar.setProgress(player.getCurrentPosition());
    }
    }
    };
    timer.schedule(timertask_music, 0, 10);
    new TimeThread().start();
    }
    private void findView(){
    text1 = (TextView) findViewById(R.id.usetime);
    text2 = (TextView) findViewById(R.id.alltime);
    text3 = (TextView) findViewById(R.id.lefttime);
    seekbar = (SeekBar) findViewById(R.id.seekBar);
    listview = (ListView) findViewById(R.id.listview);
    ibutton_play = (ImageButton) findViewById(R.id.play);
    ibutton_forward = (ImageButton) findViewById(R.id.forward);
    ibutton_backward = (ImageButton) findViewById(R.id.backward);
    ibutton_stop = (ImageButton) findViewById(R.id.stop);
    }
    private void initListview(){
    Cursor mAudioCursor = this.getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    null,
    null,
    null,
    MediaStore.Audio.AudioColumns.TITLE);
    List<Map<String, Object>> mListData = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < mAudioCursor.getCount(); i++) {
    mAudioCursor.moveToNext();
    int indexTitle = mAudioCursor
    .getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
    int indexARTIST = mAudioCursor
    .getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
    int indexALBUM = mAudioCursor
    .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);
    String strTitle = mAudioCursor.getString(indexTitle);
    String strARTIST = mAudioCursor.getString(indexARTIST);
    String strALBUM = mAudioCursor.getString(indexALBUM);

    HashMap<String, Object> nowMap = new HashMap<String, Object>();
    nowMap.put("SongName", strTitle + "---" + strARTIST
    + "---" + strALBUM);
    mListData.add(nowMap);
    }
    listview.setAdapter(new SimpleCursorAdapter(MainActivity.this,
    android.R.layout.simple_expandable_list_item_1,
    mAudioCursor,
    new String[] {MediaStore.Audio.AudioColumns.TITLE,MediaStore.Audio.AudioColumns.ARTIST},
    new int[] {android.R.id.text1}));
    }
    private void viewAction(){
    ibutton_play.setOnClickListener(new ClickAction());
    ibutton_forward.setOnClickListener(new ClickAction());
    ibutton_backward.setOnClickListener(new ClickAction());
    ibutton_stop.setOnClickListener(new ClickAction());
    seekbar.setOnSeekBarChangeListener(new SeekbarAction());
    listview.setOnItemClickListener(new ItemClickAction());
    }
    private void initMediaPlayer(long arg){
    try{
    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, String.valueOf(arg));
    if(player.isPlaying()){
    player.stop();
    }
    player = MediaPlayer.create(this.getApplicationContext(),uri);
    player.prepare();
    }catch(IllegalStateException e){
    e.printStackTrace();
    }catch (IOException e) {
    // TODO: handle exception
    e.printStackTrace();
    }catch(NullPointerException ne){
    ne.printStackTrace();
    }
    seekbar.setMax(player.getDuration());
    text2.setText(setTimeFormat(player.getDuration()));
    }
    private String setTimeFormat(int time){
    SimpleDateFormat tempDate = new SimpleDateFormat("mm:ss");
    String datetime = tempDate.format(time);
    return datetime;
    }
    private void doMusic(long arg){
    initMediaPlayer(arg);
    viewAction();
    }
    private class TimeThread extends Thread{
    private Handler mhandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    switch (msg.what) {
    case msgKey1:
    text1.setText(setTimeFormat(player.getCurrentPosition()));
    text3.setText(setTimeFormat(player.getDuration()-player.getCurrentPosition()));
    break;
    default:
    break;
    }
    super.handleMessage(msg);
    }
    };
    @Override
    public void run() {
    // TODO Auto-generated method stub
    while(true){
    try{
    Thread.sleep(1000);
    Message msg = new Message();
    msg.what = msgKey1;
    mhandler.sendMessage(msg);
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }
    }
    }
    /*对控件点击事件的处理*/
    private class ClickAction implements OnClickListener{
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == ibutton_play){
    seekbar.setMax(player.getDuration());
    if(flag == 0){
    player.start();
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.pause));
    flag = 1;
    }else{
    player.pause();
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.play));
    flag = 0;
    }
    }else if(v == ibutton_stop){
    player.pause();
    player.seekTo(0);
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.play));
    }else if(v == ibutton_forward){
    if(MainActivity.listPosition == 0){
    player.seekTo(0);
    Toast.makeText(MainActivity.this, "已经是第一首歌了!", Toast.LENGTH_LONG).show();
    }else{
    player.pause();
    player.seekTo(0);
    MainActivity.listPosition--;
    doMusic(listview.getItemIdAtPosition(MainActivity.listPosition));
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.pause));
    flag = 1;
    player.start();
    Toast.makeText(MainActivity.this, "第"+(MainActivity.listPosition+1)+"首歌!", Toast.LENGTH_LONG).show();
    }
    }else if(v == ibutton_backward){

    if((listview.getCount()-1) == MainActivity.listPosition){
    player.seekTo(0);
    Toast.makeText(MainActivity.this, "已经是最后一首歌了!"+listview.getCount(), Toast.LENGTH_LONG).show();
    }else{
    player.pause();
    player.seekTo(0);
    MainActivity.listPosition++;
    doMusic(listview.getItemIdAtPosition(MainActivity.listPosition));
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.pause));
    flag = 1;
    player.start();
    Toast.makeText(MainActivity.this, "第"+(MainActivity.listPosition+1)+"首歌!", Toast.LENGTH_LONG).show();
    }
    }
    }
    }
    private class ItemClickAction implements OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    // TODO Auto-generated method stub
    doMusic(id);
    MainActivity.listPosition = position;
    //Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_LONG).show();
    ibutton_play.setImageDrawable(getResources().getDrawable(R.drawable.pause));
    flag = 1;
    player.start();
    Toast.makeText(MainActivity.this, "第"+(MainActivity.listPosition+1)+"首歌!", Toast.LENGTH_LONG).show();
    }
    }
    /*对进度条拖动事件的处理*/
    private class SeekbarAction implements OnSeekBarChangeListener{
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    // TODO Auto-generated method stub
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    ischanging=true;//停止timer
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    player.seekTo(seekBar.getProgress());
    ischanging=false;//开启timer
    }
    }
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    player.stop();
    player.release();
    super.onDestroy();
    }
    }
    
    
  • 相关阅读:
    Caffe_Example之训练mnist
    监督学习和无监督学习
    linux 命令cp拷贝
    Caffe solver.prototxt学习
    caffe下python环境的编译
    ubuntu 绘制lenet网络结构图遇到的问题汇总
    1-6 能否形成三角形
    Python的四个内置数据类型list, tuple, dict, set
    Python 函数(二)
    Python 函数(一)
  • 原文地址:https://www.cnblogs.com/Suejun/p/10105850.html
Copyright © 2011-2022 走看看