2 qq 32275135 qq_32275135 于 2016.04.24 09:34 提问

为何音乐播放器代码在esclipse中没报错,点击按钮却没有实现功能,上图上代码,求前辈们指教

图片说明![图片说明](http://img.ask.csdn.net/upload/201604/24/1461461584_552949.png)图片说明
Mainactivity
package com.example.music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnClickListener {
List musiclists = new ArrayList();
List> list = new ArrayList>();
Button play_pause, stop, onplay, downplay, close, exit;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
Intent intentservice;
// 定义音乐的播放状态 ,0X11 代表停止 ,0x12代表播放,0x13代表暂停
int status = 0x11;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    UIinit();
    logic();
    musicList();
    activityReceiver = new ActivityReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_ACTION);
    registerReceiver(activityReceiver, filter);
    intentservice = new Intent(this, MusicService.class);
    startService(intentservice);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void UIinit() {
    play_pause = (Button) this.findViewById(R.id.play_pause);
    stop = (Button) this.findViewById(R.id.stop);
    onplay = (Button) this.findViewById(R.id.onplay);
    close = (Button) this.findViewById(R.id.close);
    exit = (Button) this.findViewById(R.id.exit);
    downplay = (Button) this.findViewById(R.id.downplay);
}

public void logic() {
    play_pause.setOnClickListener(this);
    stop.setOnClickListener(this);
    onplay.setOnClickListener(this);
    downplay.setOnClickListener(this);
    close.setOnClickListener(this);
    exit.setOnClickListener(this);
}

@Override
public void onClick(View source) {
    Intent intent = new Intent(CTL_ACTION);
    switch (source.getId()) {
    case R.id.play_pause: {
        intent.putExtra("control", 1);
        break;
    }
    case R.id.stop: {
        intent.putExtra("control", 2);
        break;
    }
    case R.id.onplay: {
        intent.putExtra("control", 3);
        break;
    }
    case R.id.downplay: {
        intent.putExtra("control", 4);
        break;
    }
    case R.id.close: {
        this.finish();
        break;
    }
    case R.id.exit: {
        stopService(intentservice);
        this.finish();
        break;
    }
    }
    sendBroadcast(intent);

}


public class ActivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取Intent中的update消息,update代表播放状态
        int update = intent.getIntExtra("update", -1);
        switch (update) {
        case 0x11: {
            play_pause.setText("播放");
            status = 0x11;
            break;
        }

        // 控制系统进入播放状态
        case 0x12: {
            // 播放状态下设置使用按钮
            play_pause.setText("暂停");
            // 设置当前状态
            status = 0x12;
            break;
        }
        // 控制系统进入暂停状态
        case 0x13: {
            play_pause.setText("播放");
            status = 0x13;
            break;
        }
        }
    }

}

/* 播放列表 */
public void musicList() {
    // 取得指定位置的文件设置显示到播放列表
    String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
            Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
            music, null, null, null);
    while (cursor.moveToNext()) {
        Music temp = new Music();
        temp.setFilename(cursor.getString(1));
        temp.setTitle(cursor.getString(2));
        temp.setDuration(cursor.getInt(3));
        temp.setArtist(cursor.getString(4));
        temp.setData(cursor.getString(5));
        musiclists.add(temp);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", cursor.getString(1));
        map.put("artist", cursor.getString(4));
        list.add(map);
    }

    ListView listview = (ListView) findViewById(R.id.musics);
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.musicsshow, new String[] { "name", "artist" },
            new int[] { R.id.name, R.id.artist });
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int current, long id) {

            Intent intent=new Intent(CTL_ACTION);
            intent.putExtra("control", 5);
            intent.putExtra("current", current);
            sendBroadcast(intent);
        }
    });
}

}

TitanMusic.java
package com.example.titan;

public class TitanMusic {

private String filename;
private String Title;
private int   duration;
private String artist;
private String location;


public String getFilename() {
    return filename;
}
public void setFilename(String filename) {
    this.filename = filename;
}
public String getTitle() {
    return Title;
}
public void setTitle(String title) {
    Title = title;
}
public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}
public String getArtist() {
    return artist;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public String getData() {
    return location;
}
public void setData(String location) {
    this.location = location;
}

// {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
// MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION,
// MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA
}
TitanMusicService.java
package com.example.titan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;

public class TitanMusicService extends Service {
List musiclists = new ArrayList();
List> list = new ArrayList>();
MyReceiver serviceReceiver;
MediaPlayer mPlayer;
int status = 0x11;
//歌曲下标
int current = 0;
int count = 0;
int flog = 0;
//String filename;
@Override
public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public void onDestroy() {
    flog=0;
    mPlayer.stop();
    mPlayer.release();
    super.onDestroy();
}

@Override
public void onCreate() {
    flog=1;
    musicList();
    count = list.size();
    Log.e("Service", "onStart");
    serviceReceiver = new MyReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(MainActivity.CTL_ACTION);
    registerReceiver(serviceReceiver, filter);
    mPlayer = new MediaPlayer();
    mPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            current++;
            if (current >= count) {
                current = 0;
            }
            String filename = ((TitanMusic) musiclists.get(current)).getData();
            playMusic(filename);
        }
    });

    super.onCreate();
}


@Override
public void onStart(Intent intent, int startId) {   
    if(flog==2){
        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
        sendIntent.putExtra("update", status);
        //sendIntent.putExtra("current", current);
        sendBroadcast(sendIntent);
    }
    flog=2;
}





 private void playMusic(String path)   
    {   
        try   
        {   
            /* 重置MediaPlayer */   
            mPlayer.reset();   
            /* 设置要播放的文件的路径 */   
            mPlayer.setDataSource(path);   
            /* 准备播放 */   
            mPlayer.prepare();   
            /* 开始播放 */   
            mPlayer.start();   
            mPlayer.setOnCompletionListener(new OnCompletionListener()    
            {   
                public void onCompletion(MediaPlayer arg0)   
                {   
                    //播放完成一首之后进行下一首   
                    current--;
                    if (current < 0) {
                        current = count;
                    }
                    playMusic( ((TitanMusic) musiclists.get(current)).getData());
                    status = 0x12;
                }   
            });   
        }catch (IOException e){}   
    }   

    /* 播放列表 */
    public void musicList() {
        // 取得指定位置的文件设置显示到播放列表
        String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
                Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
        Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
                music, null, null, null);
        while (cursor.moveToNext()) {
            TitanMusic temp = new TitanMusic();
            temp.setFilename(cursor.getString(1));
            temp.setTitle(cursor.getString(2));
            temp.setDuration(cursor.getInt(3));
            temp.setArtist(cursor.getString(4));
            temp.setData(cursor.getString(5));
            musiclists.add(temp);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", cursor.getString(1));
            map.put("artist", cursor.getString(4));
            list.add(map);
        }
    }

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int control = intent.getIntExtra("control", -1);
        switch (control) {
        case 1: {
            // 如果原来处于停止的状态
            if (status == 0x11) {
                playMusic( ((TitanMusic) musiclists.get(current)).getData());
                status = 0x12;
            }
            // 原来处于播放状态
            else if (status == 0x12) {
                mPlayer.pause();
                status = 0x13;
            }
            // 原来处于暂停状态
            else if (status == 0x13) {
                mPlayer.start();
                status = 0x12;
            }
            break;
        }
        case 2: {
            // 如果原来正在播放或暂停
            if (status == 0x12 || status == 0x13) {
                mPlayer.stop();
                status = 0x11;
            }
            break;
        }
        case 3: {
            current--;
            if (current < 0) {
                current = count;
            }
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        case 4: {
            current++;
            if (current > count) {
                current = 0;
            }
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        case 5: {
            current = intent.getIntExtra("current", -1);;
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        }
        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
        sendIntent.putExtra("update", status);
    //  sendIntent.putExtra("current", current);
        sendBroadcast(sendIntent);
    }

}

}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/onplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一首" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/play_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />

    </LinearLayout>
    <RelativeLayout 
         android:layout_width="match_parent"
    android:layout_height="wrap_content">

       <Button
           android:id="@+id/downplay"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_alignParentTop="true"
           android:text="下一首" />

     </RelativeLayout>
</LinearLayout>
<!--
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="音乐列表"
    android:textColor="#ff0000ff"
    android:textSize="30sp" />
-->
<ListView
    android:id="@+id/musics"
    android:layout_width="match_parent"
    android:layout_height="257dp"
    android:layout_weight="1.53" >
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
  <RelativeLayout 
       android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="最小化" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="退出" />
</RelativeLayout>
</LinearLayout>


歌曲布局:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp" />

<TextView
    android:id="@+id/artist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp" />


3个回答

qq_32275135
qq_32275135   2016.04.24 09:34

图片说明

qq_32275135
qq_32275135   2016.04.24 09:43

求前辈们看一下啊啊啊啊啊啊啊啊啊,这是我们的作业啊

suwu150
suwu150   2016.04.24 15:00

报啥错了?弄个logcat 的图出来,可能是按钮事件出错了,你在仔细检查下按钮事件

Csdn user default icon
上传中...
上传图片
插入图片